|
import gradio as gr |
|
from gradio_client import Client, handle_file |
|
import os |
|
|
|
|
|
|
|
HF_TOKEN = os.getenv("HF_TOKEN") |
|
|
|
|
|
client = Client("mangoesai/Elections_Comparison_Agent_V4", hf_token=HF_TOKEN) |
|
|
|
client_name = ['2016 Election','2024 Election', 'Comparison two years'] |
|
|
|
|
|
|
|
def stream_chat_with_rag( |
|
message: str, |
|
|
|
client_name: str |
|
): |
|
|
|
|
|
answer, fig = client.predict( |
|
query= message, |
|
election_year=client_name, |
|
api_name="/process_query" |
|
) |
|
|
|
|
|
print("Raw answer from API:") |
|
print(answer) |
|
|
|
|
|
return answer, fig |
|
|
|
|
|
|
|
|
|
with gr.Blocks(title="Reddit Election Analysis") as demo: |
|
gr.Markdown("# Reddit Public sentiment & Social topic distribution ") |
|
|
|
|
|
|
|
gr.Markdown("# Reddit Election Posts/Comments Analysis") |
|
gr.Markdown("Ask questions about election-related comments and posts") |
|
|
|
with gr.Row(): |
|
with gr.Column(): |
|
year_selector = gr.Radio( |
|
choices=["2016 Election", "2024 Election", "Comparison two years"], |
|
label="Select Election Year", |
|
value="2016 Election" |
|
) |
|
|
|
query_input = gr.Textbox( |
|
label="Your Question", |
|
placeholder="Ask about election comments or posts..." |
|
) |
|
|
|
submit_btn = gr.Button("Submit") |
|
|
|
gr.Markdown(""" |
|
## Example Questions: |
|
- Is there any comments don't like the election results |
|
- Summarize the main discussions about voting process |
|
- What are the common opinions about candidates? |
|
""") |
|
with gr.Column(): |
|
output_text = gr.Textbox( |
|
label="Response", |
|
lines=20 |
|
) |
|
|
|
with gr.Row(): |
|
output_plot = gr.Plot( |
|
label="Topic Distribution", |
|
container=True, |
|
elem_classes="topic-plot" |
|
) |
|
|
|
|
|
gr.HTML(""" |
|
<style> |
|
.topic-plot { |
|
min-height: 600px; |
|
width: 100%; |
|
margin: auto; |
|
} |
|
</style> |
|
""") |
|
|
|
|
|
submit_btn.click( |
|
fn=stream_chat_with_rag, |
|
inputs=[query_input, year_selector], |
|
outputs=[output_text, output_plot] |
|
) |
|
|
|
|
|
|
|
if __name__ == "__main__": |
|
demo.launch(share=True) |
|
|
|
|
|
|
|
|