Raybilhelp commited on
Commit
267e727
·
verified ·
1 Parent(s): 6a18867

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -30
app.py CHANGED
@@ -1,18 +1,17 @@
1
  import os
2
- import requests
3
  from fastapi import FastAPI, Form, HTTPException
4
  from fastapi.responses import HTMLResponse, FileResponse
 
5
 
6
  app = FastAPI()
7
 
8
- # একটি অত্যন্ত ্ট এবং সটেব-টু-স্পিচ মডে
9
- API_URL = "https://api-inference.huggingface.co/models/facebook/mms-tts-eng"
10
-
11
- # হাগিংফেস স্পেসের নিজস্ব পরিবেশ থেকে টোকেন নেওয়া
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():
@@ -24,10 +23,10 @@ def index():
24
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
25
  <title>Raybil Voice AI Engine</title>
26
  <style>
27
- body { font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; max-width: 600px; margin: 40px auto; padding: 30px; background: #0f172a; color: #f8fafc; border-radius: 12px; box-shadow: 0 10px 15px -3px rgba(0,0,0,0.3); }
28
  h2 { color: #38bdf8; text-align: center; }
29
  textarea { width: 100%; height: 120px; padding: 12px; margin: 15px 0; background: #1e293b; color: #fff; border: 1px solid #475569; border-radius: 6px; box-sizing: border-box; resize: none; }
30
- button { width: 100%; padding: 14px; background: #0284c7; color: white; border: none; border-radius: 6px; font-size: 16px; font-weight: bold; cursor: pointer; transition: 0.2s; }
31
  button:hover { background: #0369a1; }
32
  </style>
33
  </head>
@@ -44,29 +43,18 @@ def index():
44
 
45
  @app.post("/generate")
46
  def generate_voice(text: str = Form(...)):
47
- payload = {"inputs": text}
48
-
49
  try:
50
- # হাগিংফেস সাররকেসপন্ করার য প্যাপ্ত সময় (৬০ সেকেন্ড) দওয়া হ
51
- response = requests.post(API_URL, headers=headers, json=payload, timeout=60)
52
- except Exception as e:
53
- raise HTTPException(status_code=500, detail=f"Network Timeout/Error: {str(e)}")
54
-
55
- if response.status_code == 200:
56
- # ফেসবুকের এই মডেলগুলো .wav ফরম্যাটে অডিও দেয়, তাই আমরা সেফ ফরম্যাট ব্যবহার করছি
57
  output_path = "output.wav"
58
- try:
59
- with open(output_path, "wb") as f:
60
- f.write(response.content)
61
- return FileResponse(output_path, media_type="audio/wav", filename="voice.wav")
62
- except Exception as e:
63
- raise HTTPException(status_code=500, detail=f"File System Error: {str(e)}")
64
 
65
- # যদি মডেলটি হাগিংফেস সার্ভারে লোড হতে থাকে, তবে এটি ৫৩৩ বা ২০২ কোড দেয়
66
- elif response.status_code in [202, 503]:
67
- raise HTTPException(status_code=response.status_code, detail="মডেলটি হাগিংফেস সার্ভারে লোড হচ্ছে। অনুগ্রহ করে ৩০ সেকেন্ড পর আবার চেষ্টা করুন।")
68
- else:
69
- raise HTTPException(status_code=response.status_code, detail=f"HF Server returned error: {response.text}")
70
 
71
  if __name__ == "__main__":
72
  import uvicorn
 
1
  import os
 
2
  from fastapi import FastAPI, Form, HTTPException
3
  from fastapi.responses import HTMLResponse, FileResponse
4
+ from huggingface_hub import InferenceClient
5
 
6
  app = FastAPI()
7
 
8
+ # হাগি্লায়্ট ইনিশিয়াাইজ করা
9
+ # স্পেসের Secrets থেকে টোকেন থাকলে নেবে, না থাকলে ফ্রি টিয়ারে কাজ করবে
 
 
10
  HF_TOKEN = os.getenv("HF_TOKEN", "")
11
+ client = InferenceClient(token=HF_TOKEN if HF_TOKEN else None)
12
+
13
+ # আপনি চাইলে Kokoro বা Facebook এর যেকোনো মডেল এখানে দিতে পারেন
14
+ MODEL_ID = "facebook/mms-tts-eng"
15
 
16
  @app.get("/", response_class=HTMLResponse)
17
  def index():
 
23
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
24
  <title>Raybil Voice AI Engine</title>
25
  <style>
26
+ body { font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; max-width: 600px; margin: 40px auto; padding: 30px; background: #0f172a; color: #f8fafc; border-radius: 12px; }
27
  h2 { color: #38bdf8; text-align: center; }
28
  textarea { width: 100%; height: 120px; padding: 12px; margin: 15px 0; background: #1e293b; color: #fff; border: 1px solid #475569; border-radius: 6px; box-sizing: border-box; resize: none; }
29
+ button { width: 100%; padding: 14px; background: #0284c7; color: white; border: none; border-radius: 6px; font-size: 16px; font-weight: bold; cursor: pointer; }
30
  button:hover { background: #0369a1; }
31
  </style>
32
  </head>
 
43
 
44
  @app.post("/generate")
45
  def generate_voice(text: str = Form(...)):
 
 
46
  try:
47
+ # য়ন্ট অটোমটিকলি ঠিক এন্ডয়েন্ এবংেটও়ার্কাথ হ্যান্ডেল করবে
48
+ audio_bytes = client.text_to_speech(text, model=MODEL_ID)
49
+
 
 
 
 
50
  output_path = "output.wav"
51
+ with open(output_path, "wb") as f:
52
+ f.write(audio_bytes)
 
 
 
 
53
 
54
+ return FileResponse(output_path, media_type="audio/wav", filename="voice.wav")
55
+
56
+ except Exception as e:
57
+ raise HTTPException(status_code=500, detail=f"AI Engine Error: {str(e)}")
 
58
 
59
  if __name__ == "__main__":
60
  import uvicorn