Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -8,9 +8,25 @@ openai.api_key = os.getenv("OPENAI_API_KEY") # Set this in HF Space secrets
|
|
8 |
|
9 |
summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
|
10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
def process_audio(audio_path):
|
|
|
12 |
if not audio_path or not isinstance(audio_path, str):
|
13 |
-
return "No audio file provided.", "", "", ""
|
14 |
try:
|
15 |
with open(audio_path, "rb") as audio_file:
|
16 |
transcript = openai.audio.transcriptions.create(
|
@@ -18,15 +34,14 @@ def process_audio(audio_path):
|
|
18 |
file=audio_file,
|
19 |
response_format="text"
|
20 |
)
|
21 |
-
transcript =
|
22 |
except Exception as e:
|
23 |
-
return f"Error in transcription: {
|
24 |
try:
|
25 |
detected_lang = detect(transcript)
|
|
|
26 |
except Exception:
|
27 |
-
|
28 |
-
lang_map = {'en': 'English', 'hi': 'Hindi', 'ta': 'Tamil'}
|
29 |
-
lang_text = lang_map.get(detected_lang, detected_lang)
|
30 |
transcript_en = transcript
|
31 |
if detected_lang != "en":
|
32 |
try:
|
@@ -36,16 +51,20 @@ def process_audio(audio_path):
|
|
36 |
file=audio_file,
|
37 |
response_format="text"
|
38 |
)
|
39 |
-
transcript_en =
|
40 |
except Exception as e:
|
41 |
-
transcript_en = f"Error translating: {
|
42 |
try:
|
43 |
summary = summarizer(transcript_en, max_length=100, min_length=30, do_sample=False)
|
44 |
-
|
45 |
-
|
|
|
|
|
|
|
46 |
except Exception as e:
|
47 |
-
summary_text = f"Error summarizing: {
|
48 |
-
|
|
|
49 |
|
50 |
iface = gr.Interface(
|
51 |
fn=process_audio,
|
|
|
8 |
|
9 |
summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
|
10 |
|
11 |
+
def make_str(val):
|
12 |
+
# Always return string, even if val is None, bool, list, dict, etc.
|
13 |
+
try:
|
14 |
+
if val is None:
|
15 |
+
return ""
|
16 |
+
if isinstance(val, (bool, int, float)):
|
17 |
+
return str(val)
|
18 |
+
if isinstance(val, list):
|
19 |
+
return "\n".join([make_str(v) for v in val])
|
20 |
+
if isinstance(val, dict):
|
21 |
+
return str(val)
|
22 |
+
return str(val)
|
23 |
+
except Exception:
|
24 |
+
return ""
|
25 |
+
|
26 |
def process_audio(audio_path):
|
27 |
+
# Accept only valid, non-empty file path (string)
|
28 |
if not audio_path or not isinstance(audio_path, str):
|
29 |
+
return ("No audio file provided.", "", "", "")
|
30 |
try:
|
31 |
with open(audio_path, "rb") as audio_file:
|
32 |
transcript = openai.audio.transcriptions.create(
|
|
|
34 |
file=audio_file,
|
35 |
response_format="text"
|
36 |
)
|
37 |
+
transcript = make_str(transcript).strip()
|
38 |
except Exception as e:
|
39 |
+
return (make_str(f"Error in transcription: {e}"), "", "", "")
|
40 |
try:
|
41 |
detected_lang = detect(transcript)
|
42 |
+
lang_text = {'en': 'English', 'hi': 'Hindi', 'ta': 'Tamil'}.get(detected_lang, detected_lang)
|
43 |
except Exception:
|
44 |
+
lang_text = "unknown"
|
|
|
|
|
45 |
transcript_en = transcript
|
46 |
if detected_lang != "en":
|
47 |
try:
|
|
|
51 |
file=audio_file,
|
52 |
response_format="text"
|
53 |
)
|
54 |
+
transcript_en = make_str(transcript_en).strip()
|
55 |
except Exception as e:
|
56 |
+
transcript_en = make_str(f"Error translating: {e}")
|
57 |
try:
|
58 |
summary = summarizer(transcript_en, max_length=100, min_length=30, do_sample=False)
|
59 |
+
# Make sure we always extract a string summary, never bool/None/etc.
|
60 |
+
if isinstance(summary, list) and len(summary) > 0 and "summary_text" in summary[0]:
|
61 |
+
summary_text = make_str(summary[0]["summary_text"])
|
62 |
+
else:
|
63 |
+
summary_text = make_str(summary)
|
64 |
except Exception as e:
|
65 |
+
summary_text = make_str(f"Error summarizing: {e}")
|
66 |
+
# Return only strings, never bool/None/dict/list
|
67 |
+
return (make_str(lang_text), make_str(transcript), make_str(transcript_en), make_str(summary_text))
|
68 |
|
69 |
iface = gr.Interface(
|
70 |
fn=process_audio,
|