Sapna36 commited on
Commit
956a6f4
·
verified ·
1 Parent(s): aec7832

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -58
app.py CHANGED
@@ -1,72 +1,56 @@
1
- from flask import Flask, request, jsonify, send_file
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
- app = Flask(__name__)
10
- translator = Translator()
11
-
12
- # Function to recognize speech from an audio file
13
- def recognize_speech(audio_file):
14
  recognizer = sr.Recognizer()
15
- with sr.AudioFile(audio_file) as source:
16
- audio = recognizer.record(source)
17
  try:
18
- return recognizer.recognize_google(audio, language="ps") # Default language: Pashto
 
19
  except sr.UnknownValueError:
20
- return "Speech not recognized"
21
  except sr.RequestError:
22
- return "Speech recognition service unavailable"
23
 
24
- # Function to translate text using googletrans
25
- def translate_text(text, source_lang, target_lang):
26
- translation = translator.translate(text, src=source_lang, dest=target_lang)
27
- return translation.text
 
 
 
28
 
29
  # Function to convert text to speech
30
- def text_to_speech(text, lang):
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
- translation = GoogleTranslator(source=source_lang, target=target_lang).translate(text)
41
- return translation
 
42
  except Exception as e:
43
- return f"Translation Error: {str(e)}"
44
-
45
-
46
- # Save audio file temporarily
47
- temp_audio = tempfile.NamedTemporaryFile(delete=False, suffix=".wav")
48
- audio_file.save(temp_audio.name)
49
-
50
- # Recognize speech
51
- text = recognize_speech(temp_audio.name)
52
- os.remove(temp_audio.name) # Delete after processing
53
-
54
- if text == "Speech not recognized" or text == "Speech recognition service unavailable":
55
- return jsonify({"error": text}), 500
56
-
57
- # Translate from Pashto to Urdu
58
- translated_text = translate_text(text, "ps", "ur")
59
-
60
- # Convert to speech
61
- audio_path = text_to_speech(translated_text, "ur")
62
-
63
- return jsonify({"original_text": text, "translated_text": translated_text, "audio_path": audio_path})
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()