Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,59 +1,69 @@
|
|
| 1 |
-
import
|
| 2 |
-
import sounddevice as sd
|
| 3 |
-
import numpy as np
|
| 4 |
-
import wavio
|
| 5 |
import speech_recognition as sr
|
| 6 |
-
from
|
| 7 |
from gtts import gTTS
|
| 8 |
import tempfile
|
| 9 |
import os
|
| 10 |
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 14 |
-
model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
|
| 15 |
-
translator = pipeline("translation", model=model, tokenizer=tokenizer)
|
| 16 |
-
|
| 17 |
-
def record_audio(duration=5, samplerate=44100):
|
| 18 |
-
st.write("🎤 Recording...")
|
| 19 |
-
audio = sd.rec(int(duration * samplerate), samplerate=samplerate, channels=2, dtype=np.int16)
|
| 20 |
-
sd.wait()
|
| 21 |
-
temp_audio_path = "recorded_audio.wav"
|
| 22 |
-
wavio.write(temp_audio_path, audio, samplerate, sampwidth=2)
|
| 23 |
-
return temp_audio_path
|
| 24 |
|
|
|
|
| 25 |
def recognize_speech(audio_file):
|
| 26 |
recognizer = sr.Recognizer()
|
| 27 |
with sr.AudioFile(audio_file) as source:
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
|
|
|
| 37 |
def text_to_speech(text, lang):
|
| 38 |
-
tts = gTTS(text
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
return temp_audio.name
|
| 43 |
|
| 44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
-
|
| 47 |
-
audio_path = record_audio(duration=5)
|
| 48 |
-
recognized_text = recognize_speech(audio_path)
|
| 49 |
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
else:
|
| 54 |
-
response = translator(recognized_text)[0]["translation_text"]
|
| 55 |
-
response_audio = text_to_speech(response, "ps")
|
| 56 |
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from flask import Flask, request, jsonify, send_file
|
|
|
|
|
|
|
|
|
|
| 2 |
import speech_recognition as sr
|
| 3 |
+
from googletrans import Translator
|
| 4 |
from gtts import gTTS
|
| 5 |
import tempfile
|
| 6 |
import os
|
| 7 |
|
| 8 |
+
app = Flask(__name__)
|
| 9 |
+
translator = Translator()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
+
# Function to recognize speech from an audio file
|
| 12 |
def recognize_speech(audio_file):
|
| 13 |
recognizer = sr.Recognizer()
|
| 14 |
with sr.AudioFile(audio_file) as source:
|
| 15 |
+
audio = recognizer.record(source)
|
| 16 |
+
try:
|
| 17 |
+
return recognizer.recognize_google(audio, language="ps") # Default language: Pashto
|
| 18 |
+
except sr.UnknownValueError:
|
| 19 |
+
return "Speech not recognized"
|
| 20 |
+
except sr.RequestError:
|
| 21 |
+
return "Speech recognition service unavailable"
|
| 22 |
+
|
| 23 |
+
# Function to translate text using googletrans
|
| 24 |
+
def translate_text(text, source_lang, target_lang):
|
| 25 |
+
translation = translator.translate(text, src=source_lang, dest=target_lang)
|
| 26 |
+
return translation.text
|
| 27 |
|
| 28 |
+
# Function to convert text to speech
|
| 29 |
def text_to_speech(text, lang):
|
| 30 |
+
tts = gTTS(text, lang=lang)
|
| 31 |
+
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".mp3")
|
| 32 |
+
tts.save(temp_file.name)
|
| 33 |
+
return temp_file.name
|
|
|
|
| 34 |
|
| 35 |
+
# Route to handle voice input and translation
|
| 36 |
+
@app.route("/translate", methods=["POST"])
|
| 37 |
+
def translate():
|
| 38 |
+
if "audio" not in request.files:
|
| 39 |
+
return jsonify({"error": "No audio file uploaded"}), 400
|
| 40 |
|
| 41 |
+
audio_file = request.files["audio"]
|
|
|
|
|
|
|
| 42 |
|
| 43 |
+
# Save audio file temporarily
|
| 44 |
+
temp_audio = tempfile.NamedTemporaryFile(delete=False, suffix=".wav")
|
| 45 |
+
audio_file.save(temp_audio.name)
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
+
# Recognize speech
|
| 48 |
+
text = recognize_speech(temp_audio.name)
|
| 49 |
+
os.remove(temp_audio.name) # Delete after processing
|
| 50 |
+
|
| 51 |
+
if text == "Speech not recognized" or text == "Speech recognition service unavailable":
|
| 52 |
+
return jsonify({"error": text}), 500
|
| 53 |
+
|
| 54 |
+
# Translate from Pashto to Urdu
|
| 55 |
+
translated_text = translate_text(text, "ps", "ur")
|
| 56 |
+
|
| 57 |
+
# Convert to speech
|
| 58 |
+
audio_path = text_to_speech(translated_text, "ur")
|
| 59 |
+
|
| 60 |
+
return jsonify({"original_text": text, "translated_text": translated_text, "audio_path": audio_path})
|
| 61 |
+
|
| 62 |
+
# Route to fetch the generated audio file
|
| 63 |
+
@app.route("/audio", methods=["GET"])
|
| 64 |
+
def get_audio():
|
| 65 |
+
audio_path = request.args.get("file")
|
| 66 |
+
return send_file(audio_path, mimetype="audio/mp3", as_attachment=True)
|
| 67 |
+
|
| 68 |
+
if __name__ == "__main__":
|
| 69 |
+
app.run(host="0.0.0.0", port=7860, debug=True)
|