Spaces:
Build error
Build error
Tobias Geisler commited on
Commit ·
f148d8c
1
Parent(s): 9e052e8
add chatbot loading logging and timeout
Browse files- tabs/chat.py +54 -13
tabs/chat.py
CHANGED
|
@@ -1,21 +1,62 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
from database import get_chatbot
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
def load_chatbot(chatbot_id):
|
| 5 |
-
chatbot
|
| 6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
return (
|
| 8 |
-
gr.update(value=
|
| 9 |
-
gr.update(value=
|
| 10 |
-
gr.update(value=
|
| 11 |
-
gr.update(value="Chatbot
|
| 12 |
)
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
def create_chat_tab():
|
| 21 |
chatbot_id_input = gr.Textbox(label="Chatbot-ID eingeben")
|
|
@@ -38,7 +79,7 @@ def create_chat_tab():
|
|
| 38 |
share_link = gr.Textbox(label="Link zum Teilen", interactive=False, elem_id="share_link")
|
| 39 |
|
| 40 |
load_button.click(
|
| 41 |
-
fn=
|
| 42 |
inputs=[chatbot_id_input],
|
| 43 |
outputs=[chatbot_title, chatbot_id_input, share_link, load_message]
|
| 44 |
)
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import logging
|
| 3 |
from database import get_chatbot
|
| 4 |
+
import concurrent.futures
|
| 5 |
+
|
| 6 |
+
# Setup logging
|
| 7 |
+
logging.basicConfig(level=logging.INFO)
|
| 8 |
+
logger = logging.getLogger(__name__)
|
| 9 |
|
| 10 |
def load_chatbot(chatbot_id):
|
| 11 |
+
logger.info(f"Attempting to load chatbot with ID: {chatbot_id}")
|
| 12 |
+
try:
|
| 13 |
+
chatbot = get_chatbot(chatbot_id)
|
| 14 |
+
if chatbot:
|
| 15 |
+
logger.info(f"Chatbot {chatbot_id} loaded successfully")
|
| 16 |
+
return (
|
| 17 |
+
gr.update(value=f"# {chatbot.name}\n\nWillkommen bei {chatbot.name}"),
|
| 18 |
+
gr.update(value=chatbot_id, visible=True),
|
| 19 |
+
gr.update(value=f"https://huggingface.co/spaces/codora/ai-app-creator?chatbot_id={chatbot_id}"),
|
| 20 |
+
gr.update(value="Chatbot erfolgreich geladen.")
|
| 21 |
+
)
|
| 22 |
+
logger.warning(f"Chatbot {chatbot_id} not found or inactive")
|
| 23 |
return (
|
| 24 |
+
gr.update(value="# Chatbot\n\nWillkommen beim Chatbot"),
|
| 25 |
+
gr.update(value="", visible=True),
|
| 26 |
+
gr.update(value=""),
|
| 27 |
+
gr.update(value="Ungültige Chatbot-ID oder Chatbot nicht aktiv.")
|
| 28 |
)
|
| 29 |
+
except Exception as e:
|
| 30 |
+
logger.error(f"Error loading chatbot {chatbot_id}: {str(e)}")
|
| 31 |
+
return (
|
| 32 |
+
gr.update(value="# Chatbot\n\nWillkommen beim Chatbot"),
|
| 33 |
+
gr.update(value="", visible=True),
|
| 34 |
+
gr.update(value=""),
|
| 35 |
+
gr.update(value=f"Ein Fehler ist aufgetreten: {str(e)}")
|
| 36 |
+
)
|
| 37 |
+
|
| 38 |
+
def load_chatbot_with_timeout(chatbot_id, timeout=5):
|
| 39 |
+
with concurrent.futures.ThreadPoolExecutor() as executor:
|
| 40 |
+
future = executor.submit(load_chatbot, chatbot_id)
|
| 41 |
+
try:
|
| 42 |
+
result = future.result(timeout=timeout)
|
| 43 |
+
return result
|
| 44 |
+
except concurrent.futures.TimeoutError:
|
| 45 |
+
logger.error(f"Timeout while loading chatbot {chatbot_id}")
|
| 46 |
+
return (
|
| 47 |
+
gr.update(value="# Chatbot\n\nWillkommen beim Chatbot"),
|
| 48 |
+
gr.update(value="", visible=True),
|
| 49 |
+
gr.update(value=""),
|
| 50 |
+
gr.update(value="Zeitüberschreitung beim Laden des Chatbots.")
|
| 51 |
+
)
|
| 52 |
+
except Exception as e:
|
| 53 |
+
logger.error(f"Unexpected error while loading chatbot {chatbot_id}: {str(e)}")
|
| 54 |
+
return (
|
| 55 |
+
gr.update(value="# Chatbot\n\nWillkommen beim Chatbot"),
|
| 56 |
+
gr.update(value="", visible=True),
|
| 57 |
+
gr.update(value=""),
|
| 58 |
+
gr.update(value=f"Ein unerwarteter Fehler ist aufgetreten: {str(e)}")
|
| 59 |
+
)
|
| 60 |
|
| 61 |
def create_chat_tab():
|
| 62 |
chatbot_id_input = gr.Textbox(label="Chatbot-ID eingeben")
|
|
|
|
| 79 |
share_link = gr.Textbox(label="Link zum Teilen", interactive=False, elem_id="share_link")
|
| 80 |
|
| 81 |
load_button.click(
|
| 82 |
+
fn=load_chatbot_with_timeout,
|
| 83 |
inputs=[chatbot_id_input],
|
| 84 |
outputs=[chatbot_title, chatbot_id_input, share_link, load_message]
|
| 85 |
)
|