Update app.py
Browse files
app.py
CHANGED
@@ -4,24 +4,81 @@ import gradio as gr
|
|
4 |
from PIL import Image
|
5 |
import os
|
6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
from share_btn import community_icon_html, loading_icon_html, share_js
|
8 |
|
9 |
token = os.environ.get('HF_TOKEN')
|
10 |
-
|
11 |
tts = gr.Interface.load(name="spaces/Flux9665/IMS-Toucan")
|
12 |
talking_face = gr.Blocks.load(name="spaces/fffiloni/one-shot-talking-face", api_key=token)
|
13 |
|
14 |
def infer(audio):
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
|
|
|
|
|
|
19 |
portrait_link = talking_face("wise_woman_portrait.png", audio_response, fn_index=0)
|
20 |
|
21 |
-
|
22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
|
24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
|
26 |
title = """
|
27 |
<div style="text-align: center; max-width: 500px; margin: 0 auto;">
|
|
|
4 |
from PIL import Image
|
5 |
import os
|
6 |
|
7 |
+
os.system("pip install openai")
|
8 |
+
import openai
|
9 |
+
|
10 |
+
api_key = os.environ.get('api_key')
|
11 |
+
openai.api_key = api_key
|
12 |
+
|
13 |
+
whisper = gr.Interface.load(name="spaces/sanchit-gandhi/whisper-large-v2")
|
14 |
+
|
15 |
from share_btn import community_icon_html, loading_icon_html, share_js
|
16 |
|
17 |
token = os.environ.get('HF_TOKEN')
|
18 |
+
|
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
|
40 |
+
print(f"OpenAI API request timed out: {e}")
|
41 |
+
return "oups", f"OpenAI API request timed out: {e}"
|
42 |
+
except openai.error.APIError as e:
|
43 |
+
#Handle API error, e.g. retry or log
|
44 |
+
print(f"OpenAI API returned an API Error: {e}")
|
45 |
+
return "oups", f"OpenAI API returned an API Error: {e}"
|
46 |
+
except openai.error.APIConnectionError as e:
|
47 |
+
#Handle connection error, e.g. check network or log
|
48 |
+
print(f"OpenAI API request failed to connect: {e}")
|
49 |
+
return "oups", f"OpenAI API request failed to connect: {e}"
|
50 |
+
except openai.error.InvalidRequestError as e:
|
51 |
+
#Handle invalid request error, e.g. validate parameters or log
|
52 |
+
print(f"OpenAI API request was invalid: {e}")
|
53 |
+
return "oups", f"OpenAI API request was invalid: {e}"
|
54 |
+
except openai.error.AuthenticationError as e:
|
55 |
+
#Handle authentication error, e.g. check credentials or log
|
56 |
+
print(f"OpenAI API request was not authorized: {e}")
|
57 |
+
return "oups", f"OpenAI API request was not authorized: {e}"
|
58 |
+
except openai.error.PermissionError as e:
|
59 |
+
#Handle permission error, e.g. check scope or log
|
60 |
+
print(f"OpenAI API request was not permitted: {e}")
|
61 |
+
return "oups", f"OpenAI API request was not permitted: {e}"
|
62 |
+
except openai.error.RateLimitError as e:
|
63 |
+
#Handle rate limit error, e.g. wait 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):
|
68 |
+
|
69 |
+
print("starting open ai")
|
70 |
|
71 |
+
response = openai.Completion.create(
|
72 |
+
model="text-davinci-003",
|
73 |
+
prompt=message,
|
74 |
+
temperature=0.5,
|
75 |
+
max_tokens=2048,
|
76 |
+
top_p=1,
|
77 |
+
frequency_penalty=0,
|
78 |
+
presence_penalty=0.6
|
79 |
+
)
|
80 |
+
|
81 |
+
return str(response.choices[0].text).split("\n",2)[2]
|
82 |
|
83 |
title = """
|
84 |
<div style="text-align: center; max-width: 500px; margin: 0 auto;">
|