Spaces:
Sleeping
Sleeping
Create main.py
Browse files
main.py
ADDED
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import FastAPI
|
2 |
+
from pydantic import BaseModel
|
3 |
+
import requests
|
4 |
+
import base64
|
5 |
+
import os
|
6 |
+
|
7 |
+
app = FastAPI()
|
8 |
+
|
9 |
+
HUGGINGFACE_API_URL = "https://api-inference.huggingface.co/models/Somali-tts/somali_tts_model"
|
10 |
+
HUGGINGFACE_API_TOKEN = f"Bearer {os.environ['HF_TOKEN']}" # Token-ka laga helayo environment
|
11 |
+
headers = {"Authorization": HUGGINGFACE_API_TOKEN}
|
12 |
+
|
13 |
+
class TextInput(BaseModel):
|
14 |
+
text: str
|
15 |
+
|
16 |
+
@app.post("/tts/")
|
17 |
+
def generate_audio(input: TextInput):
|
18 |
+
response = requests.post(HUGGINGFACE_API_URL, headers=headers, json={"inputs": input.text})
|
19 |
+
if response.status_code == 200:
|
20 |
+
audio_base64 = base64.b64encode(response.content).decode("utf-8")
|
21 |
+
return {"audio_base64": audio_base64}
|
22 |
+
else:
|
23 |
+
return {"error": response.json()}
|