File size: 3,586 Bytes
96911b6 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
import gradio as gr
from gradio_client import Client, handle_file
import os
# Define your Hugging Face token (make sure to set it as an environment variable)
HF_TOKEN = os.getenv("HF_TOKEN") # Replace with your actual token if not using an environment variable
# Initialize the Gradio Client for the specified API
client = Client("mangoesai/Elections_Comparing_Agent_V2", hf_token=HF_TOKEN)
client_name = ['2016 Election','2024 Election', 'Comparison two years']
def stream_chat_with_rag(
message: str,
history: list,
client_name: str
):
print(f"Message: {message}")
print(f"History: {history}")
# Build the conversation prompt including system prompt and history
conversation = f"{system_prompt}\n\nFor Client: {client_name}\n"
# Add previous conversation history
for user_input, assistant_response in history:
conversation += f"User: {user_input}\nAssistant: {assistant_response}\n"
# Add the current user message
conversation += f"User: {message}\nAssistant:"
# Call the API with the user's process_query
question = message
#answer = client.predict(question=question, api_name="/run_graph")
answer = client.predict(
query= message,
election_year=client_name,
api_name="/process_query"
)
# Debugging: Print the raw response
print("Raw answer from API:")
print(answer)
return answer
# Title for the application
TITLE = "<h1 style='text-align:center;'>Reddit Election Q&A agent v0.1</h1>"
# Create the Gradio Blocks interface
with gr.Blocks(css=CSS) as demo:
gr.HTML(TITLE)
with gr.Tab("Chat"):
chatbot = gr.Chatbot() # Create a chatbot interface
chat_interface = gr.ChatInterface(
fn=stream_chat_with_rag,
chatbot=chatbot,
additional_inputs_accordion=gr.Accordion(label="⚙️ Parameters", open=False, render=False),
additional_inputs=[
gr.Dropdown(client_name,value="2016 Election",label="Select Election year", render=False,allow_custom_value=True)
],
)
# with gr.Tab("Process PDF"):
# pdf_input = gr.File(label="Upload PDF File")
# #select_client_dropdown = gr.Dropdown(client_name, value="rosariarossi", label="Select or Type Client", allow_custom_value=True)
# pdf_output = gr.Textbox(label="PDF Result", interactive=False)
# pdf_button = gr.Button("Process PDF")
# pdf_button.click(
# process_pdf,
# inputs=[pdf_input], # Pass both PDF and client name is not required
# outputs=pdf_output
# )
# with gr.Tab("Answer with RAG"):
# question_input = gr.Textbox(label="Enter Question for RAG")
# answer_with_rag_select_client_dropdown = gr.Dropdown(client_name, value="primo", label="Select or Type Client", allow_custom_value=True)
# rag_output = gr.Textbox(label="RAG Answer Result", interactive=False)
# rag_button = gr.Button("Get Answer")
# rag_button.click(
# rag_api,
# inputs=[question_input,answer_with_rag_select_client_dropdown ],
# outputs=rag_output
# )
# with gr.Tab(label="Manage Files"):
# with gr.Column():
# delete_index_button = gr.Button("Delete All Files")
# delete_index_textout = gr.Textbox(label="Deleted Files and Refresh Result")
# delete_index_button.click(fn=delete_index, inputs=[],outputs=[delete_index_textout])
# Launch the app
if __name__ == "__main__":
demo.launch()
|