Spaces:
Paused
Paused
File size: 2,980 Bytes
3c39fc9 267e727 c342693 267e727 3c39fc9 267e727 c342693 3c39fc9 c342693 3c39fc9 c342693 267e727 3c39fc9 267e727 3c39fc9 c342693 3c39fc9 c342693 3c39fc9 3eb21eb 267e727 9a34b36 267e727 9a34b36 267e727 3c39fc9 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | import os
from fastapi import FastAPI, Form, HTTPException
from fastapi.responses import HTMLResponse, FileResponse
from huggingface_hub import InferenceClient
app = FastAPI()
# হাগিংফেস ক্লায়েন্ট ইনিশিয়ালাইজ করা
# স্পেসের Secrets থেকে টোকেন থাকলে নেবে, না থাকলে ফ্রি টিয়ারে কাজ করবে
HF_TOKEN = os.getenv("HF_TOKEN", "")
client = InferenceClient(token=HF_TOKEN if HF_TOKEN else None)
# আপনি চাইলে Kokoro বা Facebook এর যেকোনো মডেল এখানে দিতে পারেন
MODEL_ID = "facebook/mms-tts-eng"
@app.get("/", response_class=HTMLResponse)
def index():
return """
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Raybil Voice AI Engine</title>
<style>
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; }
h2 { color: #38bdf8; text-align: center; }
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; }
button { width: 100%; padding: 14px; background: #0284c7; color: white; border: none; border-radius: 6px; font-size: 16px; font-weight: bold; cursor: pointer; }
button:hover { background: #0369a1; }
</style>
</head>
<body>
<h2>🎙️ Raybil Voice AI Engine</h2>
<form action="/generate" method="post">
<label>আপনার টেক্সট লিখুন:</label>
<textarea name="text" placeholder="এখানে ইংরেজি টেক্সট লিখুন..." required></textarea>
<button type="submit">ভয়েস জেনারেট করুন</button>
</form>
</body>
</html>
"""
@app.post("/generate")
def generate_voice(text: str = Form(...)):
try:
# ক্লায়েন্ট অটোমেটিকলি সঠিক এন্ডপয়েন্ট এবং নেটওয়ার্ক পাথ হ্যান্ডেল করবে
audio_bytes = client.text_to_speech(text, model=MODEL_ID)
output_path = "output.wav"
with open(output_path, "wb") as f:
f.write(audio_bytes)
return FileResponse(output_path, media_type="audio/wav", filename="voice.wav")
except Exception as e:
raise HTTPException(status_code=500, detail=f"AI Engine Error: {str(e)}")
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=7860)
|