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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -40
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_Comparing_Agent_V2", hf_token=HF_TOKEN)
11
 
12
  client_name = ['2016 Election','2024 Election', 'Comparison two years']
13
 
@@ -19,22 +19,8 @@ def stream_chat_with_rag(
19
  client_name: str
20
  ):
21
  # print(f"Message: {message}")
22
- # print(f"History: {history}")
23
-
24
- # # Build the conversation prompt including system prompt and history
25
- # conversation = f"For Client: {client_name}\n"
26
-
27
- # # Add previous conversation history
28
- # for user_input, assistant_response in history:
29
- # conversation += f"User: {user_input}\nAssistant: {assistant_response}\n"
30
-
31
- # # Add the current user message
32
- # conversation += f"User: {message}\nAssistant:"
33
-
34
- # # # Call the API with the user's process_query
35
- # question = message
36
  #answer = client.predict(question=question, api_name="/run_graph")
37
- answer = client.predict(
38
  query= message,
39
  election_year=client_name,
40
  api_name="/process_query"
@@ -45,54 +31,72 @@ def stream_chat_with_rag(
45
  print(answer)
46
 
47
 
48
- return answer
49
 
50
 
51
 
52
  # Create Gradio interface
53
- with gr.Blocks(title="Reddit Election Comments Analysis") as demo:
54
- gr.Markdown("# Reddit Election Comments Analysis")
 
 
 
 
55
  gr.Markdown("Ask questions about election-related comments and posts")
56
-
57
  with gr.Row():
58
  with gr.Column():
59
- # Add election year selector
60
  year_selector = gr.Radio(
61
  choices=["2016 Election", "2024 Election", "Comparison two years"],
62
  label="Select Election Year",
63
- value="2016 Election" # Default value
64
  )
65
-
66
  query_input = gr.Textbox(
67
  label="Your Question",
68
  placeholder="Ask about election comments or posts..."
69
  )
70
- # context_input = gr.Textbox(
71
- # label="Context (Optional)",
72
- # value = "Looking for discussions about the election results in 2016" #default value
73
- # )
74
  submit_btn = gr.Button("Submit")
75
-
 
 
 
 
 
 
76
  with gr.Column():
77
- output = gr.Textbox(
78
  label="Response",
79
  lines=20
80
  )
81
-
82
- # Update submit button to include year selection
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
83
  submit_btn.click(
84
  fn=stream_chat_with_rag,
85
  inputs=[query_input, year_selector],
86
- outputs=output
87
  )
88
-
89
- gr.Markdown("""
90
- ## Example Questions:
91
- - Is there any comments don't like the election results
92
- - Summarize the main discussions about voting process
93
- - What are the common opinions about candidates?
94
- - How have people's attitudes toward the Republican Party changed in the past two years?
95
- """)
96
 
97
  if __name__ == "__main__":
98
  demo.launch(share=True)
 
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
 
 
19
  client_name: str
20
  ):
21
  # print(f"Message: {message}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
  #answer = client.predict(question=question, api_name="/run_graph")
23
+ answer, fig = client.predict(
24
  query= message,
25
  election_year=client_name,
26
  api_name="/process_query"
 
31
  print(answer)
32
 
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
+
44
+ gr.Markdown("# Reddit Election Posts/Comments Analysis")
45
  gr.Markdown("Ask questions about election-related comments and posts")
46
+
47
  with gr.Row():
48
  with gr.Column():
 
49
  year_selector = gr.Radio(
50
  choices=["2016 Election", "2024 Election", "Comparison two years"],
51
  label="Select Election Year",
52
+ value="2016 Election"
53
  )
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")
61
+
62
+ gr.Markdown("""
63
+ ## Example Questions:
64
+ - Is there any comments don't like the election results
65
+ - Summarize the main discussions about voting process
66
+ - What are the common opinions about candidates?
67
+ """)
68
  with gr.Column():
69
+ output_text = gr.Textbox(
70
  label="Response",
71
  lines=20
72
  )
73
+
74
+ with gr.Row():
75
+ output_plot = gr.Plot(
76
+ label="Topic Distribution",
77
+ container=True, # Ensures the plot is contained within its area
78
+ elem_classes="topic-plot" # Add a custom class for styling
79
+ )
80
+
81
+ # Add custom CSS to ensure proper plot sizing
82
+ gr.HTML("""
83
+ <style>
84
+ .topic-plot {
85
+ min-height: 600px;
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)