Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -2,12 +2,24 @@ import gradio as gr
|
|
2 |
from transformers import pipeline
|
3 |
from langdetect import detect
|
4 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
def process_audio(audio_path):
|
|
|
|
|
6 |
try:
|
7 |
-
|
8 |
result = asr(audio_path)
|
9 |
transcript = result["text"]
|
10 |
except Exception as e:
|
|
|
11 |
return f"Error in transcription: {e}", "", "", ""
|
12 |
try:
|
13 |
detected_lang = detect(transcript)
|
@@ -18,20 +30,16 @@ def process_audio(audio_path):
|
|
18 |
transcript_en = transcript
|
19 |
if detected_lang != "en":
|
20 |
try:
|
21 |
-
asr_translate = pipeline(
|
22 |
-
"automatic-speech-recognition",
|
23 |
-
model="openai/whisper-large",
|
24 |
-
task="translate"
|
25 |
-
)
|
26 |
result_translate = asr_translate(audio_path)
|
27 |
transcript_en = result_translate["text"]
|
28 |
except Exception as e:
|
|
|
29 |
transcript_en = f"Error translating: {e}"
|
30 |
try:
|
31 |
-
summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
|
32 |
summary = summarizer(transcript_en, max_length=100, min_length=30, do_sample=False)
|
33 |
summary_text = summary[0]["summary_text"]
|
34 |
except Exception as e:
|
|
|
35 |
summary_text = f"Error summarizing: {e}"
|
36 |
return lang_text, transcript, transcript_en, summary_text
|
37 |
|
|
|
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):
|
16 |
+
return "No audio file provided.", "", "", ""
|
17 |
try:
|
18 |
+
# Transcription
|
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 |
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 |
|