Ath commited on
Commit
575cf6f
·
verified ·
1 Parent(s): 97d1d38

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -43
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
- # Send the user's text input to the chat session
33
- response = chat_session.send_message(user_input)
34
- response_text = response.text
35
-
36
- # Update the chat history with text input and response
37
- history.append((user_input, response_text))
38
-
39
- return history, history
 
 
 
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
- # Convert uploaded audio file to text
48
- user_input = convert_audio_to_text(audio_file)
49
-
50
- # Send the user's audio input to the chat session
51
- response = chat_session.send_message(user_input)
52
- response_text = response.text
53
-
54
- # Eleven Labs text-to-speech request payload
55
- payload = {
56
- "text": response_text,
57
- "voice_settings": {
58
- "stability": 0,
59
- "similarity_boost": 0
 
 
60
  }
61
- }
62
- headers = {
63
- "xi-api-key": eleven_labs_api_key,
64
- "Content-Type": "application/json"
65
- }
66
-
67
- # Make the request to Eleven Labs API
68
- tts_response = requests.post(eleven_labs_url, json=payload, headers=headers)
69
-
70
- # Check if the response is successful and save the audio content to a temporary file
71
- if tts_response.status_code == 200:
72
- with NamedTemporaryFile(delete=False, suffix=".mp3") as temp_audio:
73
- temp_audio.write(tts_response.content)
74
- audio_path = temp_audio.name
75
- else:
76
- audio_path = None
77
-
78
- # Update the chat history with audio input and response
79
- history.append((user_input, response_text))
80
-
81
- return history, history, audio_path
 
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()