lord-reso commited on
Commit
f2fb9a3
·
verified ·
1 Parent(s): 5777f66

Convert flask app into fastAPI

Browse files
Files changed (1) hide show
  1. app.py +28 -24
app.py CHANGED
@@ -1,28 +1,36 @@
1
- from flask import Flask, render_template, request, jsonify
 
 
2
  from logic import synthesize_voice, plot_data, plot_waveforms
3
  import base64
4
- from flask_cors import CORS
5
 
6
- app = Flask(__name__)
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
- @app.route('/')
17
- def index():
18
- return {"message": "Hello World"}
 
 
 
 
 
 
 
 
 
 
 
19
 
20
- @app.route('/synthesize', methods=['POST'])
21
- def synthesize():
22
- font_type = request.json['font_select']
23
- input_text = request.json['input_text']
24
 
25
- # Font selection logic (you can customize this based on your requirements)
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
- #You can customize the response based on what information you want to send to the frontend
50
  response_data = {
51
  'mel_spectrogram': mel_output_base64,
52
  'audio_data': audio_base64,
53
- 'waveform' : wave_base64,
54
  'some_other_data': 'example_value',
55
  }
56
 
57
- return jsonify(response_data)
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)