Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,5 @@
|
|
| 1 |
import os
|
| 2 |
-
import httpx
|
| 3 |
import base64
|
| 4 |
-
import asyncio
|
| 5 |
from io import BytesIO
|
| 6 |
from typing import Optional
|
| 7 |
import async_timeout
|
|
@@ -10,6 +8,7 @@ from dotenv import load_dotenv
|
|
| 10 |
from elevenlabs import generate, set_api_key
|
| 11 |
from loguru import logger
|
| 12 |
from pydantic import BaseModel
|
|
|
|
| 13 |
|
| 14 |
load_dotenv()
|
| 15 |
|
|
@@ -32,34 +31,31 @@ async def make_completion(messages, nb_retries: int = 5, delay: int = 30) -> Opt
|
|
| 32 |
"Content-Type": "application/json",
|
| 33 |
"Authorization": f"Bearer {API_KEY}"
|
| 34 |
}
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
except asyncio.TimeoutError as e:
|
| 61 |
-
logger.error(f"Timeout {delay} seconds !")
|
| 62 |
-
return None
|
| 63 |
|
| 64 |
|
| 65 |
def audio_to_html(audio_bytes):
|
|
@@ -105,5 +101,6 @@ with gr.Blocks() as demo:
|
|
| 105 |
output_html.visible = False
|
| 106 |
|
| 107 |
txt.submit(predict, [txt, state], [chatbot, state, output_html])
|
|
|
|
| 108 |
|
| 109 |
demo.launch(debug=True)
|
|
|
|
| 1 |
import os
|
|
|
|
| 2 |
import base64
|
|
|
|
| 3 |
from io import BytesIO
|
| 4 |
from typing import Optional
|
| 5 |
import async_timeout
|
|
|
|
| 8 |
from elevenlabs import generate, set_api_key
|
| 9 |
from loguru import logger
|
| 10 |
from pydantic import BaseModel
|
| 11 |
+
import openai
|
| 12 |
|
| 13 |
load_dotenv()
|
| 14 |
|
|
|
|
| 31 |
"Content-Type": "application/json",
|
| 32 |
"Authorization": f"Bearer {API_KEY}"
|
| 33 |
}
|
| 34 |
+
async with async_timeout.timeout(delay=delay):
|
| 35 |
+
counter = 0
|
| 36 |
+
keep_loop = True
|
| 37 |
+
while keep_loop:
|
| 38 |
+
logger.debug(f"Chat/Completions Nb Retries : {counter}")
|
| 39 |
+
try:
|
| 40 |
+
resp = openai.ChatCompletion.create(
|
| 41 |
+
model="gpt-3.5-turbo",
|
| 42 |
+
messages=[{"role": "system", "content": system}] + messages
|
| 43 |
+
)
|
| 44 |
+
except openai.error.APIError as e:
|
| 45 |
+
# Handle API error here, e.g. retry or log
|
| 46 |
+
print(f"OpenAI API returned an API Error: {e}")
|
| 47 |
+
pass
|
| 48 |
+
except openai.error.APIConnectionError as e:
|
| 49 |
+
# Handle connection error here
|
| 50 |
+
print(f"Failed to connect to OpenAI API: {e}")
|
| 51 |
+
pass
|
| 52 |
+
logger.error(e)
|
| 53 |
+
else:
|
| 54 |
+
return resp["choices"][0]["message"]["content"]
|
| 55 |
+
finally:
|
| 56 |
+
counter += 1
|
| 57 |
+
keep_loop = counter < nb_retries
|
| 58 |
+
return ''
|
|
|
|
|
|
|
|
|
|
| 59 |
|
| 60 |
|
| 61 |
def audio_to_html(audio_bytes):
|
|
|
|
| 101 |
output_html.visible = False
|
| 102 |
|
| 103 |
txt.submit(predict, [txt, state], [chatbot, state, output_html])
|
| 104 |
+
txt.submit(lambda x: gr.update(value=''), [txt],[txt])
|
| 105 |
|
| 106 |
demo.launch(debug=True)
|