Spaces:
Runtime error
Runtime error
Update app.py
Browse files22 unmodified lines23242526272835 unmodified lines646566676869707172737475767778798081828384852 unmodified lines8889909192939499 unmodified lines19419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725822 unmodified lines"mistralai/Mixtral-8x7B-Instruct-v0.1","mistralai/Mistral-Nemo-Instruct-2407","meta-llama/Meta-Llama-3.1-8B-Instruct","meta-llama/Meta-Llama-3.1-70B-Instruct"]35 unmodified linesfor response in respond(message, history, model, temperature, num_calls, use_embeddings, system_prompt):history[-1] = (message, response)yield historyexcept gr.CancelledError:yield historyexcept Exception as e:logging.error(f"Unexpected error in chatbot_interface: {str(e)}")history[-1] = (message, f"An unexpected error occurred: {str(e)}")yield historydef retry_last_response(history, model, temperature, num_calls, use_embeddings, system_prompt):if not history:return historylast_user_msg = history[-1][0]history = history[:-1] # Remove the last responsereturn chatbot_interface(last_user_msg, history, model, temperature, num_calls, use_embeddings, system_prompt)def respond(message, history, model, temperature, num_calls, use_embeddings, system_prompt):logging.info(f"User Query: {message}")logging.info(f"Model Used: {model}")2 unmodified linestry:for main_content, sources in get_response_with_search(message, model, num_calls, temperature, use_embeddings, system_prompt):response = f"{main_content}\n\n{sources}"first_line = response.split('\n')[0] if response else ''yield responseexcept Exception as e:logging.error(f"Error with {model}: {str(e)}")99 unmodified lines"To get started, ask me a question!")]with gr.Blocks(css=css, theme=gr.themes.Soft(primary_hue="orange",secondary_hue="amber",neutral_hue="gray",font=[gr.themes.GoogleFont("Exo"), "ui-sans-serif", "system-ui", "sans-serif"]).set(body_background_fill_dark="#0c0505",block_background_fill_dark="#0c0505",block_border_width="1px",block_title_background_fill_dark="#1b0f0f",input_background_fill_dark="#140b0b",button_secondary_background_fill_dark="#140b0b",border_color_accent_dark="#1b0f0f",border_color_primary_dark="#1b0f0f",background_fill_secondary_dark="#0c0505",color_accent_soft_dark="transparent",code_background_fill_dark="#140b0b")) as demo:gr.Markdown("# AI-powered Web Search Assistant")gr.Markdown("Ask questions and get answers from web search results.")chatbot = gr.Chatbot(show_copy_button=True,likeable=True,layout="bubble",height=400,value=initial_conversation())with gr.Row():msg = gr.Textbox(placeholder="Ask a question", container=False, scale=7)submit = gr.Button("Submit")with gr.Accordion("Advanced Options", open=False):model = gr.Dropdown(choices=MODELS, label="Select Model", value=MODELS[2])temperature = gr.Slider(minimum=0.1, maximum=1.0, value=0.2, step=0.1, label="Temperature")num_calls = gr.Slider(minimum=1, maximum=5, value=1, step=1, label="Number of API Calls")use_embeddings = gr.Checkbox(label="Use Embeddings", value=True)system_prompt = gr.Textbox(label="System Prompt", value=DEFAULT_SYSTEM_PROMPT, lines=5)clear = gr.Button("Clear")retry = gr.Button("Retry Last Response")# Set up event handlerssubmit.click(chatbot_interface, [msg, chatbot, model, temperature, num_calls, use_embeddings, system_prompt], chatbot)msg.submit(chatbot_interface, [msg, chatbot, model, temperature, num_calls, use_embeddings, system_prompt], chatbot)clear.click(lambda: None, None, chatbot, queue=False)retry.click(retry_last_response, [chatbot, model, temperature, num_calls, use_embeddings, system_prompt], chatbot)chatbot.like(vote, None, None)gr.Examples(examples=[["What are the latest developments in artificial intelligence?"],["Can you explain the basics of quantum computing?"],["What are the current global economic trends?"]],inputs=msg)if __name__ == "__main__":demo.launch(share=True)22 unmodified lines232425262728293035 unmodified lines66676869707172737475762 unmodified lines79808182838499 unmodified lines18418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423522 unmodified lines"mistralai/Mixtral-8x7B-Instruct-v0.1","mistralai/Mistral-Nemo-Instruct-2407","meta-llama/Meta-Llama-3.1-8B-Instruct","meta-llama/Meta-Llama-3.1-70B-Instruct","meta-llama/Meta-Llama-3.1-8B-Instruct","meta-llama/Meta-Llama-3.1-70B-Instruct"]35 unmodified linesfor response in respond(message, history, model, temperature, num_calls, use_embeddings, system_prompt):history[-1] = (message, response)yield historyexcept Exception as e:logging.error(f"Unexpected error in chatbot_interface: {str(e)}")history[-1] = (message, f"An unexpected error occurred: {str(e)}")yield historydef respond(message, history, model, temperature, num_calls, use_embeddings, system_prompt):logging.info(f"User Query: {message}")logging.info(f"Model Used: {model}")2 unmodified linestry:for main_content, sources in get_response_with_search(message, model, num_calls, temperature, use_embeddings, system_prompt):response = f"{main_content}\n\n{sources}"yield responseexcept Exception as e:logging.error(f"Error with {model}: {str(e)}")99 unmodified lines"To get started, ask me a question!")]demo = gr.ChatInterface(chatbot_interface,additional_inputs=[gr.Dropdown(choices=MODELS, label="Select Model", value=MODELS[2]),gr.Slider(minimum=0.1, maximum=1.0, value=0.2, step=0.1, label="Temperature"),gr.Slider(minimum=1, maximum=5, value=1, step=1, label="Number of API Calls"),gr.Checkbox(label="Use Embeddings", value=True),gr.Textbox(label="System Prompt", value=DEFAULT_SYSTEM_PROMPT, lines=5),],title="AI-powered Web Search Assistant",description="Ask questions and get answers from web search results.",theme=gr.themes.Soft(primary_hue="orange",secondary_hue="amber",neutral_hue="gray",font=[gr.themes.GoogleFont("Exo"), "ui-sans-serif", "system-ui", "sans-serif"]).set(body_background_fill_dark="#0c0505",block_background_fill_dark="#0c0505",block_border_width="1px",block_title_background_fill_dark="#1b0f0f",input_background_fill_dark="#140b0b",button_secondary_background_fill_dark="#140b0b",border_color_accent_dark="#1b0f0f",border_color_primary_dark="#1b0f0f",background_fill_secondary_dark="#0c0505",color_accent_soft_dark="transparent",code_background_fill_dark="#140b0b"),css=css,examples=[["What are the latest developments in artificial intelligence?"],["Can you explain the basics of quantum computing?"],["What are the current global economic trends?"]],cache_examples=False,analytics_enabled=False,textbox=gr.Textbox(placeholder="Ask a question", container=False, scale=7),chatbot = gr.Chatbot(show_copy_button=True,likeable=True,layout="bubble",height=400,value=initial_conversation()))if __name__ == "__main__":demo.launch(share=True)
app.py+42-65