Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,116 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import openai, os, time
|
3 |
+
|
4 |
+
from openai import OpenAI
|
5 |
+
from utils import show_json
|
6 |
+
|
7 |
+
_client = OpenAI(api_key=os.environ.get("OPENAI_API_KEY"))
|
8 |
+
|
9 |
+
_assistant, _thread = None, None
|
10 |
+
|
11 |
+
def create_assistant(client):
|
12 |
+
assistant = client.beta.assistants.create(
|
13 |
+
name="Math Tutor",
|
14 |
+
instructions="You are a personal math tutor. Answer questions briefly, in a sentence or less.",
|
15 |
+
model="gpt-4-1106-preview",
|
16 |
+
tools=[{"type": "code_interpreter"}],
|
17 |
+
)
|
18 |
+
show_json("assistant", assistant)
|
19 |
+
return assistant
|
20 |
+
|
21 |
+
def create_thread(client):
|
22 |
+
thread = client.beta.threads.create()
|
23 |
+
show_json("thread", thread)
|
24 |
+
return thread
|
25 |
+
|
26 |
+
def create_message(client, thread, msg):
|
27 |
+
message = client.beta.threads.messages.create(
|
28 |
+
role="user",
|
29 |
+
thread_id=thread.id,
|
30 |
+
content=msg,
|
31 |
+
)
|
32 |
+
show_json("message", message)
|
33 |
+
return message
|
34 |
+
|
35 |
+
def create_run(client, assistant, thread):
|
36 |
+
run = client.beta.threads.runs.create(
|
37 |
+
assistant_id=assistant.id,
|
38 |
+
thread_id=thread.id,
|
39 |
+
)
|
40 |
+
show_json("run", run)
|
41 |
+
return run
|
42 |
+
|
43 |
+
def wait_on_run(client, thread, run):
|
44 |
+
while run.status == "queued" or run.status == "in_progress":
|
45 |
+
run = client.beta.threads.runs.retrieve(
|
46 |
+
thread_id=thread.id,
|
47 |
+
run_id=run.id,
|
48 |
+
)
|
49 |
+
time.sleep(0.25)
|
50 |
+
show_json("run", run)
|
51 |
+
return run
|
52 |
+
|
53 |
+
def list_run_steps(client, thread, run):
|
54 |
+
run_steps = client.beta.threads.runs.steps.list(
|
55 |
+
thread_id=thread.id,
|
56 |
+
run_id=run.id,
|
57 |
+
order="asc",
|
58 |
+
)
|
59 |
+
for step in run_steps.data:
|
60 |
+
step_details = step.step_details
|
61 |
+
#print(json.dumps(show_json("step_details", step_details), indent=4))
|
62 |
+
show_json("step_details", step_details)
|
63 |
+
return run_steps
|
64 |
+
|
65 |
+
def list_messages(client, thread):
|
66 |
+
messages = client.beta.threads.messages.list(
|
67 |
+
thread_id=thread.id
|
68 |
+
)
|
69 |
+
show_json("messages", messages)
|
70 |
+
return messages
|
71 |
+
|
72 |
+
def extract_content_values(data):
|
73 |
+
content_values = []
|
74 |
+
for item in data.data:
|
75 |
+
for content in item.content:
|
76 |
+
if content.type == "text":
|
77 |
+
content_values.append(content.text.value)
|
78 |
+
return content_values
|
79 |
+
|
80 |
+
def chat(message, history, openai_api_key):
|
81 |
+
global _client, _assistant, _thread
|
82 |
+
|
83 |
+
if _assistant == None:
|
84 |
+
_assistant = create_assistant(_client)
|
85 |
+
|
86 |
+
if _thread == None:
|
87 |
+
_thread = create_thread(_client)
|
88 |
+
|
89 |
+
create_message(_client, _thread, message)
|
90 |
+
|
91 |
+
# async
|
92 |
+
run = create_run(_client, _assistant, _thread)
|
93 |
+
run = wait_on_run(_client, _thread, run)
|
94 |
+
|
95 |
+
list_run_steps(_client, _thread, run)
|
96 |
+
|
97 |
+
messages = list_messages(_client, _thread)
|
98 |
+
|
99 |
+
return extract_content_values(messages)[0]
|
100 |
+
|
101 |
+
gr.ChatInterface(
|
102 |
+
chat,
|
103 |
+
chatbot=gr.Chatbot(height=300),
|
104 |
+
textbox=gr.Textbox(placeholder="Type here", container=False, scale=7),
|
105 |
+
title="OpenAI Assistants API",
|
106 |
+
description="Ask AAA Assistant, BBB Assistant, and CCC Assistant any question",
|
107 |
+
retry_btn=None,
|
108 |
+
undo_btn=None,
|
109 |
+
clear_btn="Clear",
|
110 |
+
examples=[["I need to solve the equation '2x + 11 = 7.5'. Can you help me?", "sk-<BringYourOwn>"],
|
111 |
+
["Generate the first 20 fibbonaci numbers with code.", "sk-<BringYourOwn>"]],
|
112 |
+
cache_examples=False,
|
113 |
+
additional_inputs=[
|
114 |
+
gr.Textbox("sk-", label="OpenAI API Key", type = "password"),
|
115 |
+
],
|
116 |
+
).launch()
|