Spaces:
Paused
Paused
Update app.py
Browse files
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 |
-
|
| 10 |
-
|
| 11 |
-
# হাগিংফেস স্পেসের নিজস্ব পরিবেশ থেকে টোকেন নেওয়া
|
| 12 |
HF_TOKEN = os.getenv("HF_TOKEN", "")
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
|
|
|
| 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;
|
| 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>
|
|
@@ -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 |
-
|
| 52 |
-
|
| 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 |
-
|
| 59 |
-
|
| 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 |
-
|
| 67 |
-
|
| 68 |
-
|
| 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
|