File size: 1,946 Bytes
ca563f9
 
96742cc
8083432
96742cc
ca563f9
7dd6cdd
 
cdf5103
bb6b522
c647947
 
cdf5103
bb6b522
 
7dd6cdd
bb6b522
 
 
 
 
 
 
 
 
 
c647947
bb6b522
 
 
 
e1e2259
c647947
06173fd
c647947
 
06173fd
c647947
06173fd
c647947
06173fd
 
 
 
bb6b522
06173fd
bb6b522
06173fd
 
 
96742cc
 
 
 
 
43021b3
 
96742cc
d4d2a93
43021b3
 
e1e2259
96742cc
 
c647947
 
e1e2259
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
# Imports

import gradio as gr
from agent import agent


# UI

with gr.Blocks() as demo:

    # Centered title and description using HTML
    gr.HTML("""
        <style>footer {display: none !important;} .flex.gap-4 a {display: none !important;}</style>
        <div style="text-align: center; font-family: 'Arial', sans-serif;">
            <h1 style="color:#1f77b4; margin-bottom: 20px; font-weight: 300;">
                🔧 TechSpark AI Assistant 🤖
            </h1>

            <p style="margin-top: 0; font-weight: 300; font-size: 16px; color:#555;">
                Welcome to the TechSpark AI Assistant!<br>
                Ask anything about TechSpark staff, tools, courses or location of tools.<br>
            </p>
        </div>
    """)

    # Chatbot: no `type=` arg, but it STILL uses "messages" format internally
    chat = gr.Chatbot(height=420)
    inp = gr.Textbox(
        placeholder="Ask your question in natural language.",
        label="Your question"
    )

    def respond(message, history):
        # history here is a list of {"role", "content"} dicts (messages format)
        if history is None:
            history = []

        try:
            out = str(agent.run(message, reset=False))
        except Exception as e:
            out = f"[Error] {e}"

        # APPEND MESSAGES AS DICTS, NOT LISTS/TUPLES
        history = history + [
            {"role": "user", "content": message},
            {"role": "assistant", "content": out},
        ]

        # Clear input, update chat
        return "", history

    gr.Examples(
        examples=[
            "Who is Ed?",
            "Who to talk to to create a wooden table?",
            "How to access the laser cutters?",
            "How to go to the welding space?",
        ],
        inputs=[inp],
        outputs=[inp, chat],
        fn=respond,
        cache_examples=False,
    )

    inp.submit(respond, [inp, chat], [inp, chat])

demo.launch()