tudeplom commited on
Commit
dda3962
·
verified ·
1 Parent(s): d0d2610

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -9
app.py CHANGED
@@ -6,45 +6,55 @@ import os
6
  # Khởi tạo FastAPI
7
  app = FastAPI()
8
 
9
- # Lấy API key từ biến môi trường (Không lộ API key)
10
  HF_API_KEY = os.getenv("HF_API_KEY")
11
  if not HF_API_KEY:
12
- raise ValueError("Missing HF_API_KEY. Add it to Hugging Face Spaces Secrets.")
13
 
 
14
  client = InferenceClient(token=HF_API_KEY)
15
 
16
- # Thư mục lưu file tạm thời
17
  TEMP_DIR = "temp"
18
  os.makedirs(TEMP_DIR, exist_ok=True)
19
 
20
- # Mô hình TTS & STT tối ưu CPU
21
  STT_MODEL = "openai/whisper-tiny.en"
22
  TTS_MODEL = "facebook/mms-tts-eng"
23
 
24
- # API TTS: Chuyển văn bản thành giọng nói
25
  @app.post("/tts")
26
  async def text_to_speech(text: str):
27
  output_path = os.path.join(TEMP_DIR, "output.wav")
28
-
29
  try:
 
30
  audio = client.text_to_speech(model=TTS_MODEL, inputs=text)
31
  with open(output_path, "wb") as f:
32
  f.write(audio)
33
  return FileResponse(output_path, media_type="audio/wav", filename="output.wav")
34
  except Exception as e:
 
35
  return {"error": str(e)}
36
 
37
- # API STT: Chuyển giọng nói thành văn bản
38
  @app.post("/stt")
39
  async def speech_to_text(file: UploadFile = File(...)):
40
  try:
 
41
  audio_data = await file.read()
42
  text = client.automatic_speech_recognition(model=STT_MODEL, data=audio_data)
43
  return {"text": text}
44
  except Exception as e:
 
45
  return {"error": str(e)}
46
 
47
- # Kiểm tra API đang chạy
48
  @app.get("/")
49
  async def root():
50
- return {"message": "TTS & STT API is running!"}
 
 
 
 
 
 
 
6
  # Khởi tạo FastAPI
7
  app = FastAPI()
8
 
9
+ # Lấy API key từ biến môi trường
10
  HF_API_KEY = os.getenv("HF_API_KEY")
11
  if not HF_API_KEY:
12
+ raise ValueError("Missing HF_API_KEY! Add it to Hugging Face Spaces Secrets.")
13
 
14
+ # Khởi tạo Hugging Face Client
15
  client = InferenceClient(token=HF_API_KEY)
16
 
17
+ # Tạo thư mục lưu file tạm
18
  TEMP_DIR = "temp"
19
  os.makedirs(TEMP_DIR, exist_ok=True)
20
 
21
+ # Mô hình TTS & STT
22
  STT_MODEL = "openai/whisper-tiny.en"
23
  TTS_MODEL = "facebook/mms-tts-eng"
24
 
25
+ # 🗣 API TTS: Chuyển văn bản thành giọng nói
26
  @app.post("/tts")
27
  async def text_to_speech(text: str):
28
  output_path = os.path.join(TEMP_DIR, "output.wav")
 
29
  try:
30
+ print(f"🔄 Đang xử lý TTS với model {TTS_MODEL}...") # Debug log
31
  audio = client.text_to_speech(model=TTS_MODEL, inputs=text)
32
  with open(output_path, "wb") as f:
33
  f.write(audio)
34
  return FileResponse(output_path, media_type="audio/wav", filename="output.wav")
35
  except Exception as e:
36
+ print(f"❌ Lỗi TTS: {e}")
37
  return {"error": str(e)}
38
 
39
+ # 🎙 API STT: Chuyển giọng nói thành văn bản
40
  @app.post("/stt")
41
  async def speech_to_text(file: UploadFile = File(...)):
42
  try:
43
+ print(f"🔄 Đang xử lý STT với model {STT_MODEL}...") # Debug log
44
  audio_data = await file.read()
45
  text = client.automatic_speech_recognition(model=STT_MODEL, data=audio_data)
46
  return {"text": text}
47
  except Exception as e:
48
+ print(f"❌ Lỗi STT: {e}")
49
  return {"error": str(e)}
50
 
51
+ # Kiểm tra API
52
  @app.get("/")
53
  async def root():
54
+ return {"message": "TTS & STT API is running!"}
55
+
56
+ # Chạy Uvicorn (tránh lỗi "application does not seem to be initialized")
57
+ if __name__ == "__main__":
58
+ import uvicorn
59
+ print("🚀 Khởi động FastAPI Server...")
60
+ uvicorn.run(app, host="0.0.0.0", port=7860)