Spaces:
Sleeping
Sleeping
File size: 1,617 Bytes
1d86133 c2d7887 a4f74bd 1d86133 a4f74bd 1d86133 9224c83 1d86133 a4f74bd 1d86133 a4f74bd 1d86133 a4f74bd 1d86133 a4f74bd c2d7887 1d86133 a4f74bd c2d7887 1d86133 a4f74bd | 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 58 59 60 61 62 63 64 65 | from fastapi import FastAPI, File, UploadFile
from transformers import pipeline
from gtts import gTTS
import tempfile
import os
import uvicorn
import subprocess
app = FastAPI()
# 🎤 Speech → Text (Whisper) — نجبره على العربية
stt = pipeline(
"automatic-speech-recognition",
model="openai/whisper-tiny",
generate_kwargs={"language": "arabic"}
)
# 🧠 Text → Text (Chat)
chat = pipeline(
"text2text-generation",
model="google/flan-t5-base"
)
@app.get("/")
def root():
return {"status": "ok"}
@app.post("/voice")
async def voice(file: UploadFile = File(...)):
# 1️⃣ حفظ الملف الصوتي كما جاء (m4a / mp3 / wav)
with tempfile.NamedTemporaryFile(delete=False) as f:
f.write(await file.read())
audio_in = f.name
# 2️⃣ تحويل أي صوت إلى WAV (حلّ مشاكل الهاتف)
audio_wav = audio_in + ".wav"
subprocess.run(
["ffmpeg", "-y", "-i", audio_in, audio_wav],
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL
)
# 3️⃣ Speech → Text
text_in = stt(audio_wav)["text"]
# 4️⃣ Chat response
reply = chat(text_in, max_new_tokens=80)[0]["generated_text"]
# 5️⃣ Text → Speech (عربي)
audio_out = audio_in + "_reply.mp3"
tts = gTTS(reply, lang="ar")
tts.save(audio_out)
return {
"heard_text": text_in,
"reply_text": reply,
"audio_file": audio_out
}
if __name__ == "__main__":
uvicorn.run(
app,
host="0.0.0.0",
port=int(os.environ.get("PORT", 7860))
) |