Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -10,17 +10,21 @@ _assistant, _thread = None, None
|
|
10 |
|
11 |
def create_assistant(client):
|
12 |
assistant = client.beta.assistants.create(
|
13 |
-
name="Python Code Generator
|
14 |
-
instructions="You are a Python programming language expert that generates Pylint-compliant code and explains it. Only execute code when
|
15 |
model="gpt-4o",
|
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):
|
@@ -29,7 +33,9 @@ def create_message(client, thread, msg):
|
|
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):
|
@@ -37,7 +43,9 @@ def create_run(client, assistant, thread):
|
|
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):
|
@@ -46,8 +54,11 @@ def wait_on_run(client, thread, run):
|
|
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):
|
@@ -56,24 +67,30 @@ def list_run_steps(client, thread, run):
|
|
56 |
run_id=run.id,
|
57 |
order="asc",
|
58 |
)
|
|
|
59 |
for step in run_steps.data:
|
60 |
step_details = step.step_details
|
61 |
show_json("step_details", step_details)
|
|
|
62 |
return run_steps
|
63 |
|
64 |
def list_messages(client, thread):
|
65 |
messages = client.beta.threads.messages.list(
|
66 |
thread_id=thread.id
|
67 |
)
|
|
|
68 |
show_json("messages", messages)
|
|
|
69 |
return messages
|
70 |
|
71 |
def extract_content_values(data):
|
72 |
content_values = []
|
|
|
73 |
for item in data.data:
|
74 |
for content in item.content:
|
75 |
if content.type == "text":
|
76 |
content_values.append(content.text.value)
|
|
|
77 |
return content_values
|
78 |
|
79 |
def chat(message, history, openai_api_key):
|
@@ -87,7 +104,6 @@ def chat(message, history, openai_api_key):
|
|
87 |
|
88 |
create_message(_client, _thread, message)
|
89 |
|
90 |
-
# async
|
91 |
run = create_run(_client, _assistant, _thread)
|
92 |
run = wait_on_run(_client, _thread, run)
|
93 |
|
@@ -95,14 +111,17 @@ def chat(message, history, openai_api_key):
|
|
95 |
|
96 |
messages = list_messages(_client, _thread)
|
97 |
|
98 |
-
|
|
|
|
|
99 |
|
100 |
gr.ChatInterface(
|
101 |
chat,
|
102 |
chatbot=gr.Chatbot(height=300),
|
103 |
textbox=gr.Textbox(placeholder="Ask anything", container=False, scale=7),
|
104 |
-
|
105 |
-
|
|
|
106 |
clear_btn="Clear",
|
107 |
retry_btn="Retry",
|
108 |
undo_btn="Undo",
|
|
|
10 |
|
11 |
def create_assistant(client):
|
12 |
assistant = client.beta.assistants.create(
|
13 |
+
name="Python Code Generator",
|
14 |
+
instructions="You are a Python programming language expert that generates Pylint-compliant code and explains it. Only execute code when explicitly asked to.",
|
15 |
model="gpt-4o",
|
16 |
tools=[{"type": "code_interpreter"}],
|
17 |
)
|
18 |
+
|
19 |
show_json("assistant", assistant)
|
20 |
+
|
21 |
return assistant
|
22 |
|
23 |
def create_thread(client):
|
24 |
thread = client.beta.threads.create()
|
25 |
+
|
26 |
show_json("thread", thread)
|
27 |
+
|
28 |
return thread
|
29 |
|
30 |
def create_message(client, thread, msg):
|
|
|
33 |
thread_id=thread.id,
|
34 |
content=msg,
|
35 |
)
|
36 |
+
|
37 |
show_json("message", message)
|
38 |
+
|
39 |
return message
|
40 |
|
41 |
def create_run(client, assistant, thread):
|
|
|
43 |
assistant_id=assistant.id,
|
44 |
thread_id=thread.id,
|
45 |
)
|
46 |
+
|
47 |
show_json("run", run)
|
48 |
+
|
49 |
return run
|
50 |
|
51 |
def wait_on_run(client, thread, run):
|
|
|
54 |
thread_id=thread.id,
|
55 |
run_id=run.id,
|
56 |
)
|
57 |
+
|
58 |
time.sleep(0.25)
|
59 |
+
|
60 |
show_json("run", run)
|
61 |
+
|
62 |
return run
|
63 |
|
64 |
def list_run_steps(client, thread, run):
|
|
|
67 |
run_id=run.id,
|
68 |
order="asc",
|
69 |
)
|
70 |
+
|
71 |
for step in run_steps.data:
|
72 |
step_details = step.step_details
|
73 |
show_json("step_details", step_details)
|
74 |
+
|
75 |
return run_steps
|
76 |
|
77 |
def list_messages(client, thread):
|
78 |
messages = client.beta.threads.messages.list(
|
79 |
thread_id=thread.id
|
80 |
)
|
81 |
+
|
82 |
show_json("messages", messages)
|
83 |
+
|
84 |
return messages
|
85 |
|
86 |
def extract_content_values(data):
|
87 |
content_values = []
|
88 |
+
|
89 |
for item in data.data:
|
90 |
for content in item.content:
|
91 |
if content.type == "text":
|
92 |
content_values.append(content.text.value)
|
93 |
+
|
94 |
return content_values
|
95 |
|
96 |
def chat(message, history, openai_api_key):
|
|
|
104 |
|
105 |
create_message(_client, _thread, message)
|
106 |
|
|
|
107 |
run = create_run(_client, _assistant, _thread)
|
108 |
run = wait_on_run(_client, _thread, run)
|
109 |
|
|
|
111 |
|
112 |
messages = list_messages(_client, _thread)
|
113 |
|
114 |
+
content_values = extract_content_values(messages)
|
115 |
+
|
116 |
+
return content_values[0]
|
117 |
|
118 |
gr.ChatInterface(
|
119 |
chat,
|
120 |
chatbot=gr.Chatbot(height=300),
|
121 |
textbox=gr.Textbox(placeholder="Ask anything", container=False, scale=7),
|
122 |
+
textbox2=gr.Textbox(placeholder="OpenAI API Key", container=False, scale=7),
|
123 |
+
title="Python Code Generator",
|
124 |
+
description="Generate, explain, fix, optimize, document, test, help, ... Can execute code when asked to.",
|
125 |
clear_btn="Clear",
|
126 |
retry_btn="Retry",
|
127 |
undo_btn="Undo",
|