import os
from fastapi import FastAPI, Form, HTTPException
from fastapi.responses import HTMLResponse, FileResponse
import requests
app = FastAPI()
# Hugging Face Inference API URL
API_URL = "https://api-inference.huggingface.co/models/hexgrad/Kokoro-82M"
# Hugging Face Space নিজের টোকেন অটোমেটিকলি রিড করতে পারে যদি Secrets-এ দেওয়া থাকে
HF_TOKEN = os.getenv("HF_TOKEN", "")
headers = {"Authorization": f"Bearer {HF_TOKEN}"} if HF_TOKEN else {}
@app.get("/", response_class=HTMLResponse)
def index():
return """
Raybil Voice AI Engine
🎙️ Raybil Voice AI Engine
"""
@app.post("/generate")
def generate_voice(text: str = Form(...)):
payload = {"inputs": text}
response = requests.post(API_URL, headers=headers, json=payload)
if response.status_code == 200:
# Spaces-এ ফাইল রাইট করার জন্য /tmp ডিরেক্টরি ব্যবহার করা নিরাপদ ও স্ট্যান্ডার্ড
output_path = "/tmp/output.mp3"
with open(output_path, "wb") as f:
f.write(response.content)
return FileResponse(output_path, media_type="audio/mpeg", filename="voice.mp3")
else:
raise HTTPException(status_code=response.status_code, detail="Hugging Face API Error. Please check your Token or Request.")
if __name__ == "__main__":
import uvicorn
# Hugging Face Spaces-এর রিকোয়ারমেন্ট অনুযায়ী অবশই পোর্ট ৭৮৬০ হতে হবে
uvicorn.run(app, host="0.0.0.0", port=7860)