XuminYu commited on
Commit
53efdc2
1 Parent(s): d1e35d4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -26
app.py CHANGED
@@ -11,6 +11,8 @@ import hash_code_for_cached_output
11
 
12
 
13
  API_URL = os.environ.get("API_URL")
 
 
14
  supported_languages = ['zh', 'en', 'ja', 'ko', 'es', 'fr']
15
  supported_styles = {
16
  'zh': "zh_default",
@@ -139,42 +141,43 @@ def predict(prompt, style, audio_file_pth, speed, agree):
139
  }
140
 
141
  start = time.time()
142
- # Send the data as a POST request
143
- response = requests.post(API_URL, json=data, timeout=60)
 
 
 
 
144
  print(f'Get response successfully within {time.time() - start}')
145
 
146
- # Check the response
147
- if response.status_code == 200:
148
- try:
149
- json_data = json.loads(response.content)
150
- text_hint += f"[ERROR] {json_data['error']} \n"
 
 
 
 
 
151
  gr.Warning(
152
- f"[ERROR] {json_data['error']} \n"
153
  )
154
  return (
155
  text_hint,
156
  None,
157
  None,
158
  )
159
- except:
 
160
  with open(save_path, 'wb') as f:
161
- f.write(response.content)
162
- else:
163
- text_hint += f"[HTTP ERROR] {response.status_code} - {response.text} \n"
164
- gr.Warning(
165
- f"[HTTP ERROR] {response.status_code} - {response.text} \n"
166
- )
167
- return (
168
- text_hint,
169
- None,
170
- None,
171
- )
172
- text_hint += f'''Get response successfully \n'''
173
- return (
174
- text_hint,
175
- save_path,
176
- speaker_wav,
177
- )
178
 
179
 
180
  title = "MyShell OpenVoice V2"
 
11
 
12
 
13
  API_URL = os.environ.get("API_URL")
14
+ TOKEN = os.environ.get("TOKEN")
15
+ RESULT_URL = os.environ.get("RESULT_URL")
16
  supported_languages = ['zh', 'en', 'ja', 'ko', 'es', 'fr']
17
  supported_styles = {
18
  'zh': "zh_default",
 
141
  }
142
 
143
  start = time.time()
144
+
145
+ headers = {
146
+ "Authorization": f"Bearer {TOKEN}"
147
+ }
148
+
149
+ response = requests.post(API_URL, json=data, headers=headers, timeout=60)
150
  print(f'Get response successfully within {time.time() - start}')
151
 
152
+ task_id = response.json()['task_id']
153
+ while True:
154
+ response = requests.post(RESULT_URL, json={'task_id': task_id}, headers=headers)
155
+ json_data = response.json()
156
+ status = json_data['status']
157
+ if status in ["CREATED", "RUNNING"]:
158
+ time.sleep(1)
159
+ continue
160
+ if status == 'FAILED':
161
+ text_hint += f"[HTTP ERROR] {json_data['error']} \n"
162
  gr.Warning(
163
+ f"[HTTP ERROR] {json_data['error']} \n"
164
  )
165
  return (
166
  text_hint,
167
  None,
168
  None,
169
  )
170
+ else:
171
+ decoded_bytes = base64.b64decode(json_data['result']['base64'].encode('utf-8'))
172
  with open(save_path, 'wb') as f:
173
+ f.write(decoded_bytes)
174
+
175
+ text_hint += f'''Get response successfully \n'''
176
+ return (
177
+ text_hint,
178
+ save_path,
179
+ speaker_wav,
180
+ )
 
 
 
 
 
 
 
 
 
181
 
182
 
183
  title = "MyShell OpenVoice V2"