jaisun2004 commited on
Commit
e227091
·
verified ·
1 Parent(s): d797fd6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -24
app.py CHANGED
@@ -2,19 +2,13 @@ import gradio as gr
2
  from transformers import pipeline
3
  from langdetect import detect
4
 
5
- # Use the small model for Spaces to avoid OOM
6
- WHISPER_MODEL = "openai/whisper-small"
7
- SUMMARIZER_MODEL = "facebook/bart-large-cnn"
8
-
9
- # Load pipelines ONCE at startup
10
- asr = pipeline("automatic-speech-recognition", model=WHISPER_MODEL)
11
- summarizer = pipeline("summarization", model=SUMMARIZER_MODEL)
12
 
13
  def process_audio(audio_path):
14
- if not audio_path or not isinstance(audio_path, str):
15
  return "No audio file provided.", "", "", ""
16
  try:
17
- # Transcription
18
  result = asr(audio_path)
19
  transcript = result["text"]
20
  except Exception as e:
@@ -28,7 +22,6 @@ def process_audio(audio_path):
28
  transcript_en = transcript
29
  if detected_lang != "en":
30
  try:
31
- # Use the same pipeline, but set task as "translate" for Whisper
32
  result_translate = asr(audio_path, generate_kwargs={"task": "translate"})
33
  transcript_en = result_translate["text"]
34
  except Exception as e:
@@ -40,18 +33,17 @@ def process_audio(audio_path):
40
  summary_text = f"Error summarizing: {e}"
41
  return lang_text, transcript, transcript_en, summary_text
42
 
43
- with gr.Blocks() as demo:
44
- gr.Markdown("## Audio Transcript, Translation & Summary (Whisper + Hugging Face)")
45
- audio_input = gr.Audio(type="filepath", label="Upload MP3/WAV Audio")
46
- btn = gr.Button("Process")
47
- lang_out = gr.Textbox(label="Detected Language")
48
- transcript_out = gr.Textbox(label="Original Transcript")
49
- transcript_en_out = gr.Textbox(label="English Transcript (if translated)")
50
- summary_out = gr.Textbox(label="Summary")
51
- btn.click(
52
- process_audio,
53
- inputs=[audio_input],
54
- outputs=[lang_out, transcript_out, transcript_en_out, summary_out]
55
- )
56
 
57
- demo.launch()
 
2
  from transformers import pipeline
3
  from langdetect import detect
4
 
5
+ asr = pipeline("automatic-speech-recognition", model="openai/whisper-small")
6
+ summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
 
 
 
 
 
7
 
8
  def process_audio(audio_path):
9
+ if not audio_path or isinstance(audio_path, bool):
10
  return "No audio file provided.", "", "", ""
11
  try:
 
12
  result = asr(audio_path)
13
  transcript = result["text"]
14
  except Exception as e:
 
22
  transcript_en = transcript
23
  if detected_lang != "en":
24
  try:
 
25
  result_translate = asr(audio_path, generate_kwargs={"task": "translate"})
26
  transcript_en = result_translate["text"]
27
  except Exception as e:
 
33
  summary_text = f"Error summarizing: {e}"
34
  return lang_text, transcript, transcript_en, summary_text
35
 
36
+ iface = gr.Interface(
37
+ fn=process_audio,
38
+ inputs=gr.Audio(source="upload", type="filepath", label="Upload MP3/WAV Audio"),
39
+ outputs=[
40
+ gr.Textbox(label="Detected Language"),
41
+ gr.Textbox(label="Original Transcript"),
42
+ gr.Textbox(label="English Transcript (if translated)"),
43
+ gr.Textbox(label="Summary"),
44
+ ],
45
+ title="Audio Transcript, Translation & Summary",
46
+ description="Upload your audio file (MP3/WAV). This app transcribes, detects language, translates to English if needed, and summarizes."
47
+ )
 
48
 
49
+ iface.launch()