Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -0,0 +1,93 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import requests
|
| 3 |
+
import whisper
|
| 4 |
+
import os
|
| 5 |
+
import sounddevice as sd
|
| 6 |
+
import wave
|
| 7 |
+
import numpy as np
|
| 8 |
+
from fastapi import FastAPI, UploadFile, File
|
| 9 |
+
from fastapi.responses import FileResponse
|
| 10 |
+
from gtts import gTTS
|
| 11 |
+
import uvicorn
|
| 12 |
+
|
| 13 |
+
app = FastAPI()
|
| 14 |
+
|
| 15 |
+
# Load Whisper model
|
| 16 |
+
model = whisper.load_model("base")
|
| 17 |
+
|
| 18 |
+
# LibreTranslate API (Open Source)
|
| 19 |
+
TRANSLATE_API_URL = "https://libretranslate.com/translate"
|
| 20 |
+
|
| 21 |
+
# Record audio
|
| 22 |
+
AUDIO_FILE = "recorded_audio.wav"
|
| 23 |
+
OUTPUT_AUDIO = "output.mp3"
|
| 24 |
+
|
| 25 |
+
def record_audio(duration=5, samplerate=44100):
|
| 26 |
+
print("Recording...")
|
| 27 |
+
audio_data = sd.rec(int(duration * samplerate), samplerate=samplerate, channels=2, dtype=np.int16)
|
| 28 |
+
sd.wait()
|
| 29 |
+
print("Recording Stopped")
|
| 30 |
+
|
| 31 |
+
with wave.open(AUDIO_FILE, "wb") as wf:
|
| 32 |
+
wf.setnchannels(2)
|
| 33 |
+
wf.setsampwidth(2)
|
| 34 |
+
wf.setframerate(samplerate)
|
| 35 |
+
wf.writeframes(audio_data.tobytes())
|
| 36 |
+
|
| 37 |
+
return AUDIO_FILE
|
| 38 |
+
|
| 39 |
+
# Transcribe speech
|
| 40 |
+
def transcribe_audio(file_path):
|
| 41 |
+
result = model.transcribe(file_path)
|
| 42 |
+
return result["text"]
|
| 43 |
+
|
| 44 |
+
# Translate text
|
| 45 |
+
def translate_text(text, source_lang, target_lang):
|
| 46 |
+
response = requests.post(TRANSLATE_API_URL, data={"q": text, "source": source_lang, "target": target_lang})
|
| 47 |
+
return response.json().get("translatedText", "Translation Error")
|
| 48 |
+
|
| 49 |
+
# Convert text to speech
|
| 50 |
+
def text_to_speech(text, lang):
|
| 51 |
+
tts = gTTS(text=text, lang=lang)
|
| 52 |
+
tts.save(OUTPUT_AUDIO)
|
| 53 |
+
return OUTPUT_AUDIO
|
| 54 |
+
|
| 55 |
+
@app.post("/upload-audio/")
|
| 56 |
+
async def upload_audio(file: UploadFile = File(...)):
|
| 57 |
+
file_path = f"temp/{file.filename}"
|
| 58 |
+
with open(file_path, "wb") as audio_file:
|
| 59 |
+
audio_file.write(await file.read())
|
| 60 |
+
|
| 61 |
+
detected_text = transcribe_audio(file_path)
|
| 62 |
+
source_lang = "ur" if any(char in detected_text for char in "اآبپتثجچحخ") else "ps"
|
| 63 |
+
target_lang = "ps" if source_lang == "ur" else "ur"
|
| 64 |
+
|
| 65 |
+
translated_text = translate_text(detected_text, source_lang, target_lang)
|
| 66 |
+
text_to_speech(translated_text, target_lang)
|
| 67 |
+
|
| 68 |
+
return {"text": detected_text, "translated_text": translated_text, "audio": "/get-audio/"}
|
| 69 |
+
|
| 70 |
+
@app.get("/get-audio/")
|
| 71 |
+
def get_audio():
|
| 72 |
+
return FileResponse(OUTPUT_AUDIO, media_type="audio/mpeg")
|
| 73 |
+
|
| 74 |
+
# Gradio UI
|
| 75 |
+
def process_audio():
|
| 76 |
+
files = {"file": open(AUDIO_FILE, "rb")}
|
| 77 |
+
response = requests.post("http://127.0.0.1:8000/upload-audio/", files=files).json()
|
| 78 |
+
return response["text"], response["translated_text"], response["audio"]
|
| 79 |
+
|
| 80 |
+
with gr.Blocks() as demo:
|
| 81 |
+
gr.Markdown("### 🎙️ Urdu ↔ Pashto Voice Translator")
|
| 82 |
+
record_btn = gr.Button("🎤 Record")
|
| 83 |
+
stop_btn = gr.Button("⏹️ Stop")
|
| 84 |
+
translate_btn = gr.Button("🔄 Translate")
|
| 85 |
+
transcribed_text = gr.Textbox(label="Transcribed Text")
|
| 86 |
+
translated_text = gr.Textbox(label="Translated Text")
|
| 87 |
+
audio_output = gr.Audio(label="Translated Audio")
|
| 88 |
+
|
| 89 |
+
record_btn.click(fn=record_audio, inputs=[], outputs=[])
|
| 90 |
+
stop_btn.click(fn=lambda: None, inputs=[], outputs=[])
|
| 91 |
+
translate_btn.click(fn=process_audio, inputs=[], outputs=[transcribed_text, translated_text, audio_output])
|
| 92 |
+
|
| 93 |
+
demo.launch()
|