Update app.py
Browse files
app.py
CHANGED
|
@@ -7,8 +7,8 @@ import os
|
|
| 7 |
os.system("pip install openai")
|
| 8 |
import openai
|
| 9 |
|
| 10 |
-
api_key = os.environ.get('api_key')
|
| 11 |
-
|
| 12 |
|
| 13 |
whisper = gr.Interface.load(name="spaces/sanchit-gandhi/whisper-large-v2")
|
| 14 |
|
|
@@ -19,21 +19,21 @@ token = os.environ.get('HF_TOKEN')
|
|
| 19 |
tts = gr.Interface.load(name="spaces/Flux9665/IMS-Toucan")
|
| 20 |
talking_face = gr.Blocks.load(name="spaces/fffiloni/one-shot-talking-face", api_key=token)
|
| 21 |
|
| 22 |
-
def infer(audio):
|
| 23 |
|
| 24 |
whisper_result = whisper(audio, None, "translate", fn_index=0)
|
| 25 |
|
| 26 |
-
gpt_response = try_api(whisper_result)
|
| 27 |
|
| 28 |
audio_response = tts(gpt_response[0], "English Text", "English Accent", "English Speaker's Voice", fn_index=0)
|
| 29 |
|
| 30 |
portrait_link = talking_face("wise_woman_portrait.png", audio_response, fn_index=0)
|
| 31 |
|
| 32 |
-
return whisper_result, portrait_link, gr.update(visible=True)
|
| 33 |
|
| 34 |
-
def try_api(message):
|
| 35 |
try:
|
| 36 |
-
response = call_api(message)
|
| 37 |
return response, "no error"
|
| 38 |
except openai.error.Timeout as e:
|
| 39 |
#Handle timeout error, e.g. retry or log
|
|
@@ -64,10 +64,12 @@ def try_api(message):
|
|
| 64 |
print(f"OpenAI API request exceeded rate limit: {e}")
|
| 65 |
return "oups", f"OpenAI API request exceeded rate limit: {e}"
|
| 66 |
|
| 67 |
-
def call_api(message):
|
| 68 |
|
| 69 |
print("starting open ai")
|
| 70 |
|
|
|
|
|
|
|
| 71 |
response = openai.Completion.create(
|
| 72 |
model="text-davinci-003",
|
| 73 |
prompt=message,
|
|
@@ -108,10 +110,12 @@ with gr.Blocks(css="style.css") as demo:
|
|
| 108 |
gr.HTML(title)
|
| 109 |
|
| 110 |
gpt_response = gr.Video(label="Talking Portrait response", elem_id="video_out")
|
|
|
|
| 111 |
|
| 112 |
with gr.Column(elem_id="col-container-2"):
|
| 113 |
-
|
| 114 |
-
|
|
|
|
| 115 |
whisper_tr = gr.Textbox(label="whisper english translation", elem_id="text_inp")
|
| 116 |
|
| 117 |
send_btn = gr.Button("Send my request !")
|
|
@@ -121,7 +125,7 @@ with gr.Blocks(css="style.css") as demo:
|
|
| 121 |
loading_icon = gr.HTML(loading_icon_html)
|
| 122 |
share_button = gr.Button("Share to community", elem_id="share-btn")
|
| 123 |
|
| 124 |
-
send_btn.click(infer, inputs=[record_input], outputs=[whisper_tr, gpt_response, share_group])
|
| 125 |
share_button.click(None, [], [], _js=share_js)
|
| 126 |
|
| 127 |
demo.queue(max_size=32, concurrency_count=20).launch(debug=True)
|
|
|
|
| 7 |
os.system("pip install openai")
|
| 8 |
import openai
|
| 9 |
|
| 10 |
+
#api_key = os.environ.get('api_key')
|
| 11 |
+
|
| 12 |
|
| 13 |
whisper = gr.Interface.load(name="spaces/sanchit-gandhi/whisper-large-v2")
|
| 14 |
|
|
|
|
| 19 |
tts = gr.Interface.load(name="spaces/Flux9665/IMS-Toucan")
|
| 20 |
talking_face = gr.Blocks.load(name="spaces/fffiloni/one-shot-talking-face", api_key=token)
|
| 21 |
|
| 22 |
+
def infer(audio, openai_api_key):
|
| 23 |
|
| 24 |
whisper_result = whisper(audio, None, "translate", fn_index=0)
|
| 25 |
|
| 26 |
+
gpt_response = try_api(whisper_result, openai_api_key)
|
| 27 |
|
| 28 |
audio_response = tts(gpt_response[0], "English Text", "English Accent", "English Speaker's Voice", fn_index=0)
|
| 29 |
|
| 30 |
portrait_link = talking_face("wise_woman_portrait.png", audio_response, fn_index=0)
|
| 31 |
|
| 32 |
+
return whisper_result, portrait_link, gpt_response[1], gr.update(visible=True)
|
| 33 |
|
| 34 |
+
def try_api(message, openai_api_key):
|
| 35 |
try:
|
| 36 |
+
response = call_api(message, openai_api_key)
|
| 37 |
return response, "no error"
|
| 38 |
except openai.error.Timeout as e:
|
| 39 |
#Handle timeout error, e.g. retry or log
|
|
|
|
| 64 |
print(f"OpenAI API request exceeded rate limit: {e}")
|
| 65 |
return "oups", f"OpenAI API request exceeded rate limit: {e}"
|
| 66 |
|
| 67 |
+
def call_api(message, openai_api_key):
|
| 68 |
|
| 69 |
print("starting open ai")
|
| 70 |
|
| 71 |
+
openai.api_key = openai_api_key
|
| 72 |
+
|
| 73 |
response = openai.Completion.create(
|
| 74 |
model="text-davinci-003",
|
| 75 |
prompt=message,
|
|
|
|
| 110 |
gr.HTML(title)
|
| 111 |
|
| 112 |
gpt_response = gr.Video(label="Talking Portrait response", elem_id="video_out")
|
| 113 |
+
error_handler = gr.Text()
|
| 114 |
|
| 115 |
with gr.Column(elem_id="col-container-2"):
|
| 116 |
+
with gr.Row():
|
| 117 |
+
record_input = gr.Audio(source="microphone",type="filepath", label="Audio input", show_label=True, elem_id="record_btn")
|
| 118 |
+
openai_api_key = gr.Text(label="Your OpenAI API Key")
|
| 119 |
whisper_tr = gr.Textbox(label="whisper english translation", elem_id="text_inp")
|
| 120 |
|
| 121 |
send_btn = gr.Button("Send my request !")
|
|
|
|
| 125 |
loading_icon = gr.HTML(loading_icon_html)
|
| 126 |
share_button = gr.Button("Share to community", elem_id="share-btn")
|
| 127 |
|
| 128 |
+
send_btn.click(infer, inputs=[record_input, openai_api_key], outputs=[whisper_tr, gpt_response, error_handler, share_group])
|
| 129 |
share_button.click(None, [], [], _js=share_js)
|
| 130 |
|
| 131 |
demo.queue(max_size=32, concurrency_count=20).launch(debug=True)
|