import gradio as gr import logging from database import get_chatbot import concurrent.futures # Setup logging logging.basicConfig(level=logging.INFO) logger = logging.getLogger(__name__) def load_chatbot(chatbot_id): logger.info(f"Attempting to load chatbot with ID: {chatbot_id}") if chatbot_id is None or chatbot_id == "": return ( gr.update(value="# Chatbot\n\nWillkommen beim Chatbot"), gr.update(value="", visible=True), gr.update(value=""), gr.update(value="Bitte gib eine Chatbot-ID ein.") ) try: chatbot = get_chatbot(chatbot_id) except Exception as e: logger.error(f"Error loading chatbot {chatbot_id}: {str(e)}") return ( gr.update(value="# Chatbot\n\nWillkommen beim Chatbot"), gr.update(value="", visible=True), gr.update(value=""), gr.update(value=f"Ein Fehler ist aufgetreten: {str(e)}") ) try: if chatbot: logger.info(f"Chatbot {chatbot_id} loaded successfully") return ( gr.update(value=f"# {chatbot.name}\n\nWillkommen bei {chatbot.name}"), gr.update(value=chatbot_id, visible=True), gr.update(value=f"https://huggingface.co/spaces/codora/ai-app-creator?chatbot_id={chatbot_id}"), gr.update(value="Chatbot erfolgreich geladen.") ) logger.warning(f"Chatbot {chatbot_id} not found or inactive") return ( gr.update(value="# Chatbot\n\nWillkommen beim Chatbot"), gr.update(value="", visible=True), gr.update(value=""), gr.update(value="Ungültige Chatbot-ID oder Chatbot nicht aktiv.") ) except Exception as e: logger.error(f"Error filling chatbot fields for {chatbot_id}: {str(e)}") return ( gr.update(value="# Chatbot\n\nWillkommen beim Chatbot"), gr.update(value="", visible=True), gr.update(value=""), gr.update(value=f"Ein Fehler ist aufgetreten: {str(e)}") ) def load_chatbot_with_timeout(chatbot_id, timeout=5): with concurrent.futures.ThreadPoolExecutor() as executor: future = executor.submit(load_chatbot, chatbot_id) try: result = future.result(timeout=timeout) return result except concurrent.futures.TimeoutError: logger.error(f"Timeout while loading chatbot {chatbot_id}") result = ( gr.update(value="# Chatbot\n\nWillkommen beim Chatbot"), gr.update(value="", visible=True), gr.update(value=""), gr.update(value="Zeitüberschreitung beim Laden des Chatbots.") ) gr.refresh() # Force UI refresh return result except Exception as e: logger.error(f"Unexpected error while loading chatbot {chatbot_id}: {str(e)}") return ( gr.update(value="# Chatbot\n\nWillkommen beim Chatbot"), gr.update(value="", visible=True), gr.update(value=""), gr.update(value=f"Ein unerwarteter Fehler ist aufgetreten: {str(e)}") ) def create_chat_tab(): chatbot_id_input = gr.Textbox(label="Chatbot-ID eingeben") # Add examples right below the chatbot ID input field examples = gr.Examples( examples=["courteous-duckling-628", "delightful-red-panda-273", "glowing-toucan-982", "mellow-toucan-512", "upbeat-elephant-433", "shiny-platypus-699"], inputs=chatbot_id_input, label="Beispiel-Chatbots" ) load_button = gr.Button("Chatbot laden") load_message = gr.Textbox(label="Lademeldung", interactive=False) chatbot_title = gr.Markdown("# Chatbot\n\nWillkommen beim Chatbot") chat_interface = gr.ChatInterface( chat_with_bot, additional_inputs=[chatbot_id_input], ) share_link = gr.Textbox(label="Link zum Teilen", interactive=False, elem_id="share_link") load_button.click( fn=load_chatbot_with_timeout, inputs=[chatbot_id_input], outputs=[chatbot_title, chatbot_id_input, share_link, load_message] ) # Add a custom HTML button for copying the share URL to the clipboard copy_button = gr.HTML(""" """) return { "chatbot_id_input": chatbot_id_input, "load_button": load_button, "load_message": load_message, "title": chatbot_title, "interface": chat_interface, "share_link": share_link, "copy_button": copy_button, "examples": examples } def chat_with_bot(message, history, chatbot_id): from utils.openai_utils import chat_with_bot as openai_chat_with_bot return openai_chat_with_bot(message, history, chatbot_id)