Update app.py
Browse files22 unmodified lines232425262728293035 unmodified lines666768676869707172737475767778798081827475762 unmodified lines7980819182838499 unmodified lines18418518618718818919019119219319419519619719719819920020120220320420520620720820921021121221321421521621721819819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023122522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425523223323423522 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 lines for response in respond(message, history, model, temperature, num_calls, use_embeddings, system_prompt): history[-1] = (message, response) yield history except gr.CancelledError: yield history except Exception as e: logging.error(f"Unexpected error in chatbot_interface: {str(e)}") history[-1] = (message, f"An unexpected error occurred: {str(e)}") yield history
def retry_last_response(history, model, temperature, num_calls, use_embeddings, system_prompt): if not history: return history last_user_msg = history[-1][0] history = history[:-1] # Remove the last response return 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 lines try: 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 response except 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.",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( 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() )
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 handlers submit.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)app.py+42-65