Vera-ZWY commited on
Commit
cbaaf94
·
verified ·
1 Parent(s): bb3ba32

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -6
app.py CHANGED
@@ -7,7 +7,7 @@ import os
7
  HF_TOKEN = os.getenv("HF_TOKEN") # Replace with your actual token if not using an environment variable
8
 
9
  # Initialize the Gradio Client for the specified API
10
- client = Client("mangoesai/Elections_Comparison_Agent_V3", hf_token=HF_TOKEN)
11
 
12
  client_name = ['2016 Election','2024 Election', 'Comparison two years']
13
 
@@ -15,7 +15,6 @@ client_name = ['2016 Election','2024 Election', 'Comparison two years']
15
 
16
  def stream_chat_with_rag(
17
  message: str,
18
- # history: list,
19
  client_name: str
20
  ):
21
  # print(f"Message: {message}")
@@ -33,11 +32,36 @@ def stream_chat_with_rag(
33
 
34
  return answer, fig
35
 
 
 
 
 
 
 
 
 
36
 
37
 
38
  # Create Gradio interface
39
  with gr.Blocks(title="Reddit Election Analysis") as demo:
40
  gr.Markdown("# Reddit Public sentiment & Social topic distribution ")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41
 
42
 
43
 
@@ -54,7 +78,8 @@ with gr.Blocks(title="Reddit Election Analysis") as demo:
54
 
55
  query_input = gr.Textbox(
56
  label="Your Question",
57
- placeholder="Ask about election comments or posts..."
 
58
  )
59
 
60
  submit_btn = gr.Button("Submit")
@@ -86,18 +111,22 @@ with gr.Blocks(title="Reddit Election Analysis") as demo:
86
  width: 100%;
87
  margin: auto;
88
  }
 
 
 
 
 
89
  </style>
90
  """)
91
 
92
  # Update both outputs when submit is clicked
93
- submit_btn.click(
94
- fn=stream_chat_with_rag,
95
  inputs=[query_input, year_selector],
96
  outputs=[output_text, output_plot]
97
  )
98
 
99
 
100
-
101
  if __name__ == "__main__":
102
  demo.launch(share=True)
103
 
 
7
  HF_TOKEN = os.getenv("HF_TOKEN") # Replace with your actual token if not using an environment variable
8
 
9
  # Initialize the Gradio Client for the specified API
10
+ client = Client("mangoesai/Elections_Comparison_Agent_V4", hf_token=HF_TOKEN)
11
 
12
  client_name = ['2016 Election','2024 Election', 'Comparison two years']
13
 
 
15
 
16
  def stream_chat_with_rag(
17
  message: str,
 
18
  client_name: str
19
  ):
20
  # print(f"Message: {message}")
 
32
 
33
  return answer, fig
34
 
35
+ def heatmap(top_n):
36
+ map = client.predict(
37
+ top_n = top_n,
38
+ api_name = "/heatmap"
39
+ )
40
+
41
+ return map
42
+
43
 
44
 
45
  # Create Gradio interface
46
  with gr.Blocks(title="Reddit Election Analysis") as demo:
47
  gr.Markdown("# Reddit Public sentiment & Social topic distribution ")
48
+ with gr.Row():
49
+ with gr.Column():
50
+ with gr.Row():
51
+ top_n = gr.Dropdown(choices= [1,2,3,4,5,6,7,8,9,10])
52
+ with gr.Row():
53
+ fresh_btn = gr.Button("Refresh Heatmap")
54
+ with gr.Column():
55
+ output_heatmap = gr.Plot(
56
+ label="Top Public sentiment & Social topic Heatmap",
57
+ container=True, # Ensures the plot is contained within its area
58
+ elem_classes="heatmap-plot" # Add a custom class for styling
59
+ )
60
+ fresh_btn.click(
61
+ fn=heatmap,
62
+ inputs=top_n,
63
+ outputs=output_heatmap
64
+ )
65
 
66
 
67
 
 
78
 
79
  query_input = gr.Textbox(
80
  label="Your Question",
81
+ placeholder="Ask about election comments or posts, like, Is there any comments don't like the election results?",
82
+ value = "Is there any comments don't like the election results"
83
  )
84
 
85
  submit_btn = gr.Button("Submit")
 
111
  width: 100%;
112
  margin: auto;
113
  }
114
+ .heatmap-plot {
115
+ min-height: 400px;
116
+ width: 100%;
117
+ margin: auto;
118
+ }
119
  </style>
120
  """)
121
 
122
  # Update both outputs when submit is clicked
123
+ submit_btn.stream_chat_with_rag(
124
+ fn=process_query,
125
  inputs=[query_input, year_selector],
126
  outputs=[output_text, output_plot]
127
  )
128
 
129
 
 
130
  if __name__ == "__main__":
131
  demo.launch(share=True)
132