Spaces:
Runtime error
Runtime error
Update app.py
Browse files
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(
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
|
| 201 |
-
|
| 202 |
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 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.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
|
| 260 |
-
|
| 261 |
-
|
| 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__":
|