Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -2,6 +2,8 @@ import os
|
|
2 |
import openai
|
3 |
import gradio as gr
|
4 |
import time
|
|
|
|
|
5 |
from jtar import jtar_txt
|
6 |
|
7 |
# Set up password
|
@@ -9,7 +11,7 @@ username = os.getenv('username')
|
|
9 |
password = os.getenv('password')
|
10 |
|
11 |
# Set up OpenAI client
|
12 |
-
openai_api_key = os.getenv('
|
13 |
openai.api_key = openai_api_key
|
14 |
client = openai.Client(api_key=openai.api_key)
|
15 |
|
@@ -21,38 +23,37 @@ thread = client.beta.threads.create()
|
|
21 |
# Get avatar link
|
22 |
avatar_link = os.getenv('avatar_link')
|
23 |
|
|
|
|
|
|
|
|
|
24 |
def chat_with_assistant(message, history):
|
25 |
-
#
|
|
|
|
|
|
|
26 |
client.beta.threads.messages.create(
|
27 |
thread_id=thread.id,
|
28 |
role="user",
|
29 |
-
content=
|
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)
|
41 |
if run_status.status == 'completed':
|
42 |
# Retrieve the assistant's response
|
43 |
messages = client.beta.threads.messages.list(thread_id=thread.id)
|
44 |
-
|
45 |
-
|
46 |
-
# Check if the response is text or an image
|
47 |
-
if hasattr(content_block, 'text'):
|
48 |
-
assistant_response = content_block.text.value
|
49 |
-
elif hasattr(content_block, 'image_file'):
|
50 |
-
assistant_response = f"<img src='{content_block.image_file.url}' alt='Image' />"
|
51 |
-
else:
|
52 |
-
assistant_response = "[Unknown content type]"
|
53 |
break
|
54 |
time.sleep(1)
|
55 |
-
|
56 |
return assistant_response
|
57 |
|
58 |
# Custom CSS for chat bubbles and colors
|
@@ -63,8 +64,8 @@ custom_css = """
|
|
63 |
|
64 |
# Create the Gradio interface
|
65 |
with gr.Blocks(css=custom_css) as demo:
|
66 |
-
gr.HTML(f"""<img src
|
67 |
-
gr.Markdown("# **IB Results Analyst
|
68 |
chatbot = gr.Chatbot(
|
69 |
[],
|
70 |
elem_id="chatbot",
|
@@ -88,6 +89,7 @@ with gr.Blocks(css=custom_css) as demo:
|
|
88 |
msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
|
89 |
bot, chatbot, chatbot
|
90 |
)
|
|
|
91 |
gr.Markdown(jtar_txt)
|
92 |
|
93 |
demo.launch(auth=(username, password))
|
|
|
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
|
|
|
11 |
password = os.getenv('password')
|
12 |
|
13 |
# Set up OpenAI client
|
14 |
+
openai_api_key = os.getenv('OPENAI_API_KEY')
|
15 |
openai.api_key = openai_api_key
|
16 |
client = openai.Client(api_key=openai.api_key)
|
17 |
|
|
|
23 |
# Get avatar link
|
24 |
avatar_link = os.getenv('avatar_link')
|
25 |
|
26 |
+
# Load and prepare the DataFrame
|
27 |
+
df = pd.read_json('/content/IB May 2024 Results for Assistant.txt')
|
28 |
+
df_json = df.to_json(orient='records')
|
29 |
+
|
30 |
def chat_with_assistant(message, history):
|
31 |
+
# Combine the user's message with the DataFrame
|
32 |
+
combined_message = f"{message}\n\nHere's the DataFrame in JSON format:\n{df_json}"
|
33 |
+
|
34 |
+
# Add the combined message to the thread
|
35 |
client.beta.threads.messages.create(
|
36 |
thread_id=thread.id,
|
37 |
role="user",
|
38 |
+
content=combined_message
|
39 |
)
|
40 |
+
|
41 |
# Run the assistant
|
42 |
run = client.beta.threads.runs.create(
|
43 |
thread_id=thread.id,
|
44 |
assistant_id=assistant_id
|
45 |
)
|
46 |
+
|
47 |
# Wait for the assistant's response
|
48 |
while True:
|
49 |
run_status = client.beta.threads.runs.retrieve(thread_id=thread.id, run_id=run.id)
|
50 |
if run_status.status == 'completed':
|
51 |
# Retrieve the assistant's response
|
52 |
messages = client.beta.threads.messages.list(thread_id=thread.id)
|
53 |
+
assistant_response = messages.data[0].content[0].text.value
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
54 |
break
|
55 |
time.sleep(1)
|
56 |
+
|
57 |
return assistant_response
|
58 |
|
59 |
# Custom CSS for chat bubbles and colors
|
|
|
64 |
|
65 |
# Create the Gradio interface
|
66 |
with gr.Blocks(css=custom_css) as demo:
|
67 |
+
gr.HTML(f"""<img src="{avatar_link}" width="320" height="240">""")
|
68 |
+
gr.Markdown("# **IB Results Analyst**📊")
|
69 |
chatbot = gr.Chatbot(
|
70 |
[],
|
71 |
elem_id="chatbot",
|
|
|
89 |
msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
|
90 |
bot, chatbot, chatbot
|
91 |
)
|
92 |
+
|
93 |
gr.Markdown(jtar_txt)
|
94 |
|
95 |
demo.launch(auth=(username, password))
|