translator / app.py
Sapna36's picture
Update app.py
956a6f4 verified
import gradio as gr
import speech_recognition as sr
from deep_translator import GoogleTranslator
from gtts import gTTS
import os
# Function to convert speech to text
def speech_to_text(audio):
recognizer = sr.Recognizer()
with sr.AudioFile(audio) as source:
audio_data = recognizer.record(source)
try:
text = recognizer.recognize_google(audio_data, language="ur") # Urdu speech recognition
return text
except sr.UnknownValueError:
return "Sorry, could not understand the audio."
except sr.RequestError:
return "Error: Could not connect to the recognition service."
# Function to translate text from Urdu to Pashto
def translate_text(text):
try:
translated_text = GoogleTranslator(source="ur", target="ps").translate(text) # Urdu to Pashto
return translated_text
except Exception as e:
return f"Translation Error: {e}"
# Function to convert text to speech
def text_to_speech(text):
try:
tts = gTTS(text, lang="ps") # Convert to Pashto speech
tts.save("translated_audio.mp3")
return "translated_audio.mp3"
except Exception as e:
return f"TTS Error: {e}"
# Main function to handle end-to-end processing
def process_voice(audio):
text = speech_to_text(audio)
if "Error" in text:
return text, None
translated_text = translate_text(text)
audio_path = text_to_speech(translated_text)
return translated_text, audio_path
# Create Gradio Interface for Hugging Face
iface = gr.Interface(
fn=process_voice,
inputs=gr.Audio(source="microphone", type="filepath"),
outputs=[gr.Textbox(label="Translated Text (Pashto)"), gr.Audio(label="Translated Speech")],
title="Voice-to-Voice Translator (Urdu → Pashto)",
description="🎤 Speak in **Urdu**, and get the **Pashto** translation along with audio output."
)
# Launch the app
iface.launch()