Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,14 +1,14 @@
|
|
| 1 |
import os
|
|
|
|
| 2 |
from fastapi import FastAPI, Form, HTTPException
|
| 3 |
from fastapi.responses import HTMLResponse, FileResponse
|
| 4 |
-
import requests
|
| 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:
|
|
@@ -24,10 +24,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; }
|
| 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; }
|
| 31 |
button:hover { background: #0369a1; }
|
| 32 |
</style>
|
| 33 |
</head>
|
|
@@ -47,19 +47,26 @@ def generate_voice(text: str = Form(...)):
|
|
| 47 |
payload = {"inputs": text}
|
| 48 |
|
| 49 |
try:
|
| 50 |
-
|
|
|
|
| 51 |
except Exception as e:
|
| 52 |
-
raise HTTPException(status_code=500, detail=f"
|
| 53 |
|
| 54 |
if response.status_code == 200:
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
else:
|
| 60 |
-
|
| 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
|
|
|
|
| 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:
|
|
|
|
| 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>
|
|
|
|
| 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
|