Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -2,19 +2,16 @@ import os
|
|
2 |
import openai
|
3 |
import gradio as gr
|
4 |
import time
|
5 |
-
import pandas as pd
|
6 |
-
import json
|
7 |
from jtar import jtar_txt
|
8 |
|
9 |
# Set up password
|
10 |
username = os.getenv('username')
|
11 |
password = os.getenv('password')
|
12 |
|
13 |
-
openai.api_key = os.getenv('OPENAI_API_KEY')
|
14 |
-
openai_api_key = openai.api_key
|
15 |
-
|
16 |
# Set up OpenAI client
|
17 |
-
|
|
|
|
|
18 |
|
19 |
# Set up assistant
|
20 |
assistant_id = os.getenv('assistant_id')
|
@@ -24,27 +21,20 @@ thread = client.beta.threads.create()
|
|
24 |
# Get avatar link
|
25 |
avatar_link = os.getenv('avatar_link')
|
26 |
|
27 |
-
# Load and prepare the DataFrame
|
28 |
-
df = pd.read_json('/content/IB May 2024 Results for Assistant.txt')
|
29 |
-
df_json = df.to_json(orient='records')
|
30 |
-
|
31 |
def chat_with_assistant(message, history):
|
32 |
-
#
|
33 |
-
combined_message = f"{message}\n\nHere's the DataFrame in JSON format:\n{df_json}"
|
34 |
-
|
35 |
-
# Add the combined message to the thread
|
36 |
client.beta.threads.messages.create(
|
37 |
thread_id=thread.id,
|
38 |
role="user",
|
39 |
-
content=
|
40 |
)
|
41 |
-
|
42 |
# Run the assistant
|
43 |
run = client.beta.threads.runs.create(
|
44 |
thread_id=thread.id,
|
45 |
assistant_id=assistant_id
|
46 |
)
|
47 |
-
|
48 |
# Wait for the assistant's response
|
49 |
while True:
|
50 |
run_status = client.beta.threads.runs.retrieve(thread_id=thread.id, run_id=run.id)
|
@@ -54,7 +44,7 @@ def chat_with_assistant(message, history):
|
|
54 |
assistant_response = messages.data[0].content[0].text.value
|
55 |
break
|
56 |
time.sleep(1)
|
57 |
-
|
58 |
return assistant_response
|
59 |
|
60 |
# Custom CSS for chat bubbles and colors
|
@@ -65,8 +55,8 @@ custom_css = """
|
|
65 |
|
66 |
# Create the Gradio interface
|
67 |
with gr.Blocks(css=custom_css) as demo:
|
68 |
-
gr.HTML(f"""<img src="
|
69 |
-
gr.Markdown("# **IB Results Analyst
|
70 |
chatbot = gr.Chatbot(
|
71 |
[],
|
72 |
elem_id="chatbot",
|
@@ -90,7 +80,6 @@ with gr.Blocks(css=custom_css) as demo:
|
|
90 |
msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
|
91 |
bot, chatbot, chatbot
|
92 |
)
|
93 |
-
|
94 |
gr.Markdown(jtar_txt)
|
95 |
|
96 |
-
demo.launch(auth=(username, password))
|
|
|
2 |
import openai
|
3 |
import gradio as gr
|
4 |
import time
|
|
|
|
|
5 |
from jtar import jtar_txt
|
6 |
|
7 |
# Set up password
|
8 |
username = os.getenv('username')
|
9 |
password = os.getenv('password')
|
10 |
|
|
|
|
|
|
|
11 |
# Set up OpenAI client
|
12 |
+
openai_api_key = os.getenv('openai_api_key')
|
13 |
+
openai.api_key = openai_api_key
|
14 |
+
client = openai.Client(api_key=openai.api_key)
|
15 |
|
16 |
# Set up assistant
|
17 |
assistant_id = os.getenv('assistant_id')
|
|
|
21 |
# Get avatar link
|
22 |
avatar_link = os.getenv('avatar_link')
|
23 |
|
|
|
|
|
|
|
|
|
24 |
def chat_with_assistant(message, history):
|
25 |
+
# Add the user's message to the thread
|
|
|
|
|
|
|
26 |
client.beta.threads.messages.create(
|
27 |
thread_id=thread.id,
|
28 |
role="user",
|
29 |
+
content=message
|
30 |
)
|
31 |
+
|
32 |
# Run the assistant
|
33 |
run = client.beta.threads.runs.create(
|
34 |
thread_id=thread.id,
|
35 |
assistant_id=assistant_id
|
36 |
)
|
37 |
+
|
38 |
# Wait for the assistant's response
|
39 |
while True:
|
40 |
run_status = client.beta.threads.runs.retrieve(thread_id=thread.id, run_id=run.id)
|
|
|
44 |
assistant_response = messages.data[0].content[0].text.value
|
45 |
break
|
46 |
time.sleep(1)
|
47 |
+
|
48 |
return assistant_response
|
49 |
|
50 |
# Custom CSS for chat bubbles and colors
|
|
|
55 |
|
56 |
# Create the Gradio interface
|
57 |
with gr.Blocks(css=custom_css) as demo:
|
58 |
+
gr.HTML(f"""<img src = "https://i.postimg.cc/43kfn8Xx/a-charming-pixar-style-3d-render-of-a-dolphin-in-a-j-GGid5px-R86-Wo-FLn-Re27d-Q-w9-Mz-RVy8-Qti-PQ2-Irspua-LQ.png" width="320" height="240">""")
|
59 |
+
gr.Markdown("# **IB Results Analyst** 📊")
|
60 |
chatbot = gr.Chatbot(
|
61 |
[],
|
62 |
elem_id="chatbot",
|
|
|
80 |
msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
|
81 |
bot, chatbot, chatbot
|
82 |
)
|
|
|
83 |
gr.Markdown(jtar_txt)
|
84 |
|
85 |
+
demo.launch(auth=(username, password))"
|