File size: 4,935 Bytes
71736e8
f148d8c
5d557f1
f148d8c
 
 
 
 
71736e8
5d557f1
f148d8c
8845a3f
 
 
 
 
 
 
f148d8c
 
e6d4d36
 
 
 
 
 
 
 
 
f148d8c
 
 
 
 
 
 
 
 
5d557f1
f148d8c
 
 
 
5d557f1
f148d8c
e6d4d36
f148d8c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e6d4d36
f148d8c
 
 
 
 
e6d4d36
 
f148d8c
 
 
 
 
 
 
 
5d557f1
168235c
 
 
 
 
9e052e8
168235c
 
 
 
 
 
 
fd868fe
71736e8
 
 
 
9e991a9
5d557f1
 
f148d8c
5d557f1
 
 
 
168235c
5d557f1
168235c
5d557f1
 
71736e8
168235c
 
 
71736e8
 
5d557f1
168235c
 
5d557f1
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
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("""
    <button onclick="copyToClipboard('share_link')">In die Zwischenablage kopieren</button>
    """)

    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)