Vera-ZWY commited on
Commit
9a76e3e
·
verified ·
1 Parent(s): 2e61e42

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +63 -37
app.py CHANGED
@@ -7,7 +7,7 @@ import pandas as pd
7
  from io import StringIO, BytesIO
8
  import base64
9
  import json
10
- import plotly.io as pio
11
  # from linePlot import plot_stacked_time_series, plot_emotion_topic_grid
12
 
13
  # Define your Hugging Face token (make sure to set it as an environment variable)
@@ -36,24 +36,42 @@ def stream_chat_with_rag(
36
  # Debugging: Print the raw response
37
  print("Raw answer from API:")
38
  print(answer)
39
- print("top works from API:")
40
- print(fig)
41
 
42
- fig_dict = json.loads(plotly_data['plot'])
43
 
44
- # Render the figure
45
- fig = pio.from_json(json.dumps(fig_dict))
46
- fig.show()
47
  return answer, fig
48
 
49
 
 
 
 
 
 
 
 
 
 
 
50
 
51
- def chat_function(chat_history, year, user_message):
52
- # Mock response (replace this with your logic for generating a response)
53
- response = f"Year selected: {year}. Here's a response."
54
- answer = stream_chat_with_rag(user_message, year)[0]
55
- chat_history.append((user_message, response +"\n"+ answer))
56
- return chat_history, ""
 
 
 
 
 
 
 
 
57
 
58
  chatbot_state = gr.State([])
59
 
@@ -186,26 +204,26 @@ with gr.Blocks(title="Reddit Election Analysis") as demo:
186
  gr.Markdown("Ask questions about election-related comments and posts")
187
 
188
  with gr.Row():
189
- with gr.Column():
190
- year_selector = gr.Radio(
191
- choices=["2016 Election", "2024 Election", "Comparison two years"],
192
- label="Select Election Year",
193
- value="2016 Election"
194
- )
195
 
196
- query_input = gr.Textbox(
197
- label="Your Question",
198
- placeholder="Ask about election comments or posts..."
199
- )
200
 
201
- submit_btn = gr.Button("Submit")
202
 
203
- gr.Markdown("""
204
- ## Example Questions:
205
- - Is there any comments don't like the election results
206
- - Summarize the main discussions about voting process
207
- - What are the common opinions about candidates?
208
- """)
209
  # with gr.Column():
210
  # output_text = gr.Textbox(
211
  # label="Response",
@@ -213,7 +231,15 @@ with gr.Blocks(title="Reddit Election Analysis") as demo:
213
  # )
214
 
215
  with gr.Column():
216
- chatbot = gr.Chatbot(label="Chat History")
 
 
 
 
 
 
 
 
217
 
218
  gr.Markdown("## Top works of the relevant Q&A")
219
  # with gr.Row():
@@ -254,12 +280,12 @@ with gr.Blocks(title="Reddit Election Analysis") as demo:
254
  outputs = [time_series_fig, linePlot_status_text]
255
  )
256
 
257
- # Update both outputs when submit is clicked
258
- submit_btn.click(
259
- fn=chat_function,
260
- inputs=[query_input, year_selector],
261
- outputs= [chatbot, query_input]
262
- )
263
 
264
 
265
  if __name__ == "__main__":
 
7
  from io import StringIO, BytesIO
8
  import base64
9
  import json
10
+ # import plotly.io as pio
11
  # from linePlot import plot_stacked_time_series, plot_emotion_topic_grid
12
 
13
  # Define your Hugging Face token (make sure to set it as an environment variable)
 
36
  # Debugging: Print the raw response
37
  print("Raw answer from API:")
38
  print(answer)
39
+ # print("top works from API:")
40
+ # print(fig)
41
 
42
+ # fig_dict = json.loads(plotly_data['plot'])
43
 
44
+ # # Render the figure
45
+ # fig = pio.from_json(json.dumps(fig_dict))
46
+ # fig.show()
47
  return answer, fig
48
 
49
 
50
+ def predict(message, history):
51
+ history_langchain_format = []
52
+ for msg in history:
53
+ if msg['role'] == "user":
54
+ history_langchain_format.append(HumanMessage(content=msg['content']))
55
+ elif msg['role'] == "assistant":
56
+ history_langchain_format.append(AIMessage(content=msg['content']))
57
+ history_langchain_format.append(HumanMessage(content=message))
58
+ gpt_response = llm(history_langchain_format)
59
+ return gpt_response.content
60
 
61
+ def chat_function(message, history, year):
62
+ history_langchain_format = []
63
+ for msg in history:
64
+ if msg['role'] == "user":
65
+ history_langchain_format.append(HumanMessage(content=msg['content']))
66
+ elif msg['role'] == "assistant":
67
+ history_langchain_format.append(AIMessage(content=msg['content']))
68
+ history_langchain_format.append(HumanMessage(content=message))
69
+ rag_response = stream_chat_with_rag(history_langchain_format,year)[0]
70
+
71
+ # response = f"Year selected: {year}. Here's a response."
72
+ # answer = stream_chat_with_rag(user_message, year)[0]
73
+ # chat_history.append((user_message, response +"\n"+ answer))
74
+ return rag_response
75
 
76
  chatbot_state = gr.State([])
77
 
 
204
  gr.Markdown("Ask questions about election-related comments and posts")
205
 
206
  with gr.Row():
207
+ # with gr.Column():
208
+ # year_selector = gr.Radio(
209
+ # choices=["2016 Election", "2024 Election", "Comparison two years"],
210
+ # label="Select Election Year",
211
+ # value="2016 Election"
212
+ # )
213
 
214
+ # query_input = gr.Textbox(
215
+ # label="Your Question",
216
+ # placeholder="Ask about election comments or posts..."
217
+ # )
218
 
219
+ # submit_btn = gr.Button("Submit")
220
 
221
+ # gr.Markdown("""
222
+ # ## Example Questions:
223
+ # - Is there any comments don't like the election results
224
+ # - Summarize the main discussions about voting process
225
+ # - What are the common opinions about candidates?
226
+ # """)
227
  # with gr.Column():
228
  # output_text = gr.Textbox(
229
  # label="Response",
 
231
  # )
232
 
233
  with gr.Column():
234
+ chatbot = gr.ChatInterface(chat_function,
235
+ type="messages",
236
+ addtional_inputs = [
237
+ year_selector = gr.Radio(
238
+ choices=["2016 Election", "2024 Election", "Comparison two years"],
239
+ label="Select Election Year",
240
+ value="2016 Election"
241
+ )
242
+ ])
243
 
244
  gr.Markdown("## Top works of the relevant Q&A")
245
  # with gr.Row():
 
280
  outputs = [time_series_fig, linePlot_status_text]
281
  )
282
 
283
+ # # Update both outputs when submit is clicked
284
+ # submit_btn.click(
285
+ # fn=chat_function,
286
+ # inputs=[query_input, year_selector],
287
+ # outputs= chatbot
288
+ # )
289
 
290
 
291
  if __name__ == "__main__":