Spaces:
Running
Running
Update app.py
Browse filesOpenAI client no longer global.
app.py
CHANGED
|
@@ -37,7 +37,7 @@ else:
|
|
| 37 |
|
| 38 |
speak_file = dataDir + "speek.wav"
|
| 39 |
|
| 40 |
-
client = OpenAI(api_key = key)
|
| 41 |
|
| 42 |
#digits = ['zero: ','one: ','two: ','three: ','four: ','five: ','six: ','seven: ','eight: ','nine: ']
|
| 43 |
|
|
@@ -52,7 +52,9 @@ class Step(BaseModel):
|
|
| 52 |
class MathReasoning(BaseModel):
|
| 53 |
steps: list[Step]
|
| 54 |
final_answer: str
|
| 55 |
-
|
|
|
|
|
|
|
| 56 |
|
| 57 |
def solve(prompt, chatType):
|
| 58 |
tokens_in = 0
|
|
@@ -63,7 +65,7 @@ def solve(prompt, chatType):
|
|
| 63 |
elif chatType == "logic":
|
| 64 |
instruction = "you are an expert in logic and reasoning. Guide the user through the solution step by step"
|
| 65 |
try:
|
| 66 |
-
completion =
|
| 67 |
model = 'gpt-4o-2024-08-06',
|
| 68 |
messages = [
|
| 69 |
{"role": "system", "content": instruction},
|
|
@@ -277,7 +279,7 @@ def chat(prompt, user_window, pwd_window, past, response, gptModel, uploaded_ima
|
|
| 277 |
(reply, tokens_in, tokens_out, tokens) = solve(prompt, chatType)
|
| 278 |
reporting_model = image_gen_model
|
| 279 |
elif not gen_image:
|
| 280 |
-
completion =
|
| 281 |
messages=past)
|
| 282 |
reporting_model = gptModel
|
| 283 |
else:
|
|
@@ -335,7 +337,7 @@ def transcribe(user, pwd, fpath):
|
|
| 335 |
with open(dataDir + user + '_audio.txt','a') as f:
|
| 336 |
f.write(f'audio:{str(duration)}\n')
|
| 337 |
with open(fpath,'rb') as audio_file:
|
| 338 |
-
transcript =
|
| 339 |
model='whisper-1', file = audio_file ,response_format = 'text' )
|
| 340 |
reply = transcript
|
| 341 |
return str(reply)
|
|
@@ -346,7 +348,7 @@ def pause_message():
|
|
| 346 |
# def gen_output_audio(txt):
|
| 347 |
# if len(txt) < 10:
|
| 348 |
# txt = "This dialog is too short to mess with!"
|
| 349 |
-
# response =
|
| 350 |
# with open(speak_file, 'wb') as fp:
|
| 351 |
# fp.write(response.content)
|
| 352 |
# return speak_file
|
|
@@ -370,7 +372,7 @@ def update_user(user_win):
|
|
| 370 |
def speech_worker(chunks=[],q=[]):
|
| 371 |
for chunk in chunks:
|
| 372 |
fpath = q.pop(0)
|
| 373 |
-
response =
|
| 374 |
with open(fpath, 'wb') as fp:
|
| 375 |
fp.write(response.content)
|
| 376 |
|
|
@@ -431,7 +433,7 @@ def make_image(prompt, user, pwd):
|
|
| 431 |
if len(prompt.strip()) == 0:
|
| 432 |
return [gr.Image(value=None, visible=False), 'You must provide a prompt describing image you desire']
|
| 433 |
try:
|
| 434 |
-
response =
|
| 435 |
quality='standard', response_format='b64_json')
|
| 436 |
except Exception as ex:
|
| 437 |
msg = ex.message
|
|
@@ -520,7 +522,7 @@ def analyze_image(user, model, prompt):
|
|
| 520 |
status = "base64 image file not found"
|
| 521 |
return [None, status]
|
| 522 |
|
| 523 |
-
completion =
|
| 524 |
model=model,
|
| 525 |
messages=[
|
| 526 |
{ "role": "user",
|
|
@@ -640,7 +642,7 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 640 |
fname = q.pop(0)
|
| 641 |
if len(chunklist) > 0:
|
| 642 |
threading.Thread(target=speech_worker, daemon=True, args=(chunklist[1:],fname_list[1:])).start()
|
| 643 |
-
response =
|
| 644 |
with open(fname, 'wb') as fp:
|
| 645 |
fp.write(response.content)
|
| 646 |
return [fname, q]
|
|
@@ -654,7 +656,7 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 654 |
if not os.path.exists(fname):
|
| 655 |
sleep(3)
|
| 656 |
if not os.path.exists(fname):
|
| 657 |
-
response =
|
| 658 |
input='Sorry, text-to-speech is responding too slow right now', speed=0.85, response_format='wav')
|
| 659 |
with open(fname, 'wb') as fp:
|
| 660 |
fp.write(response.content)
|
|
@@ -689,8 +691,9 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 689 |
image_window2 = gr.Image(visible=False, interactive=True, label='Image to Analyze', type='filepath')
|
| 690 |
with gr.Column():
|
| 691 |
image_window = gr.Image(visible=False, label='Generated Image')
|
| 692 |
-
submit_button.click(chat,
|
| 693 |
-
|
|
|
|
| 694 |
clear_button.click(fn=new_conversation, inputs=user_window,
|
| 695 |
outputs=[prompt_window, history, output_window, image_window, image_window2, uploaded_image_file])
|
| 696 |
audio_widget.stop_recording(fn=transcribe, inputs=[user_window, password, audio_widget],
|
|
|
|
| 37 |
|
| 38 |
speak_file = dataDir + "speek.wav"
|
| 39 |
|
| 40 |
+
# client = OpenAI(api_key = key)
|
| 41 |
|
| 42 |
#digits = ['zero: ','one: ','two: ','three: ','four: ','five: ','six: ','seven: ','eight: ','nine: ']
|
| 43 |
|
|
|
|
| 52 |
class MathReasoning(BaseModel):
|
| 53 |
steps: list[Step]
|
| 54 |
final_answer: str
|
| 55 |
+
|
| 56 |
+
def Client():
|
| 57 |
+
return OpenAI(api_key = key)
|
| 58 |
|
| 59 |
def solve(prompt, chatType):
|
| 60 |
tokens_in = 0
|
|
|
|
| 65 |
elif chatType == "logic":
|
| 66 |
instruction = "you are an expert in logic and reasoning. Guide the user through the solution step by step"
|
| 67 |
try:
|
| 68 |
+
completion = Client().beta.chat.completions.parse(
|
| 69 |
model = 'gpt-4o-2024-08-06',
|
| 70 |
messages = [
|
| 71 |
{"role": "system", "content": instruction},
|
|
|
|
| 279 |
(reply, tokens_in, tokens_out, tokens) = solve(prompt, chatType)
|
| 280 |
reporting_model = image_gen_model
|
| 281 |
elif not gen_image:
|
| 282 |
+
completion = Client().chat.completions.create(model=gptModel,
|
| 283 |
messages=past)
|
| 284 |
reporting_model = gptModel
|
| 285 |
else:
|
|
|
|
| 337 |
with open(dataDir + user + '_audio.txt','a') as f:
|
| 338 |
f.write(f'audio:{str(duration)}\n')
|
| 339 |
with open(fpath,'rb') as audio_file:
|
| 340 |
+
transcript = Client().audio.transcriptions.create(
|
| 341 |
model='whisper-1', file = audio_file ,response_format = 'text' )
|
| 342 |
reply = transcript
|
| 343 |
return str(reply)
|
|
|
|
| 348 |
# def gen_output_audio(txt):
|
| 349 |
# if len(txt) < 10:
|
| 350 |
# txt = "This dialog is too short to mess with!"
|
| 351 |
+
# response = Client().audio.speech.create(model="tts-1", voice="fable", input=txt)
|
| 352 |
# with open(speak_file, 'wb') as fp:
|
| 353 |
# fp.write(response.content)
|
| 354 |
# return speak_file
|
|
|
|
| 372 |
def speech_worker(chunks=[],q=[]):
|
| 373 |
for chunk in chunks:
|
| 374 |
fpath = q.pop(0)
|
| 375 |
+
response = Client().audio.speech.create(model="tts-1", voice="fable", input=chunk, speed=0.85, response_format='wav')
|
| 376 |
with open(fpath, 'wb') as fp:
|
| 377 |
fp.write(response.content)
|
| 378 |
|
|
|
|
| 433 |
if len(prompt.strip()) == 0:
|
| 434 |
return [gr.Image(value=None, visible=False), 'You must provide a prompt describing image you desire']
|
| 435 |
try:
|
| 436 |
+
response = Client().images.generate(model='dall-e-2', prompt=prompt,size='512x512',
|
| 437 |
quality='standard', response_format='b64_json')
|
| 438 |
except Exception as ex:
|
| 439 |
msg = ex.message
|
|
|
|
| 522 |
status = "base64 image file not found"
|
| 523 |
return [None, status]
|
| 524 |
|
| 525 |
+
completion = Client().chat.completions.create(
|
| 526 |
model=model,
|
| 527 |
messages=[
|
| 528 |
{ "role": "user",
|
|
|
|
| 642 |
fname = q.pop(0)
|
| 643 |
if len(chunklist) > 0:
|
| 644 |
threading.Thread(target=speech_worker, daemon=True, args=(chunklist[1:],fname_list[1:])).start()
|
| 645 |
+
response = Client().audio.speech.create(model="tts-1", voice="fable", input=chunk, speed=0.85, response_format='wav')
|
| 646 |
with open(fname, 'wb') as fp:
|
| 647 |
fp.write(response.content)
|
| 648 |
return [fname, q]
|
|
|
|
| 656 |
if not os.path.exists(fname):
|
| 657 |
sleep(3)
|
| 658 |
if not os.path.exists(fname):
|
| 659 |
+
response = Client().audio.speech.create(model="tts-1", voice="fable",
|
| 660 |
input='Sorry, text-to-speech is responding too slow right now', speed=0.85, response_format='wav')
|
| 661 |
with open(fname, 'wb') as fp:
|
| 662 |
fp.write(response.content)
|
|
|
|
| 691 |
image_window2 = gr.Image(visible=False, interactive=True, label='Image to Analyze', type='filepath')
|
| 692 |
with gr.Column():
|
| 693 |
image_window = gr.Image(visible=False, label='Generated Image')
|
| 694 |
+
submit_button.click(chat,
|
| 695 |
+
inputs=[prompt_window, user_window, password, history, output_window, model, uploaded_image_file],
|
| 696 |
+
outputs=[history, output_window, prompt_window, model, uploaded_image_file])
|
| 697 |
clear_button.click(fn=new_conversation, inputs=user_window,
|
| 698 |
outputs=[prompt_window, history, output_window, image_window, image_window2, uploaded_image_file])
|
| 699 |
audio_widget.stop_recording(fn=transcribe, inputs=[user_window, password, audio_widget],
|