Ravishankarsharma commited on
Commit
be92e8a
·
verified ·
1 Parent(s): 36df307

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -16
app.py CHANGED
@@ -1,39 +1,38 @@
1
  import gradio as gr
2
  import whisper
3
  from transformers import pipeline
 
 
4
 
5
- # =============== MODEL LOADING =====================
6
- # Load Whisper model (speech-to-text)
7
- whisper_model = whisper.load_model("base") # Requires openai-whisper package
8
 
9
- # Load summarization model (text summarization)
10
- summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
11
-
12
- # =============== FUNCTION ==========================
13
  def transcribe_and_summarize(audio_path):
14
  if audio_path is None:
15
  return "No file uploaded.", "No summary.", "API not available"
16
 
17
- # Transcribe audio using Whisper
18
  try:
19
  result = whisper_model.transcribe(audio_path)
20
  transcription = result["text"]
21
  except Exception as e:
22
  return f"Transcription failed: {e}", "No summary.", "API not available"
23
 
24
- # Summarize using Transformers
25
  try:
26
  summary = summarizer(transcription, max_length=60, min_length=10, do_sample=False)[0]["summary_text"]
27
  except Exception as e:
28
  summary = f"Summarization failed: {e}"
29
 
30
- # Static API URL (replace with your actual space name)
31
- api_link = "https://your-username-your-space-name.hf.space/api/predict/"
32
-
33
  return transcription, summary, api_link
34
 
35
- # =============== GRADIO APP ========================
36
- interface = gr.Interface(
37
  fn=transcribe_and_summarize,
38
  inputs=gr.Audio(type="filepath", label="Upload Audio File"),
39
  outputs=[
@@ -45,6 +44,28 @@ interface = gr.Interface(
45
  description="Upload an audio file → Transcribe it with Whisper → Summarize it using BART → Copy the API link for integration.",
46
  )
47
 
48
- # Entry point
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49
  if __name__ == "__main__":
50
- interface.launch()
 
 
1
  import gradio as gr
2
  import whisper
3
  from transformers import pipeline
4
+ from fastapi import FastAPI, UploadFile, File
5
+ from fastapi.middleware.wsgi import WSGIMiddleware
6
 
7
+ # ================== LOAD MODELS =====================
8
+ whisper_model = whisper.load_model("base") # Speech-to-text
9
+ summarizer = pipeline("summarization", model="facebook/bart-large-cnn") # Text summarization
10
 
11
+ # ================== FUNCTION =======================
 
 
 
12
  def transcribe_and_summarize(audio_path):
13
  if audio_path is None:
14
  return "No file uploaded.", "No summary.", "API not available"
15
 
16
+ # Transcription
17
  try:
18
  result = whisper_model.transcribe(audio_path)
19
  transcription = result["text"]
20
  except Exception as e:
21
  return f"Transcription failed: {e}", "No summary.", "API not available"
22
 
23
+ # Summarization
24
  try:
25
  summary = summarizer(transcription, max_length=60, min_length=10, do_sample=False)[0]["summary_text"]
26
  except Exception as e:
27
  summary = f"Summarization failed: {e}"
28
 
29
+ # API URL (replace with your deployed Hugging Face Space or server URL)
30
+ api_link = "https://your-username-your-space-name.hf.space/run/predict"
31
+
32
  return transcription, summary, api_link
33
 
34
+ # ================== GRADIO INTERFACE =================
35
+ gradio_interface = gr.Interface(
36
  fn=transcribe_and_summarize,
37
  inputs=gr.Audio(type="filepath", label="Upload Audio File"),
38
  outputs=[
 
44
  description="Upload an audio file → Transcribe it with Whisper → Summarize it using BART → Copy the API link for integration.",
45
  )
46
 
47
+ # ================== FASTAPI APP ======================
48
+ api_app = FastAPI(title="Audio Transcriber + Summarizer API")
49
+
50
+ @api_app.post("/api/transcribe/")
51
+ async def transcribe_api(file: UploadFile = File(...)):
52
+ # Save uploaded file temporarily
53
+ temp_path = f"temp_{file.filename}"
54
+ with open(temp_path, "wb") as f:
55
+ f.write(await file.read())
56
+
57
+ # Call the same function as Gradio
58
+ transcription, summary, api_link = transcribe_and_summarize(temp_path)
59
+
60
+ # Optionally, delete temp file after processing
61
+ os.remove(temp_path)
62
+
63
+ return {"transcription": transcription, "summary": summary, "api_url": api_link}
64
+
65
+ # ================== MOUNT GRADIO ON FASTAPI ==========
66
+ api_app.mount("/gradio", WSGIMiddleware(gradio_interface.app))
67
+
68
+ # ================== ENTRY POINT =====================
69
  if __name__ == "__main__":
70
+ import uvicorn
71
+ uvicorn.run(api_app, host="0.0.0.0", port=8000)