ygauravyy commited on
Commit
a4d2895
·
verified ·
1 Parent(s): 0c3bf37

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -46
app.py CHANGED
@@ -1,6 +1,6 @@
1
  import os
2
  import torch
3
- import gradio as gr
4
  import openai
5
  from zipfile import ZipFile
6
  import requests
@@ -10,6 +10,10 @@ import langid
10
  import traceback
11
  from dotenv import load_dotenv
12
 
 
 
 
 
13
  # Load environment variables
14
  load_dotenv()
15
 
@@ -90,7 +94,7 @@ def predict(audio_file_pth, agree):
90
  if audio_file_pth is not None:
91
  speaker_wav = audio_file_pth
92
  else:
93
- text_hint += "[ERROR] Please record your voice using the Microphone.\n"
94
  return (text_hint, None)
95
 
96
  # Transcribe audio to text using OpenAI Whisper
@@ -142,7 +146,6 @@ def predict(audio_file_pth, agree):
142
  stop=None,
143
  temperature=0.7,
144
  )
145
- # Correctly access the response content
146
  reply_text = response.choices[0].message.content.strip()
147
  print(f"GPT-4 Reply: {reply_text}")
148
  except Exception as e:
@@ -177,48 +180,25 @@ def predict(audio_file_pth, agree):
177
 
178
  return (text_hint, synthesized_audio_path)
179
 
180
- with gr.Blocks(analytics_enabled=False) as demo:
181
- gr.Markdown("# Mickey Mouse Voice Assistant")
182
-
183
- with gr.Row():
184
- with gr.Column():
185
- audio_input = gr.Audio(
186
- source="microphone",
187
- type="filepath",
188
- label="Record Your Voice",
189
- info="Click the microphone button to record your voice."
190
- )
191
- tos_checkbox = gr.Checkbox(
192
- label="Agree to Terms & Conditions",
193
- value=False,
194
- info="I agree to the terms of service."
195
- )
196
- submit_button = gr.Button("Send")
197
-
198
- with gr.Column():
199
- info_output = gr.Textbox(
200
- label="Info",
201
- interactive=False,
202
- lines=4,
203
- )
204
- audio_output = gr.Audio(
205
- label="Mickey's Response",
206
- interactive=False,
207
- autoplay=True,
208
- )
209
 
210
- submit_button.click(
211
- predict,
212
- inputs=[audio_input, tos_checkbox],
213
- outputs=[info_output, audio_output]
214
- )
215
 
216
- # Launch the Gradio app
217
- demo.queue()
218
- demo.launch(
219
- server_name="0.0.0.0",
220
- server_port=int(os.environ.get("PORT", 7860)),
221
- debug=True,
222
- show_api=True,
223
- share=False
224
- )
 
1
  import os
2
  import torch
3
+ import argparse
4
  import openai
5
  from zipfile import ZipFile
6
  import requests
 
10
  import traceback
11
  from dotenv import load_dotenv
12
 
13
+ from fastapi import FastAPI, File, UploadFile, Form
14
+ from fastapi.responses import JSONResponse
15
+ import uvicorn
16
+
17
  # Load environment variables
18
  load_dotenv()
19
 
 
94
  if audio_file_pth is not None:
95
  speaker_wav = audio_file_pth
96
  else:
97
+ text_hint += "[ERROR] Please provide your voice as an audio file.\n"
98
  return (text_hint, None)
99
 
100
  # Transcribe audio to text using OpenAI Whisper
 
146
  stop=None,
147
  temperature=0.7,
148
  )
 
149
  reply_text = response.choices[0].message.content.strip()
150
  print(f"GPT-4 Reply: {reply_text}")
151
  except Exception as e:
 
180
 
181
  return (text_hint, synthesized_audio_path)
182
 
183
+ app = FastAPI()
184
+
185
+ @app.post("/predict")
186
+ async def predict_endpoint(agree: bool = Form(...), audio_file: UploadFile = File(...)):
187
+ # Save the uploaded file locally
188
+ temp_dir = "temp"
189
+ os.makedirs(temp_dir, exist_ok=True)
190
+ audio_path = os.path.join(temp_dir, audio_file.filename)
191
+ with open(audio_path, "wb") as f:
192
+ f.write(await audio_file.read())
193
+
194
+ info, audio_output_path = predict(audio_path, agree)
195
+ if audio_output_path:
196
+ # Return a JSON response with info and a path to the audio file.
197
+ # You could return the file content as base64 if you prefer.
198
+ return JSONResponse(content={"info": info, "audio_path": audio_output_path})
199
+ else:
200
+ return JSONResponse(content={"info": info, "audio_path": None}, status_code=400)
 
 
 
 
 
 
 
 
 
 
 
201
 
 
 
 
 
 
202
 
203
+ if __name__ == "__main__":
204
+ uvicorn.run(app, host="0.0.0.0", port=int(os.environ.get("PORT", 7860)), debug=True)