Spaces:
Paused
Paused
| 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" | |
| 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> | |
| """ | |
| 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) | |