Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -42,18 +42,27 @@ def predict_crop(N, P, K, temperature, humidity, ph, rainfall):
|
|
| 42 |
|
| 43 |
|
| 44 |
|
|
|
|
|
|
|
|
|
|
| 45 |
def transcribe_audio(audio_path):
|
| 46 |
recognizer = sr.Recognizer()
|
| 47 |
-
|
| 48 |
-
|
|
|
|
|
|
|
| 49 |
try:
|
| 50 |
-
|
| 51 |
-
|
|
|
|
|
|
|
| 52 |
return text, lang
|
| 53 |
except sr.UnknownValueError:
|
| 54 |
-
return "❌ Could not understand audio.", None
|
| 55 |
except sr.RequestError:
|
| 56 |
-
return "❌ Could not connect to Google API.", None
|
|
|
|
|
|
|
| 57 |
|
| 58 |
# ---------------------------
|
| 59 |
# Gemini Response in Detected Language
|
|
@@ -70,23 +79,30 @@ def get_gemini_response(query, lang_code):
|
|
| 70 |
# Text to Speech in Same Language
|
| 71 |
# ---------------------------
|
| 72 |
def text_to_speech(text, lang):
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
|
| 78 |
# ---------------------------
|
| 79 |
# Combined Function
|
| 80 |
# ---------------------------
|
| 81 |
def handle_voice_query(audio_file):
|
| 82 |
query, lang = transcribe_audio(audio_file)
|
|
|
|
| 83 |
if lang is None:
|
| 84 |
return query, "⚠️ No response", None
|
| 85 |
|
| 86 |
response = get_gemini_response(query, lang)
|
| 87 |
audio_path = text_to_speech(response, lang)
|
|
|
|
| 88 |
return query, response, audio_path
|
| 89 |
|
|
|
|
| 90 |
# ---------------------------
|
| 91 |
# Gradio Interface
|
| 92 |
# ---------------------------
|
|
|
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
+
# ---------------------------
|
| 46 |
+
# Transcribe Audio & Detect Language
|
| 47 |
+
# ---------------------------
|
| 48 |
def transcribe_audio(audio_path):
|
| 49 |
recognizer = sr.Recognizer()
|
| 50 |
+
|
| 51 |
+
# Handle both file paths and file-like objects
|
| 52 |
+
audio_input = audio_path.name if hasattr(audio_path, "name") else audio_path
|
| 53 |
+
|
| 54 |
try:
|
| 55 |
+
with sr.AudioFile(audio_input) as source:
|
| 56 |
+
audio = recognizer.record(source)
|
| 57 |
+
text = recognizer.recognize_google(audio) # auto language transcription
|
| 58 |
+
lang = detect(text) # detect language from transcribed text
|
| 59 |
return text, lang
|
| 60 |
except sr.UnknownValueError:
|
| 61 |
+
return "❌ Could not understand the audio.", None
|
| 62 |
except sr.RequestError:
|
| 63 |
+
return "❌ Could not connect to Google Speech API.", None
|
| 64 |
+
except Exception as e:
|
| 65 |
+
return f"❌ Error: {str(e)}", None
|
| 66 |
|
| 67 |
# ---------------------------
|
| 68 |
# Gemini Response in Detected Language
|
|
|
|
| 79 |
# Text to Speech in Same Language
|
| 80 |
# ---------------------------
|
| 81 |
def text_to_speech(text, lang):
|
| 82 |
+
try:
|
| 83 |
+
tts = gTTS(text=text, lang=lang)
|
| 84 |
+
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".mp3")
|
| 85 |
+
tts.save(temp_file.name)
|
| 86 |
+
return temp_file.name
|
| 87 |
+
except Exception as e:
|
| 88 |
+
print(f"❌ TTS Error: {str(e)}")
|
| 89 |
+
return None
|
| 90 |
|
| 91 |
# ---------------------------
|
| 92 |
# Combined Function
|
| 93 |
# ---------------------------
|
| 94 |
def handle_voice_query(audio_file):
|
| 95 |
query, lang = transcribe_audio(audio_file)
|
| 96 |
+
|
| 97 |
if lang is None:
|
| 98 |
return query, "⚠️ No response", None
|
| 99 |
|
| 100 |
response = get_gemini_response(query, lang)
|
| 101 |
audio_path = text_to_speech(response, lang)
|
| 102 |
+
|
| 103 |
return query, response, audio_path
|
| 104 |
|
| 105 |
+
|
| 106 |
# ---------------------------
|
| 107 |
# Gradio Interface
|
| 108 |
# ---------------------------
|