HusseinBashir commited on
Commit
216406c
·
verified ·
1 Parent(s): 96d3b2a

Create main.py

Browse files
Files changed (1) hide show
  1. main.py +23 -0
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()}