Spaces:
Sleeping
Sleeping
Convert flask app into fastAPI
Browse files
app.py
CHANGED
|
@@ -1,28 +1,36 @@
|
|
| 1 |
-
from
|
|
|
|
|
|
|
| 2 |
from logic import synthesize_voice, plot_data, plot_waveforms
|
| 3 |
import base64
|
| 4 |
-
from
|
| 5 |
|
| 6 |
-
app =
|
| 7 |
-
CORS(app)
|
| 8 |
|
| 9 |
|
| 10 |
-
# Hugging Face model URLs
|
| 11 |
-
tacotron2_model_url = "your_tacotron2_model_url"
|
| 12 |
-
hifi_gan_model_url = "your_hifi_gan_model_url"
|
| 13 |
-
|
| 14 |
# You need to replace the placeholders above with the actual URLs for the models.
|
| 15 |
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
-
@app.
|
| 21 |
-
def synthesize():
|
| 22 |
-
font_type =
|
| 23 |
-
input_text =
|
| 24 |
|
| 25 |
-
# Font selection logic (
|
| 26 |
if font_type == 'Preeti':
|
| 27 |
# Implement Preeti font logic
|
| 28 |
pass
|
|
@@ -45,17 +53,13 @@ def synthesize():
|
|
| 45 |
# Encode audio content as Base64
|
| 46 |
with open(audio_file_path, 'rb') as audio_file:
|
| 47 |
audio_base64 = base64.b64encode(audio_file.read()).decode('utf-8')
|
| 48 |
-
|
| 49 |
-
#
|
| 50 |
response_data = {
|
| 51 |
'mel_spectrogram': mel_output_base64,
|
| 52 |
'audio_data': audio_base64,
|
| 53 |
-
'waveform'
|
| 54 |
'some_other_data': 'example_value',
|
| 55 |
}
|
| 56 |
|
| 57 |
-
return
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
if __name__ == '__main__':
|
| 61 |
-
app.run(debug=True, threaded=True)
|
|
|
|
| 1 |
+
from fastapi import FastAPI, Request
|
| 2 |
+
from fastapi.responses import JSONResponse
|
| 3 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 4 |
from logic import synthesize_voice, plot_data, plot_waveforms
|
| 5 |
import base64
|
| 6 |
+
from typing import Dict
|
| 7 |
|
| 8 |
+
app = FastAPI()
|
|
|
|
| 9 |
|
| 10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
# You need to replace the placeholders above with the actual URLs for the models.
|
| 12 |
|
| 13 |
+
# Allow requests from your Vercel domain
|
| 14 |
+
origins = [
|
| 15 |
+
"https://host-test-smoky.vercel.app",
|
| 16 |
+
# Add other allowed origins if needed
|
| 17 |
+
]
|
| 18 |
+
|
| 19 |
+
# Set up CORS middleware
|
| 20 |
+
app.add_middleware(
|
| 21 |
+
CORSMiddleware,
|
| 22 |
+
allow_origins=origins,
|
| 23 |
+
allow_credentials=True,
|
| 24 |
+
allow_methods=["*"],
|
| 25 |
+
allow_headers=["*"],
|
| 26 |
+
)
|
| 27 |
|
| 28 |
+
@app.post("/synthesize", response_model=Dict[str, str])
|
| 29 |
+
async def synthesize(request_data: Dict[str, str]):
|
| 30 |
+
font_type = request_data['font_select']
|
| 31 |
+
input_text = request_data['input_text']
|
| 32 |
|
| 33 |
+
# Font selection logic (customize based on your requirements)
|
| 34 |
if font_type == 'Preeti':
|
| 35 |
# Implement Preeti font logic
|
| 36 |
pass
|
|
|
|
| 53 |
# Encode audio content as Base64
|
| 54 |
with open(audio_file_path, 'rb') as audio_file:
|
| 55 |
audio_base64 = base64.b64encode(audio_file.read()).decode('utf-8')
|
| 56 |
+
|
| 57 |
+
# Customize the response based on the information you want to send to the frontend
|
| 58 |
response_data = {
|
| 59 |
'mel_spectrogram': mel_output_base64,
|
| 60 |
'audio_data': audio_base64,
|
| 61 |
+
'waveform': wave_base64,
|
| 62 |
'some_other_data': 'example_value',
|
| 63 |
}
|
| 64 |
|
| 65 |
+
return JSONResponse(content=response_data)
|
|
|
|
|
|
|
|
|
|
|
|