Ath
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -29,14 +29,17 @@ eleven_labs_api_key = os.getenv("ELEVEN_LABS_API_KEY")
|
|
29 |
eleven_labs_url = "https://api.elevenlabs.io/v1/text-to-speech/aEO01A4wXwd1O8GPgGlF"
|
30 |
|
31 |
def chat_and_tts_text(user_input, history):
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
|
|
|
|
|
|
40 |
|
41 |
def convert_audio_to_text(audio_file):
|
42 |
# This is a placeholder function. Replace with actual implementation.
|
@@ -44,41 +47,44 @@ def convert_audio_to_text(audio_file):
|
|
44 |
return "Sample text from audio"
|
45 |
|
46 |
def chat_and_tts_audio(audio_file, history):
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
"
|
59 |
-
|
|
|
|
|
60 |
}
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
|
|
82 |
|
83 |
# Create the Gradio UI
|
84 |
with gr.Blocks() as demo:
|
@@ -103,4 +109,4 @@ with gr.Blocks() as demo:
|
|
103 |
submit_btn_text.click(chat_and_tts_text, inputs=[user_input_text, state], outputs=[chatbot, state])
|
104 |
submit_btn_audio.click(chat_and_tts_audio, inputs=[user_input_audio, state], outputs=[chatbot, state, audio_output])
|
105 |
|
106 |
-
demo.launch()
|
|
|
29 |
eleven_labs_url = "https://api.elevenlabs.io/v1/text-to-speech/aEO01A4wXwd1O8GPgGlF"
|
30 |
|
31 |
def chat_and_tts_text(user_input, history):
|
32 |
+
try:
|
33 |
+
# Send the user's text input to the chat session
|
34 |
+
response = chat_session.send_message(user_input)
|
35 |
+
response_text = response.text
|
36 |
+
|
37 |
+
# Update the chat history with text input and response
|
38 |
+
history.append((user_input, response_text))
|
39 |
+
|
40 |
+
return history, history
|
41 |
+
except Exception as e:
|
42 |
+
return history, f"Error: {str(e)}"
|
43 |
|
44 |
def convert_audio_to_text(audio_file):
|
45 |
# This is a placeholder function. Replace with actual implementation.
|
|
|
47 |
return "Sample text from audio"
|
48 |
|
49 |
def chat_and_tts_audio(audio_file, history):
|
50 |
+
try:
|
51 |
+
# Convert uploaded audio file to text
|
52 |
+
user_input = convert_audio_to_text(audio_file)
|
53 |
+
|
54 |
+
# Send the user's audio input to the chat session
|
55 |
+
response = chat_session.send_message(user_input)
|
56 |
+
response_text = response.text
|
57 |
+
|
58 |
+
# Eleven Labs text-to-speech request payload
|
59 |
+
payload = {
|
60 |
+
"text": response_text,
|
61 |
+
"voice_settings": {
|
62 |
+
"stability": 0,
|
63 |
+
"similarity_boost": 0
|
64 |
+
}
|
65 |
}
|
66 |
+
headers = {
|
67 |
+
"xi-api-key": eleven_labs_api_key,
|
68 |
+
"Content-Type": "application/json"
|
69 |
+
}
|
70 |
+
|
71 |
+
# Make the request to Eleven Labs API
|
72 |
+
tts_response = requests.post(eleven_labs_url, json=payload, headers=headers)
|
73 |
+
|
74 |
+
# Check if the response is successful and save the audio content to a temporary file
|
75 |
+
if tts_response.status_code == 200:
|
76 |
+
with NamedTemporaryFile(delete=False, suffix=".mp3") as temp_audio:
|
77 |
+
temp_audio.write(tts_response.content)
|
78 |
+
audio_path = temp_audio.name
|
79 |
+
else:
|
80 |
+
audio_path = None
|
81 |
+
|
82 |
+
# Update the chat history with audio input and response
|
83 |
+
history.append((user_input, response_text))
|
84 |
+
|
85 |
+
return history, history, audio_path
|
86 |
+
except Exception as e:
|
87 |
+
return history, f"Error: {str(e)}", None
|
88 |
|
89 |
# Create the Gradio UI
|
90 |
with gr.Blocks() as demo:
|
|
|
109 |
submit_btn_text.click(chat_and_tts_text, inputs=[user_input_text, state], outputs=[chatbot, state])
|
110 |
submit_btn_audio.click(chat_and_tts_audio, inputs=[user_input_audio, state], outputs=[chatbot, state, audio_output])
|
111 |
|
112 |
+
demo.launch()
|