Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,12 +5,14 @@ import requests
|
|
| 5 |
|
| 6 |
app = FastAPI()
|
| 7 |
|
| 8 |
-
#
|
| 9 |
-
API_URL = "https://api-inference.huggingface.co/models/
|
| 10 |
|
| 11 |
-
# Hugging Face
|
| 12 |
HF_TOKEN = os.getenv("HF_TOKEN", "")
|
| 13 |
-
headers = {
|
|
|
|
|
|
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
if response.status_code == 200:
|
| 49 |
-
|
| 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 |
-
|
|
|
|
|
|
|
| 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)
|