r0kaxmin commited on
Commit
f4acf9c
·
verified ·
1 Parent(s): 02c1266

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -19
app.py CHANGED
@@ -5,25 +5,19 @@ from fastapi import FastAPI
5
  from pydantic import BaseModel
6
  from faster_whisper import WhisperModel
7
  import gradio as gr
 
8
 
9
  # --- 1. Configuration & Model Loading ---
10
- # Set cache directories
11
  os.environ["HF_HOME"] = "/tmp/huggingface_cache"
12
  os.environ["HF_HUB_CACHE"] = "/tmp/huggingface_cache"
13
-
14
- # Load the model once when the application starts
15
  model = WhisperModel("Systran/faster-whisper-small", device="cpu", compute_type="int8")
16
 
17
  # --- 2. FastAPI Application Setup ---
18
- # Create a FastAPI app instance
19
  app = FastAPI()
20
 
21
- # Define the structure of the incoming API request
22
  class AudioInput(BaseModel):
23
- # Expects a list with one item: a base64 encoded audio data URI
24
  data: list[str]
25
 
26
- # Define the transcription function (this is what the API will use)
27
  def transcribe_audio(audio_filepath, language):
28
  if audio_filepath is None:
29
  return "Error: No audio file provided."
@@ -32,35 +26,30 @@ def transcribe_audio(audio_filepath, language):
32
  return " ".join(seg.text for seg in segments)
33
 
34
  # --- 3. Create the API Endpoint ---
35
- # This creates an endpoint at the path /predict
36
  @app.post("/predict")
37
  async def predict(audio_input: AudioInput):
38
  # The Gradio API sends data in a list, so we get the first item
39
  base64_data_uri = audio_input.data[0]
40
 
41
- # Extract the base64 part of the data URI
 
 
 
42
  header, encoded_data = base64_data_uri.split(",", 1)
43
-
44
- # Decode the base64 string into binary audio data
45
  audio_data = base64.b64decode(encoded_data)
46
 
47
- # Create a temporary file to save the audio, as the model needs a file path
48
  with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as temp_audio_file:
49
  temp_audio_file.write(audio_data)
50
  temp_filepath = temp_audio_file.name
51
 
52
  try:
53
- # Run transcription on the temporary file
54
- transcription = transcribe_audio(temp_filepath, "auto") # Using 'auto' for the API
55
  finally:
56
- # Clean up and delete the temporary file
57
  os.remove(temp_filepath)
58
 
59
- # Return the result in the format Gradio expects
60
  return {"data": [transcription]}
61
 
62
  # --- 4. Create the Gradio User Interface ---
63
- # Note: We are NOT calling iface.launch() here
64
  iface = gr.Interface(
65
  fn=transcribe_audio,
66
  inputs=[
@@ -73,5 +62,8 @@ iface = gr.Interface(
73
  )
74
 
75
  # --- 5. Mount the Gradio UI onto the FastAPI App ---
76
- # This makes the Gradio UI available at the root path "/"
77
- app = gr.mount_gradio_app(app, iface, path="/")
 
 
 
 
5
  from pydantic import BaseModel
6
  from faster_whisper import WhisperModel
7
  import gradio as gr
8
+ import uvicorn # <-- IMPORT THE SERVER
9
 
10
  # --- 1. Configuration & Model Loading ---
 
11
  os.environ["HF_HOME"] = "/tmp/huggingface_cache"
12
  os.environ["HF_HUB_CACHE"] = "/tmp/huggingface_cache"
 
 
13
  model = WhisperModel("Systran/faster-whisper-small", device="cpu", compute_type="int8")
14
 
15
  # --- 2. FastAPI Application Setup ---
 
16
  app = FastAPI()
17
 
 
18
  class AudioInput(BaseModel):
 
19
  data: list[str]
20
 
 
21
  def transcribe_audio(audio_filepath, language):
22
  if audio_filepath is None:
23
  return "Error: No audio file provided."
 
26
  return " ".join(seg.text for seg in segments)
27
 
28
  # --- 3. Create the API Endpoint ---
 
29
  @app.post("/predict")
30
  async def predict(audio_input: AudioInput):
31
  # The Gradio API sends data in a list, so we get the first item
32
  base64_data_uri = audio_input.data[0]
33
 
34
+ # Handle the null test case from curl
35
+ if base64_data_uri is None:
36
+ return {"data": ["Error: No audio file provided."]}
37
+
38
  header, encoded_data = base64_data_uri.split(",", 1)
 
 
39
  audio_data = base64.b64decode(encoded_data)
40
 
 
41
  with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as temp_audio_file:
42
  temp_audio_file.write(audio_data)
43
  temp_filepath = temp_audio_file.name
44
 
45
  try:
46
+ transcription = transcribe_audio(temp_filepath, "auto")
 
47
  finally:
 
48
  os.remove(temp_filepath)
49
 
 
50
  return {"data": [transcription]}
51
 
52
  # --- 4. Create the Gradio User Interface ---
 
53
  iface = gr.Interface(
54
  fn=transcribe_audio,
55
  inputs=[
 
62
  )
63
 
64
  # --- 5. Mount the Gradio UI onto the FastAPI App ---
65
+ app = gr.mount_gradio_app(app, iface, path="/")
66
+
67
+ # --- 6. Run the Server (THIS WAS THE MISSING PART) ---
68
+ if __name__ == "__main__":
69
+ uvicorn.run(app, host="0.0.0.0", port=int(os.getenv("PORT", 7860)))