Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,72 +1,56 @@
|
|
| 1 |
-
|
| 2 |
import speech_recognition as sr
|
| 3 |
from deep_translator import GoogleTranslator
|
| 4 |
-
|
| 5 |
from gtts import gTTS
|
| 6 |
-
import tempfile
|
| 7 |
import os
|
| 8 |
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
# Function to recognize speech from an audio file
|
| 13 |
-
def recognize_speech(audio_file):
|
| 14 |
recognizer = sr.Recognizer()
|
| 15 |
-
with sr.AudioFile(
|
| 16 |
-
|
| 17 |
try:
|
| 18 |
-
|
|
|
|
| 19 |
except sr.UnknownValueError:
|
| 20 |
-
return "
|
| 21 |
except sr.RequestError:
|
| 22 |
-
return "
|
| 23 |
|
| 24 |
-
# Function to translate text
|
| 25 |
-
def translate_text(text
|
| 26 |
-
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
# Function to convert text to speech
|
| 30 |
-
def text_to_speech(text
|
| 31 |
-
tts = gTTS(text, lang=lang)
|
| 32 |
-
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".mp3")
|
| 33 |
-
tts.save(temp_file.name)
|
| 34 |
-
return temp_file.name
|
| 35 |
-
|
| 36 |
-
# Route to handle voice input and translation
|
| 37 |
-
@app.route("/translate", methods=["POST"])
|
| 38 |
-
def translate_text(text, source_lang, target_lang):
|
| 39 |
try:
|
| 40 |
-
|
| 41 |
-
|
|
|
|
| 42 |
except Exception as e:
|
| 43 |
-
return f"
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
# Route to fetch the generated audio file
|
| 66 |
-
@app.route("/audio", methods=["GET"])
|
| 67 |
-
def get_audio():
|
| 68 |
-
audio_path = request.args.get("file")
|
| 69 |
-
return send_file(audio_path, mimetype="audio/mp3", as_attachment=True)
|
| 70 |
-
|
| 71 |
-
if __name__ == "__main__":
|
| 72 |
-
app.run(host="0.0.0.0", port=7860, debug=True)
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
import speech_recognition as sr
|
| 3 |
from deep_translator import GoogleTranslator
|
|
|
|
| 4 |
from gtts import gTTS
|
|
|
|
| 5 |
import os
|
| 6 |
|
| 7 |
+
# Function to convert speech to text
|
| 8 |
+
def speech_to_text(audio):
|
|
|
|
|
|
|
|
|
|
| 9 |
recognizer = sr.Recognizer()
|
| 10 |
+
with sr.AudioFile(audio) as source:
|
| 11 |
+
audio_data = recognizer.record(source)
|
| 12 |
try:
|
| 13 |
+
text = recognizer.recognize_google(audio_data, language="ur") # Urdu speech recognition
|
| 14 |
+
return text
|
| 15 |
except sr.UnknownValueError:
|
| 16 |
+
return "Sorry, could not understand the audio."
|
| 17 |
except sr.RequestError:
|
| 18 |
+
return "Error: Could not connect to the recognition service."
|
| 19 |
|
| 20 |
+
# Function to translate text from Urdu to Pashto
|
| 21 |
+
def translate_text(text):
|
| 22 |
+
try:
|
| 23 |
+
translated_text = GoogleTranslator(source="ur", target="ps").translate(text) # Urdu to Pashto
|
| 24 |
+
return translated_text
|
| 25 |
+
except Exception as e:
|
| 26 |
+
return f"Translation Error: {e}"
|
| 27 |
|
| 28 |
# Function to convert text to speech
|
| 29 |
+
def text_to_speech(text):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
try:
|
| 31 |
+
tts = gTTS(text, lang="ps") # Convert to Pashto speech
|
| 32 |
+
tts.save("translated_audio.mp3")
|
| 33 |
+
return "translated_audio.mp3"
|
| 34 |
except Exception as e:
|
| 35 |
+
return f"TTS Error: {e}"
|
| 36 |
+
|
| 37 |
+
# Main function to handle end-to-end processing
|
| 38 |
+
def process_voice(audio):
|
| 39 |
+
text = speech_to_text(audio)
|
| 40 |
+
if "Error" in text:
|
| 41 |
+
return text, None
|
| 42 |
+
translated_text = translate_text(text)
|
| 43 |
+
audio_path = text_to_speech(translated_text)
|
| 44 |
+
return translated_text, audio_path
|
| 45 |
+
|
| 46 |
+
# Create Gradio Interface for Hugging Face
|
| 47 |
+
iface = gr.Interface(
|
| 48 |
+
fn=process_voice,
|
| 49 |
+
inputs=gr.Audio(source="microphone", type="filepath"),
|
| 50 |
+
outputs=[gr.Textbox(label="Translated Text (Pashto)"), gr.Audio(label="Translated Speech")],
|
| 51 |
+
title="Voice-to-Voice Translator (Urdu → Pashto)",
|
| 52 |
+
description="🎤 Speak in **Urdu**, and get the **Pashto** translation along with audio output."
|
| 53 |
+
)
|
| 54 |
+
|
| 55 |
+
# Launch the app
|
| 56 |
+
iface.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|