mohamedtsou commited on
Commit
a4f74bd
·
verified ·
1 Parent(s): 9224c83

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -12
app.py CHANGED
@@ -1,18 +1,21 @@
1
  from fastapi import FastAPI, File, UploadFile
2
  from transformers import pipeline
3
  from gtts import gTTS
4
- import tempfile, os, uvicorn
 
 
 
5
 
6
  app = FastAPI()
7
 
8
- # 🎤 صوتنص (عربي)
9
  stt = pipeline(
10
  "automatic-speech-recognition",
11
  model="openai/whisper-tiny",
12
  generate_kwargs={"language": "arabic"}
13
  )
14
 
15
- # 🧠 نصجواب
16
  chat = pipeline(
17
  "text2text-generation",
18
  model="google/flan-t5-base"
@@ -24,19 +27,27 @@ def root():
24
 
25
  @app.post("/voice")
26
  async def voice(file: UploadFile = File(...)):
27
- # حفظ الصوت
28
- with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as f:
29
  f.write(await file.read())
30
  audio_in = f.name
31
 
32
- # 1) STT
33
- text_in = stt(audio_in)["text"]
 
 
 
 
 
34
 
35
- # 2) Chat
 
 
 
36
  reply = chat(text_in, max_new_tokens=80)[0]["generated_text"]
37
 
38
- # 3) TTS عربي (gTTS)
39
- audio_out = audio_in.replace(".wav", "_reply.mp3")
40
  tts = gTTS(reply, lang="ar")
41
  tts.save(audio_out)
42
 
@@ -47,5 +58,8 @@ async def voice(file: UploadFile = File(...)):
47
  }
48
 
49
  if __name__ == "__main__":
50
- uvicorn.run(app, host="0.0.0.0",
51
- port=int(os.environ.get("PORT", 7860)))
 
 
 
 
1
  from fastapi import FastAPI, File, UploadFile
2
  from transformers import pipeline
3
  from gtts import gTTS
4
+ import tempfile
5
+ import os
6
+ import uvicorn
7
+ import subprocess
8
 
9
  app = FastAPI()
10
 
11
+ # 🎤 SpeechText (Whisper) — نجبره على العربية
12
  stt = pipeline(
13
  "automatic-speech-recognition",
14
  model="openai/whisper-tiny",
15
  generate_kwargs={"language": "arabic"}
16
  )
17
 
18
+ # 🧠 TextText (Chat)
19
  chat = pipeline(
20
  "text2text-generation",
21
  model="google/flan-t5-base"
 
27
 
28
  @app.post("/voice")
29
  async def voice(file: UploadFile = File(...)):
30
+ # 1️⃣ حفظ الملف الصوتي كما جاء (m4a / mp3 / wav)
31
+ with tempfile.NamedTemporaryFile(delete=False) as f:
32
  f.write(await file.read())
33
  audio_in = f.name
34
 
35
+ # 2️⃣ تحويل أي صوت إلى WAV (حلّ مشاكل الهاتف)
36
+ audio_wav = audio_in + ".wav"
37
+ subprocess.run(
38
+ ["ffmpeg", "-y", "-i", audio_in, audio_wav],
39
+ stdout=subprocess.DEVNULL,
40
+ stderr=subprocess.DEVNULL
41
+ )
42
 
43
+ # 3️⃣ Speech → Text
44
+ text_in = stt(audio_wav)["text"]
45
+
46
+ # 4️⃣ Chat response
47
  reply = chat(text_in, max_new_tokens=80)[0]["generated_text"]
48
 
49
+ # 5️⃣ Text → Speech (عربي)
50
+ audio_out = audio_in + "_reply.mp3"
51
  tts = gTTS(reply, lang="ar")
52
  tts.save(audio_out)
53
 
 
58
  }
59
 
60
  if __name__ == "__main__":
61
+ uvicorn.run(
62
+ app,
63
+ host="0.0.0.0",
64
+ port=int(os.environ.get("PORT", 7860))
65
+ )