Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,12 +1,12 @@
|
|
1 |
import gradio as gr
|
2 |
-
import openai, os, time
|
3 |
|
4 |
from openai import OpenAI
|
5 |
from utils import show_json
|
6 |
|
7 |
-
|
8 |
|
9 |
-
|
10 |
|
11 |
def create_assistant(client):
|
12 |
assistant = client.beta.assistants.create(
|
@@ -19,7 +19,6 @@ def create_assistant(client):
|
|
19 |
model="gpt-4o",
|
20 |
tools=[
|
21 |
{"type": "code_interpreter"},
|
22 |
-
{"type": "retrieval"},
|
23 |
],
|
24 |
)
|
25 |
|
@@ -100,50 +99,43 @@ def extract_content_values(data):
|
|
100 |
|
101 |
return content_values
|
102 |
|
103 |
-
def chat(message, history
|
104 |
-
global
|
105 |
|
106 |
-
if
|
107 |
-
|
108 |
|
109 |
-
if
|
110 |
-
|
111 |
|
112 |
-
create_message(
|
113 |
|
114 |
-
run = create_run(
|
115 |
-
run = wait_on_run(
|
116 |
|
117 |
-
list_run_steps(
|
118 |
|
119 |
-
messages = list_messages(
|
120 |
|
121 |
content_values = extract_content_values(messages)
|
122 |
|
123 |
return content_values[0]
|
124 |
|
125 |
-
def vote(data: gr.LikeData):
|
126 |
-
print("voted")
|
127 |
-
|
128 |
gr.ChatInterface(
|
129 |
fn=chat,
|
130 |
-
chatbot=gr.Chatbot(height=
|
131 |
-
|
132 |
title="Python Code Generator",
|
133 |
-
description="
|
134 |
-
clear_btn="Clear",
|
135 |
-
retry_btn="Retry",
|
136 |
-
undo_btn="Undo",
|
137 |
examples=[
|
138 |
-
["Generate: NumPy/Pandas/Matplotlib & yfinance trading app"
|
139 |
-
["Explain: r'^(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])(?=.*[\\W]).{8,}$'"
|
140 |
-
["Fix: x = [5, 2, 1, 3, 4]; print(x.sort())"
|
141 |
-
["Optimize: x = []; for i in range(0, 10000): x.append(i)"
|
142 |
-
["Execute: Code to generate the first 20 fibbonaci numbers"
|
|
|
143 |
],
|
144 |
-
cache_examples=False,
|
145 |
-
additional_inputs=[
|
146 |
-
gr.Textbox("sk-", label="OpenAI API Key", type = "password"),
|
147 |
-
],
|
148 |
-
multimodal=True,
|
149 |
).launch()
|
|
|
1 |
import gradio as gr
|
2 |
+
import datetime, 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(
|
|
|
19 |
model="gpt-4o",
|
20 |
tools=[
|
21 |
{"type": "code_interpreter"},
|
|
|
22 |
],
|
23 |
)
|
24 |
|
|
|
99 |
|
100 |
return content_values
|
101 |
|
102 |
+
def chat(message, history):
|
103 |
+
global client, assistant, thread
|
104 |
|
105 |
+
if assistant == None:
|
106 |
+
assistant = create_assistant(client)
|
107 |
|
108 |
+
if thread == None:
|
109 |
+
thread = create_thread(client)
|
110 |
|
111 |
+
create_message(client, thread, message)
|
112 |
|
113 |
+
run = create_run(client, assistant, thread)
|
114 |
+
run = wait_on_run(client, thread, run)
|
115 |
|
116 |
+
list_run_steps(client, thread, run)
|
117 |
|
118 |
+
messages = list_messages(client, thread)
|
119 |
|
120 |
content_values = extract_content_values(messages)
|
121 |
|
122 |
return content_values[0]
|
123 |
|
|
|
|
|
|
|
124 |
gr.ChatInterface(
|
125 |
fn=chat,
|
126 |
+
chatbot=gr.Chatbot(height=500),
|
127 |
+
textbox=gr.Textbox(placeholder="Ask anything", container=False, scale=7),
|
128 |
title="Python Code Generator",
|
129 |
+
description="The assistant can generate, explain, fix, optimize, document, test, and generally help with code. It can also search the internet and execute code.",
|
130 |
+
#clear_btn="Clear",
|
131 |
+
#retry_btn="Retry",
|
132 |
+
#undo_btn="Undo",
|
133 |
examples=[
|
134 |
+
["Generate: NumPy/Pandas/Matplotlib & yfinance trading app"],
|
135 |
+
["Explain: r'^(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])(?=.*[\\W]).{8,}$'"],
|
136 |
+
["Fix: x = [5, 2, 1, 3, 4]; print(x.sort())"],
|
137 |
+
["Optimize: x = []; for i in range(0, 10000): x.append(i)"],
|
138 |
+
["Execute: Code to generate the first 20 fibbonaci numbers"],
|
139 |
+
[f"Today is {datetime.date.today()}. Create a plot showing stock gain YTD for NVDA, MSFT, AAPL, and GOOG, x-axis is 'Day' and y-axis is 'YTD Gain %'. Return the result, don't persist to storage."],
|
140 |
],
|
|
|
|
|
|
|
|
|
|
|
141 |
).launch()
|