Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -2,6 +2,23 @@ import gradio as gr
|
|
2 |
import json, openai, os, time
|
3 |
from openai import OpenAI
|
4 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
def show_json(obj):
|
6 |
print("###")
|
7 |
print(json.loads(obj.model_dump_json()))
|
@@ -24,21 +41,10 @@ def extract_content_value(data):
|
|
24 |
content_values.append(content.text.value)
|
25 |
return content_values
|
26 |
|
27 |
-
client = OpenAI(api_key=os.environ.get("OPENAI_API_KEY"))
|
28 |
-
|
29 |
-
assistant = client.beta.assistants.create(
|
30 |
-
name="Math Tutor",
|
31 |
-
instructions="You are a personal math tutor. Answer questions briefly, in a sentence or less.",
|
32 |
-
model="gpt-4-1106-preview",
|
33 |
-
)
|
34 |
-
|
35 |
-
show_json(assistant)
|
36 |
-
|
37 |
-
thread = client.beta.threads.create()
|
38 |
-
|
39 |
-
show_json(thread)
|
40 |
|
41 |
def chat(message, history):
|
|
|
|
|
42 |
history_openai_format = []
|
43 |
for human, assistant in history:
|
44 |
history_openai_format.append({"role": "user", "content": human })
|
|
|
2 |
import json, openai, os, time
|
3 |
from openai import OpenAI
|
4 |
|
5 |
+
def init_assistant():
|
6 |
+
client = OpenAI(api_key=os.environ.get("OPENAI_API_KEY"))
|
7 |
+
|
8 |
+
assistant = client.beta.assistants.create(
|
9 |
+
name="Math Tutor",
|
10 |
+
instructions="You are a personal math tutor. Answer questions briefly, in a sentence or less.",
|
11 |
+
model="gpt-4-1106-preview",
|
12 |
+
)
|
13 |
+
|
14 |
+
show_json(assistant)
|
15 |
+
|
16 |
+
thread = client.beta.threads.create()
|
17 |
+
|
18 |
+
show_json(thread)
|
19 |
+
|
20 |
+
return assistant, thread
|
21 |
+
|
22 |
def show_json(obj):
|
23 |
print("###")
|
24 |
print(json.loads(obj.model_dump_json()))
|
|
|
41 |
content_values.append(content.text.value)
|
42 |
return content_values
|
43 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
44 |
|
45 |
def chat(message, history):
|
46 |
+
assistant, thread = init_assistant()
|
47 |
+
|
48 |
history_openai_format = []
|
49 |
for human, assistant in history:
|
50 |
history_openai_format.append({"role": "user", "content": human })
|