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 = "

Reddit Election Q&A agent v0.1

" # 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()