File size: 2,422 Bytes
5c5a9cf
 
 
a4a652b
 
b828fac
57818b4
18cab23
57818b4
 
9825ea7
507880e
21ec701
9825ea7
b828fac
9825ea7
5c5a9cf
 
 
b828fac
9825ea7
 
 
5c5a9cf
 
 
 
 
 
 
a4a652b
 
 
 
 
5c5a9cf
 
a4a652b
 
 
 
 
 
 
 
 
 
 
b828fac
5c5a9cf
 
 
 
 
b828fac
5c5a9cf
 
a4a652b
66a8c8f
5c5a9cf
 
 
 
c237974
5c5a9cf
 
 
 
 
b828fac
5c5a9cf
 
b828fac
5c5a9cf
 
a4a652b
 
5c5a9cf
 
 
 
 
a4a652b
5c5a9cf
e10a598
a4a652b
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
import os
import openai
import gradio as gr
import time
from ftr import warn

# Set up password
username = os.getenv('username')
password = os.getenv('password')

# Set up OpenAI client
openai_api_key = os.getenv('openai_api_key')
openai.api_key = openai_api_key
client = openai.Client(api_key=openai.api_key)

# Set up assistant
assistant_id = os.getenv('assistant_id')
assistant = client.beta.assistants.retrieve(assistant_id)
thread = client.beta.threads.create()

# Get avatar link
avatar_link = os.getenv('avatar_link')

def chat_with_assistant(message, history):
    # Add the user's message to the thread
    client.beta.threads.messages.create(
        thread_id=thread.id,
        role="user",
        content=message
    )
    
    # Run the assistant
    run = client.beta.threads.runs.create(
        thread_id=thread.id,
        assistant_id=assistant_id
    )
    
    # Wait for the assistant's response
    while True:
        run_status = client.beta.threads.runs.retrieve(thread_id=thread.id, run_id=run.id)
        if run_status.status == 'completed':
            # Retrieve the assistant's response
            messages = client.beta.threads.messages.list(thread_id=thread.id)
            assistant_response = messages.data[0].content[0].text.value
            break
        time.sleep(1)
    
    return assistant_response

# Custom CSS for chat bubbles and colors
custom_css = """
.user-message { background-color: #DCF8C6; }
.assistant-message { background-color: #E2E2E2; }
"""

# Create the Gradio interface
with gr.Blocks(css=custom_css) as demo:
    gr.HTML(f"""<img src="https://i.postimg.cc/L6mbcC7F/DASH.png" width="150" height="112">""")
    gr.Markdown("# **Learning Scales Assistant**🐬")
    chatbot = gr.Chatbot(
        [],
        elem_id="chatbot",
        bubble_full_width=False,
        avatar_images=(None, avatar_link)
    )
    msg = gr.Textbox(
        show_label=False,
        placeholder="Enter text and press enter",
    )

    def user(user_message, history):
        return "", history + [[user_message, None]]

    def bot(history):
        user_message = history[-1][0]
        bot_message = chat_with_assistant(user_message, history)
        history[-1][1] = bot_message
        return history

    msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
        bot, chatbot, chatbot
    )
    gr.Markdown(warn)

demo.launch(auth=(username, password))