Rename app (2).py to app.py
Browse files- app (2).py +0 -112
- app.py +106 -0
app (2).py
DELETED
@@ -1,112 +0,0 @@
|
|
1 |
-
import gradio as gr
|
2 |
-
import os
|
3 |
-
import agent
|
4 |
-
# import comparison
|
5 |
-
from dotenv import load_dotenv
|
6 |
-
|
7 |
-
# Load environment variables
|
8 |
-
load_dotenv()
|
9 |
-
|
10 |
-
# Initialize the RAG Agent for different election years
|
11 |
-
|
12 |
-
agent = agent.RAGAgent(
|
13 |
-
api_key=os.getenv("PINECONE_API_KEY"),
|
14 |
-
# environment=os.getenv("PINECONE_ENVIRONMENT"),
|
15 |
-
index_name=os.getenv("PINECONE_INDEX")
|
16 |
-
)
|
17 |
-
# agent_two = comparison.RAGAgent(
|
18 |
-
# api_key=os.getenv("PINECONE_API_KEY"),
|
19 |
-
# # environment=os.getenv("PINECONE_ENVIRONMENT"),
|
20 |
-
# index_name=os.getenv("PINECONE_INDEX")
|
21 |
-
# )
|
22 |
-
def format_sources(sources):
|
23 |
-
"""Format source nodes for display"""
|
24 |
-
if not sources:
|
25 |
-
return ""
|
26 |
-
|
27 |
-
formatted = "Top Source Submissions/Comments:\n" + "-" * 50 + "\n"
|
28 |
-
for i, source in enumerate(sources, 1):
|
29 |
-
formatted += f"\n{i}. Text Content:\n{source['text']}\n"
|
30 |
-
formatted += f"Type: {source['metadata'].get('type', 'N/A')}\n"
|
31 |
-
formatted += f"Submission: {source['metadata'].get('post_title', 'N/A')}\n"
|
32 |
-
formatted += f"Created: {source['metadata'].get('created_time', 'N/A')}\n"
|
33 |
-
formatted += f"Score: {source['metadata'].get('score', 'N/A')}\n"
|
34 |
-
formatted += f"Year: {source['metadata'].get('year', 'N/A')}\n"
|
35 |
-
|
36 |
-
formatted += "-" * 50 + "\n"
|
37 |
-
return formatted
|
38 |
-
|
39 |
-
def process_query(query, election_year):
|
40 |
-
"""Process the query based on selected election year"""
|
41 |
-
# Select the appropriate agent
|
42 |
-
# agent = agent_2016 if election_year == "2016 Election" else agent_2024
|
43 |
-
if (election_year == "2016 Election") or (election_year == "2024 Election"):
|
44 |
-
# agent_select = agent_one
|
45 |
-
context = f"Looking for discussions about the election in {election_year}"
|
46 |
-
|
47 |
-
result = agent.query(query, context)
|
48 |
-
response = f"Analysis for {election_year}:\n\n"
|
49 |
-
response += f"Answer: {result['answer']}\n\n"
|
50 |
-
response += format_sources(result['sources'])
|
51 |
-
else:
|
52 |
-
# agent_select = agent_two
|
53 |
-
context = f"Compare the discussions about the election between 2016 and 2024."
|
54 |
-
|
55 |
-
result = agent.compare_with_context(query)
|
56 |
-
response = f"Analysis for {election_year}:\n\n"
|
57 |
-
response += f"Answer: {result['answer']}\n\n"
|
58 |
-
response += format_sources(result['sources'])
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
# result = agent_select.query(query, context)
|
63 |
-
|
64 |
-
|
65 |
-
return response
|
66 |
-
|
67 |
-
# Create Gradio interface
|
68 |
-
with gr.Blocks(title="Reddit Election Comments Analysis") as demo:
|
69 |
-
gr.Markdown("# Reddit Election Comments Analysis")
|
70 |
-
gr.Markdown("Ask questions about election-related comments and posts")
|
71 |
-
|
72 |
-
with gr.Row():
|
73 |
-
with gr.Column():
|
74 |
-
# Add election year selector
|
75 |
-
year_selector = gr.Radio(
|
76 |
-
choices=["2016 Election", "2024 Election", "Comparison two years"],
|
77 |
-
label="Select Election Year",
|
78 |
-
value="2016 Election" # Default value
|
79 |
-
)
|
80 |
-
|
81 |
-
query_input = gr.Textbox(
|
82 |
-
label="Your Question",
|
83 |
-
placeholder="Ask about election comments or posts..."
|
84 |
-
)
|
85 |
-
# context_input = gr.Textbox(
|
86 |
-
# label="Context (Optional)",
|
87 |
-
# value = "Looking for discussions about the election results in 2016" #default value
|
88 |
-
# )
|
89 |
-
submit_btn = gr.Button("Submit")
|
90 |
-
|
91 |
-
with gr.Column():
|
92 |
-
output = gr.Textbox(
|
93 |
-
label="Response",
|
94 |
-
lines=20
|
95 |
-
)
|
96 |
-
|
97 |
-
# Update submit button to include year selection
|
98 |
-
submit_btn.click(
|
99 |
-
fn=process_query,
|
100 |
-
inputs=[query_input, year_selector],
|
101 |
-
outputs=output
|
102 |
-
)
|
103 |
-
|
104 |
-
gr.Markdown("""
|
105 |
-
## Example Questions:
|
106 |
-
- Is there any comments don't like the election results
|
107 |
-
- Summarize the main discussions about voting process
|
108 |
-
- What are the common opinions about candidates?
|
109 |
-
""")
|
110 |
-
|
111 |
-
if __name__ == "__main__":
|
112 |
-
demo.launch(share=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
app.py
ADDED
@@ -0,0 +1,106 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from gradio_client import Client, handle_file
|
3 |
+
import os
|
4 |
+
|
5 |
+
|
6 |
+
# Define your Hugging Face token (make sure to set it as an environment variable)
|
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 |
+
|
14 |
+
|
15 |
+
|
16 |
+
def stream_chat_with_rag(
|
17 |
+
message: str,
|
18 |
+
history: list,
|
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"{system_prompt}\n\nFor 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"
|
41 |
+
)
|
42 |
+
|
43 |
+
# Debugging: Print the raw response
|
44 |
+
print("Raw answer from API:")
|
45 |
+
print(answer)
|
46 |
+
|
47 |
+
|
48 |
+
return answer
|
49 |
+
|
50 |
+
|
51 |
+
|
52 |
+
|
53 |
+
|
54 |
+
# Title for the application
|
55 |
+
TITLE = "<h1 style='text-align:center;'>Reddit Election Q&A agent v0.1</h1>"
|
56 |
+
|
57 |
+
# Create the Gradio Blocks interface
|
58 |
+
with gr.Blocks(css=CSS) as demo:
|
59 |
+
gr.HTML(TITLE)
|
60 |
+
with gr.Tab("Chat"):
|
61 |
+
chatbot = gr.Chatbot() # Create a chatbot interface
|
62 |
+
chat_interface = gr.ChatInterface(
|
63 |
+
fn=stream_chat_with_rag,
|
64 |
+
chatbot=chatbot,
|
65 |
+
additional_inputs_accordion=gr.Accordion(label="⚙️ Parameters", open=False, render=False),
|
66 |
+
additional_inputs=[
|
67 |
+
gr.Dropdown(client_name,value="2016 Election",label="Select Election year", render=False,allow_custom_value=True)
|
68 |
+
],
|
69 |
+
)
|
70 |
+
|
71 |
+
|
72 |
+
# with gr.Tab("Process PDF"):
|
73 |
+
# pdf_input = gr.File(label="Upload PDF File")
|
74 |
+
# #select_client_dropdown = gr.Dropdown(client_name, value="rosariarossi", label="Select or Type Client", allow_custom_value=True)
|
75 |
+
# pdf_output = gr.Textbox(label="PDF Result", interactive=False)
|
76 |
+
|
77 |
+
# pdf_button = gr.Button("Process PDF")
|
78 |
+
# pdf_button.click(
|
79 |
+
# process_pdf,
|
80 |
+
# inputs=[pdf_input], # Pass both PDF and client name is not required
|
81 |
+
# outputs=pdf_output
|
82 |
+
# )
|
83 |
+
|
84 |
+
# with gr.Tab("Answer with RAG"):
|
85 |
+
# question_input = gr.Textbox(label="Enter Question for RAG")
|
86 |
+
# answer_with_rag_select_client_dropdown = gr.Dropdown(client_name, value="primo", label="Select or Type Client", allow_custom_value=True)
|
87 |
+
# rag_output = gr.Textbox(label="RAG Answer Result", interactive=False)
|
88 |
+
|
89 |
+
# rag_button = gr.Button("Get Answer")
|
90 |
+
# rag_button.click(
|
91 |
+
# rag_api,
|
92 |
+
# inputs=[question_input,answer_with_rag_select_client_dropdown ],
|
93 |
+
# outputs=rag_output
|
94 |
+
# )
|
95 |
+
# with gr.Tab(label="Manage Files"):
|
96 |
+
# with gr.Column():
|
97 |
+
# delete_index_button = gr.Button("Delete All Files")
|
98 |
+
# delete_index_textout = gr.Textbox(label="Deleted Files and Refresh Result")
|
99 |
+
# delete_index_button.click(fn=delete_index, inputs=[],outputs=[delete_index_textout])
|
100 |
+
|
101 |
+
# Launch the app
|
102 |
+
if __name__ == "__main__":
|
103 |
+
demo.launch()
|
104 |
+
|
105 |
+
|
106 |
+
|