bstraehle commited on
Commit
5da2b8f
1 Parent(s): 3e56401

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -34
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
- _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,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, openai_api_key):
104
- global _client, _assistant, _thread
105
 
106
- if _assistant == None:
107
- _assistant = create_assistant(_client)
108
 
109
- if _thread == None:
110
- _thread = create_thread(_client)
111
 
112
- create_message(_client, _thread, message)
113
 
114
- run = create_run(_client, _assistant, _thread)
115
- run = wait_on_run(_client, _thread, run)
116
 
117
- list_run_steps(_client, _thread, run)
118
 
119
- messages = list_messages(_client, _thread)
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=300),
131
- #textbox=gr.Textbox(placeholder="Ask anything", container=False, scale=7),
132
  title="Python Code Generator",
133
- description="Generate, explain, fix, optimize, document, test, help, ... Can execute code when asked to.",
134
- clear_btn="Clear",
135
- retry_btn="Retry",
136
- undo_btn="Undo",
137
  examples=[
138
- ["Generate: NumPy/Pandas/Matplotlib & yfinance trading app", "sk-<BringYourOwn>"],
139
- ["Explain: r'^(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])(?=.*[\\W]).{8,}$'", "sk-<BringYourOwn>"],
140
- ["Fix: x = [5, 2, 1, 3, 4]; print(x.sort())", "sk-<BringYourOwn>"],
141
- ["Optimize: x = []; for i in range(0, 10000): x.append(i)", "sk-<BringYourOwn>"],
142
- ["Execute: Code to generate the first 20 fibbonaci numbers", "sk-<BringYourOwn>"],
 
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()