Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -43,66 +43,50 @@ def predict_crop(N, P, K, temperature, humidity, ph, rainfall):
|
|
| 43 |
|
| 44 |
|
| 45 |
# ---------------------------
|
| 46 |
-
#
|
| 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 "❌
|
| 62 |
except sr.RequestError:
|
| 63 |
-
return "❌
|
| 64 |
-
except Exception as e:
|
| 65 |
-
return f"❌ Error: {str(e)}", None
|
| 66 |
|
| 67 |
# ---------------------------
|
| 68 |
-
# Gemini Response
|
| 69 |
# ---------------------------
|
| 70 |
-
def get_gemini_response(query
|
| 71 |
try:
|
| 72 |
-
|
| 73 |
-
|
| 74 |
return response.text
|
| 75 |
except Exception as e:
|
| 76 |
-
return f"❌ Gemini
|
| 77 |
|
| 78 |
-
|
| 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
|
| 96 |
-
|
| 97 |
-
|
| 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 |
# ---------------------------
|
|
|
|
| 43 |
|
| 44 |
|
| 45 |
# ---------------------------
|
| 46 |
+
# Voice to Text Utility
|
| 47 |
# ---------------------------
|
| 48 |
def transcribe_audio(audio_path):
|
| 49 |
recognizer = sr.Recognizer()
|
| 50 |
+
with sr.AudioFile(audio_path) as source:
|
| 51 |
+
audio = recognizer.record(source)
|
| 52 |
+
try:
|
| 53 |
+
return recognizer.recognize_google(audio, language='pa-IN')
|
| 54 |
|
|
|
|
|
|
|
| 55 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
except sr.UnknownValueError:
|
| 57 |
+
return "❌ ਆਵਾਜ਼ ਨੂੰ ਸਮਝਿਆ ਨਹੀਂ ਜਾ ਸਕਿਆ।"
|
| 58 |
except sr.RequestError:
|
| 59 |
+
return "❌ ਗੂਗਲ ਸਪੀਚ ਐਪੀਆਈ ਨਾਲ ਕਨੇਕਟ ਨਹੀਂ ਹੋ ਸਕਿਆ।"
|
|
|
|
|
|
|
| 60 |
|
| 61 |
# ---------------------------
|
| 62 |
+
# Gemini Response & TTS
|
| 63 |
# ---------------------------
|
| 64 |
+
def get_gemini_response(query):
|
| 65 |
try:
|
| 66 |
+
response = gemini_model.generate_content(f"ਪੰਜਾਬੀ ਵਿੱਚ ਜਵਾਬ ਦਿਓ: {query}")
|
| 67 |
+
|
| 68 |
return response.text
|
| 69 |
except Exception as e:
|
| 70 |
+
return f"❌ Gemini ਤਰਫੋਂ ਗਲਤੀ: {str(e)}"
|
| 71 |
|
| 72 |
+
def text_to_speech(text, lang='pa'):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
|
| 74 |
+
|
| 75 |
+
|
| 76 |
+
tts = gTTS(text=text, lang=lang)
|
| 77 |
+
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".mp3")
|
| 78 |
+
tts.save(temp_file.name)
|
| 79 |
# Combined Function
|
| 80 |
# ---------------------------
|
| 81 |
def handle_voice_query(audio_file):
|
| 82 |
+
query = transcribe_audio(audio_file)
|
| 83 |
+
response = get_gemini_response(query)
|
| 84 |
+
audio_path = text_to_speech(response)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
|
| 86 |
|
| 87 |
+
|
| 88 |
+
return query, response, audio_path
|
| 89 |
+
|
| 90 |
# ---------------------------
|
| 91 |
# Gradio Interface
|
| 92 |
# ---------------------------
|