lord-reso commited on
Commit
579d95d
·
verified ·
1 Parent(s): c87399a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -27
app.py CHANGED
@@ -1,4 +1,4 @@
1
- from fastapi import FastAPI, Request, Response
2
  from fastapi.responses import JSONResponse, StreamingResponse
3
  from fastapi.middleware.cors import CORSMiddleware
4
  from logic import synthesize_voice, plot_data, plot_waveforms
@@ -7,15 +7,10 @@ import sys
7
  import numpy as np
8
  from io import BytesIO
9
  from hifigan.inference_e2e import hifi_gan_inference
10
- import asyncio
11
 
12
  app = FastAPI()
13
 
14
- @app.get("/")
15
- def read_root():
16
- data = {"Voice": "Cloning", "Status": "Success"}
17
- return JSONResponse(content=data)
18
-
19
  app.add_middleware(
20
  CORSMiddleware,
21
  allow_origins=["*"],
@@ -24,11 +19,15 @@ app.add_middleware(
24
  allow_headers=["*"],
25
  )
26
 
27
- hugging_face_api_url = "https://huggingface.co/spaces/lord-reso/host/synthesize"
 
 
 
28
 
29
  @app.post("/synthesize")
30
  async def synthesize(request: Request):
31
  print("call successful")
 
32
 
33
  json = await request.json()
34
  print(json)
@@ -36,26 +35,9 @@ async def synthesize(request: Request):
36
  font_type = json['font_select']
37
  input_text = json['input_text']
38
 
39
- # SSE initialization
40
- async def generate():
41
- yield "data: 0\n\n" # Initial progress
42
-
43
- response = StreamingResponse(generate(), media_type="text/event-stream")
44
-
45
- # SSE function to send progress updates
46
- async def send_progress(progress):
47
- await response.send(f"data: {progress}\n\n")
48
-
49
- await send_progress(0) # Send initial progress
50
-
51
  # Generate mel-spectrogram using Tacotron2
52
  mel_output_data, mel_output_postnet_data, alignments_data = synthesize_voice(input_text, "Shruti_finetuned.pt")
53
  print("mel generation successful")
54
-
55
- # Simulate progress updates for demonstration purposes
56
- for progress in range(10, 101, 10):
57
- await send_progress(progress)
58
- await asyncio.sleep(1)
59
 
60
  # Convert mel-spectrogram to base64 for display in HTML
61
  mel_output_base64 = plot_data([mel_output_data, mel_output_postnet_data, alignments_data])
@@ -84,6 +66,5 @@ async def synthesize(request: Request):
84
  'some_other_data': 'example_value',
85
  # 'hugging_face_response': hugging_face_response, # Include Hugging Face API response
86
  }
87
-
88
- await send_progress(100) # Send 100% progress to indicate completion
89
  return JSONResponse(content=response_data)
 
1
+ from fastapi import FastAPI, Request
2
  from fastapi.responses import JSONResponse, StreamingResponse
3
  from fastapi.middleware.cors import CORSMiddleware
4
  from logic import synthesize_voice, plot_data, plot_waveforms
 
7
  import numpy as np
8
  from io import BytesIO
9
  from hifigan.inference_e2e import hifi_gan_inference
 
10
 
11
  app = FastAPI()
12
 
13
+ # Enable CORS
 
 
 
 
14
  app.add_middleware(
15
  CORSMiddleware,
16
  allow_origins=["*"],
 
19
  allow_headers=["*"],
20
  )
21
 
22
+ # SSE initialization
23
+ async def send_progress(progress: int):
24
+ data = f"data: {progress}\n\n"
25
+ return StreamingResponse(content=data.encode(), media_type="text/event-stream")
26
 
27
  @app.post("/synthesize")
28
  async def synthesize(request: Request):
29
  print("call successful")
30
+ await send_progress(0) # Send initial progress
31
 
32
  json = await request.json()
33
  print(json)
 
35
  font_type = json['font_select']
36
  input_text = json['input_text']
37
 
 
 
 
 
 
 
 
 
 
 
 
 
38
  # Generate mel-spectrogram using Tacotron2
39
  mel_output_data, mel_output_postnet_data, alignments_data = synthesize_voice(input_text, "Shruti_finetuned.pt")
40
  print("mel generation successful")
 
 
 
 
 
41
 
42
  # Convert mel-spectrogram to base64 for display in HTML
43
  mel_output_base64 = plot_data([mel_output_data, mel_output_postnet_data, alignments_data])
 
66
  'some_other_data': 'example_value',
67
  # 'hugging_face_response': hugging_face_response, # Include Hugging Face API response
68
  }
69
+
 
70
  return JSONResponse(content=response_data)