jaisun2004 commited on
Commit
363824a
·
verified ·
1 Parent(s): 310b5ef

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -12
app.py CHANGED
@@ -2,14 +2,13 @@ import gradio as gr
2
  from transformers import pipeline
3
  from langdetect import detect
4
 
5
- # Load models only once (outside function)
6
- asr = pipeline("automatic-speech-recognition", model="openai/whisper-large")
7
- asr_translate = pipeline(
8
- "automatic-speech-recognition",
9
- model="openai/whisper-large",
10
- task="translate"
11
- )
12
- summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
13
 
14
  def process_audio(audio_path):
15
  if not audio_path or not isinstance(audio_path, str):
@@ -19,7 +18,6 @@ def process_audio(audio_path):
19
  result = asr(audio_path)
20
  transcript = result["text"]
21
  except Exception as e:
22
- print("Transcription error:", e)
23
  return f"Error in transcription: {e}", "", "", ""
24
  try:
25
  detected_lang = detect(transcript)
@@ -30,16 +28,15 @@ def process_audio(audio_path):
30
  transcript_en = transcript
31
  if detected_lang != "en":
32
  try:
33
- result_translate = asr_translate(audio_path)
 
34
  transcript_en = result_translate["text"]
35
  except Exception as e:
36
- print("Translation error:", e)
37
  transcript_en = f"Error translating: {e}"
38
  try:
39
  summary = summarizer(transcript_en, max_length=100, min_length=30, do_sample=False)
40
  summary_text = summary[0]["summary_text"]
41
  except Exception as e:
42
- print("Summarization error:", e)
43
  summary_text = f"Error summarizing: {e}"
44
  return lang_text, transcript, transcript_en, summary_text
45
 
 
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):
 
18
  result = asr(audio_path)
19
  transcript = result["text"]
20
  except Exception as e:
 
21
  return f"Error in transcription: {e}", "", "", ""
22
  try:
23
  detected_lang = detect(transcript)
 
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:
 
35
  transcript_en = f"Error translating: {e}"
36
  try:
37
  summary = summarizer(transcript_en, max_length=100, min_length=30, do_sample=False)
38
  summary_text = summary[0]["summary_text"]
39
  except Exception as e:
 
40
  summary_text = f"Error summarizing: {e}"
41
  return lang_text, transcript, transcript_en, summary_text
42