Raybilhelp commited on
Commit
3eb21eb
·
verified ·
1 Parent(s): 2425cb9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -9
app.py CHANGED
@@ -5,12 +5,14 @@ import requests
5
 
6
  app = FastAPI()
7
 
8
- # Hugging Face Inference API URL
9
- API_URL = "https://api-inference.huggingface.co/models/hexgrad/Kokoro-82M"
10
 
11
- # Hugging Face Space নিজের টোকেন অটোমেটিকলি রিড ারে দি Secrets-এ দো থাকে
12
  HF_TOKEN = os.getenv("HF_TOKEN", "")
13
- headers = {"Authorization": f"Bearer {HF_TOKEN}"} if HF_TOKEN else {}
 
 
14
 
15
  @app.get("/", response_class=HTMLResponse)
16
  def index():
@@ -43,18 +45,22 @@ def index():
43
  @app.post("/generate")
44
  def generate_voice(text: str = Form(...)):
45
  payload = {"inputs": text}
46
- response = requests.post(API_URL, headers=headers, json=payload)
 
 
 
 
47
 
48
  if response.status_code == 200:
49
- # Spaces-এ ফাইল রাইট করার জন্য /tmp ডিরেক্টরি ব্যবহার করা নিরাপদ ও স্ট্যান্ডার্ড
50
- output_path = "/tmp/output.mp3"
51
  with open(output_path, "wb") as f:
52
  f.write(response.content)
53
  return FileResponse(output_path, media_type="audio/mpeg", filename="voice.mp3")
54
  else:
55
- raise HTTPException(status_code=response.status_code, detail="Hugging Face API Error. Please check your Token or Request.")
 
 
56
 
57
  if __name__ == "__main__":
58
  import uvicorn
59
- # Hugging Face Spaces-এর রিকোয়ারমেন্ট অনুযায়ী অবশই পোর্ট ৭৮৬০ হতে হবে
60
  uvicorn.run(app, host="0.0.0.0", port=7860)
 
5
 
6
  app = FastAPI()
7
 
8
+ # Kokoro মডেলটি অনেক সময় ফ্রি এপিআই-তে ব্যস্ত থাকে, তাই স্ট্যান্ডার্ড Facebook-এর বাংলা ও ইংলিশ সাপোর্টেড TTS মডেল ব্যবহার করছি
9
+ API_URL = "https://api-inference.huggingface.co/models/facebook/mms-tts-eng"
10
 
11
+ # Hugging Face Spaces তার নিজের অ্যাকাউন্ের টোকেন অটোমেটিকলি HF_TOKEN এনভায়নমন্ট ভ্যারিয়বলে দিয়ে দেয়
12
  HF_TOKEN = os.getenv("HF_TOKEN", "")
13
+ headers = {}
14
+ if HF_TOKEN:
15
+ headers["Authorization"] = f"Bearer {HF_TOKEN}"
16
 
17
  @app.get("/", response_class=HTMLResponse)
18
  def index():
 
45
  @app.post("/generate")
46
  def generate_voice(text: str = Form(...)):
47
  payload = {"inputs": text}
48
+
49
+ try:
50
+ response = requests.post(API_URL, headers=headers, json=payload, timeout=30)
51
+ except Exception as e:
52
+ raise HTTPException(status_code=500, detail=f"Connection Error: {str(e)}")
53
 
54
  if response.status_code == 200:
55
+ output_path = "output.mp3"
 
56
  with open(output_path, "wb") as f:
57
  f.write(response.content)
58
  return FileResponse(output_path, media_type="audio/mpeg", filename="voice.mp3")
59
  else:
60
+ # এবার সার্ভার ক্র্যাশ না করে সরাসরি Hugging Face কী এরর দিচ্ছে তা স্ক্রিনে দেখাবে
61
+ error_detail = response.text if response.text else f"Status code: {response.status_code}"
62
+ raise HTTPException(status_code=response.status_code, detail=f"HF API Error: {error_detail}")
63
 
64
  if __name__ == "__main__":
65
  import uvicorn
 
66
  uvicorn.run(app, host="0.0.0.0", port=7860)