Earnglow-2 / app.py
Raybilhelp's picture
Update app.py
3c39fc9 verified
Raw
History Blame
3.01 kB
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 """
<!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(...)):
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)