kathirog commited on
Commit
9a917c3
·
verified ·
1 Parent(s): 296d58e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -10
app.py CHANGED
@@ -4,8 +4,8 @@ import pyttsx3
4
  import speech_recognition as sr
5
 
6
  # Replace with your Gemini API Key and endpoint
7
- API_KEY = "AIzaSyAm0RSpUKY38494Fug8SPIpdHLXE2d3cps"
8
- API_URL = "https://generativelanguage.googleapis.com/v1alpha2/models/gemini2:generateText" # Example URL (adjust as needed)
9
 
10
  # Function to call Gemini API
11
  def call_gemini_api(message):
@@ -18,6 +18,7 @@ def call_gemini_api(message):
18
  "max_output_tokens": 100
19
  }
20
  try:
 
21
  response = requests.post(API_URL, headers=headers, json=payload)
22
  if response.status_code == 200:
23
  return response.json().get("generated_text", "No response text")
@@ -28,10 +29,15 @@ def call_gemini_api(message):
28
 
29
  # Convert text to speech (TTS)
30
  def text_to_speech(text):
31
- engine = pyttsx3.init()
32
- engine.save_to_file(text, "response.mp3")
33
- engine.runAndWait()
34
- return "response.mp3"
 
 
 
 
 
35
 
36
  # Convert audio to text (ASR)
37
  def audio_to_text(audio_path):
@@ -48,13 +54,17 @@ def audio_to_text(audio_path):
48
  # Define function for Gradio interface
49
  def respond(text_input=None, audio_input=None):
50
  if audio_input:
51
- text_input = audio_to_text(audio_input) # Convert audio to text if audio input is provided
 
52
 
53
  if not text_input:
54
  return "Error: No input provided.", None
55
 
56
- api_response = call_gemini_api(text_input) # Get response from Gemini API
57
- audio_response = text_to_speech(api_response) # Convert response text to audio
 
 
 
58
 
59
  return api_response, audio_response
60
 
@@ -72,4 +82,4 @@ demo = gr.Interface(
72
  )
73
 
74
  if __name__ == "__main__":
75
- demo.launch(share=True)
 
4
  import speech_recognition as sr
5
 
6
  # Replace with your Gemini API Key and endpoint
7
+ API_KEY = "AIzaSyAm0RSpUKY38494Fug8SPIpdHLXE2d3cps" # Replace with your actual API key
8
+ API_URL = "https://generativelanguage.googleapis.com/v1alpha2/models/gemini2:generateText" # Adjust according to the Gemini API docs
9
 
10
  # Function to call Gemini API
11
  def call_gemini_api(message):
 
18
  "max_output_tokens": 100
19
  }
20
  try:
21
+ # Sending request to Gemini API
22
  response = requests.post(API_URL, headers=headers, json=payload)
23
  if response.status_code == 200:
24
  return response.json().get("generated_text", "No response text")
 
29
 
30
  # Convert text to speech (TTS)
31
  def text_to_speech(text):
32
+ try:
33
+ engine = pyttsx3.init()
34
+ audio_filename = "response.mp3"
35
+ engine.save_to_file(text, audio_filename)
36
+ engine.runAndWait()
37
+ return audio_filename
38
+ except Exception as e:
39
+ print(f"Error with TTS: {e}")
40
+ return None
41
 
42
  # Convert audio to text (ASR)
43
  def audio_to_text(audio_path):
 
54
  # Define function for Gradio interface
55
  def respond(text_input=None, audio_input=None):
56
  if audio_input:
57
+ # If audio input is provided, convert it to text
58
+ text_input = audio_to_text(audio_input)
59
 
60
  if not text_input:
61
  return "Error: No input provided.", None
62
 
63
+ # Call Gemini API with text input and get response
64
+ api_response = call_gemini_api(text_input)
65
+
66
+ # Convert the API response text into audio
67
+ audio_response = text_to_speech(api_response)
68
 
69
  return api_response, audio_response
70
 
 
82
  )
83
 
84
  if __name__ == "__main__":
85
+ demo.launch(debug=True)