File size: 1,919 Bytes
956a6f4
76dd525
8475806
76dd525
a8a13af
76dd525
956a6f4
 
76dd525
956a6f4
 
f2fb0a3
956a6f4
 
f2fb0a3
956a6f4
f2fb0a3
956a6f4
f2fb0a3
956a6f4
 
 
 
 
 
 
76dd525
f2fb0a3
956a6f4
8475806
956a6f4
 
 
8475806
956a6f4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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()