ilsa15 commited on
Commit
cc41fb6
·
verified ·
1 Parent(s): 87e236a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +105 -35
app.py CHANGED
@@ -1,46 +1,116 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
- import whisper
3
  import os
4
  from groq import Groq
 
 
5
 
6
- # 🔐 Get Groq API key securely from Hugging Face Secrets
7
  GROQ_API_KEY = os.getenv("GROQ_API_KEY")
8
  groq_client = Groq(api_key=GROQ_API_KEY)
9
  MODEL_NAME = "llama3-8b-8192"
10
 
11
- # 🎙 Load Whisper
12
- transcriber = whisper.load_model("base")
13
-
14
- def transcribe_and_summarize(audio):
15
- # Step 1: Transcribe + Detect Language
16
- result = transcriber.transcribe(audio)
17
- transcript = result["text"]
18
- detected_lang = result["language"]
19
-
20
- # Step 2: Summarize in the same language
21
- if detected_lang == "en":
22
- system_prompt = "You are an expert English summarizer."
23
- user_prompt = f"Please summarize the following English text:\n\n{transcript}"
24
- elif detected_lang == "ur":
25
- system_prompt = "آپ ایک ماہر خلاصہ نگار ہیں جو اردو میں خلاصہ فراہم کرتے ہیں۔"
26
- user_prompt = f"براہ کرم مندرجہ ذیل اردو متن کا خلاصہ فراہم کریں:\n\n{transcript}"
27
- else:
28
- system_prompt = "You are a helpful summarizer."
29
- user_prompt = f"Summarize this text:\n\n{transcript}"
30
-
31
- response = groq_client.chat.completions.create(
32
- model=MODEL_NAME,
33
- messages=[
34
- {"role": "system", "content": system_prompt},
35
- {"role": "user", "content": user_prompt}
36
- ]
37
- )
38
- summary = response.choices[0].message.content.strip()
39
-
40
- lang_label = "English" if detected_lang == "en" else "Urdu" if detected_lang == "ur" else detected_lang.upper()
41
-
42
- return f"[{lang_label}] {transcript}", f"[{lang_label}] {summary}"
 
 
 
 
 
 
 
 
 
 
 
43
 
 
44
  demo = gr.Interface(
45
  fn=transcribe_and_summarize,
46
  inputs=gr.Audio(type="filepath", label="🎧 Upload Audio (English or Urdu)"),
@@ -49,7 +119,7 @@ demo = gr.Interface(
49
  gr.Textbox(label="🧠 Summary")
50
  ],
51
  title="🗣️ Multilingual Audio Summarizer",
52
- description="Upload English or Urdu audio. The app transcribes and summarizes in the same language using Whisper + Groq."
53
  )
54
 
55
  demo.launch()
 
1
+ # import gradio as gr
2
+ # import whisper
3
+ # import os
4
+ # from groq import Groq
5
+
6
+ # # 🔐 Get Groq API key securely from Hugging Face Secrets
7
+ # GROQ_API_KEY = os.getenv("GROQ_API_KEY")
8
+ # groq_client = Groq(api_key=GROQ_API_KEY)
9
+ # MODEL_NAME = "llama3-8b-8192"
10
+
11
+ # # 🎙 Load Whisper
12
+ # transcriber = whisper.load_model("base")
13
+
14
+ # def transcribe_and_summarize(audio):
15
+ # # Step 1: Transcribe + Detect Language
16
+ # result = transcriber.transcribe(audio)
17
+ # transcript = result["text"]
18
+ # detected_lang = result["language"]
19
+
20
+ # # Step 2: Summarize in the same language
21
+ # if detected_lang == "en":
22
+ # system_prompt = "You are an expert English summarizer."
23
+ # user_prompt = f"Please summarize the following English text:\n\n{transcript}"
24
+ # elif detected_lang == "ur":
25
+ # system_prompt = "آپ ایک ماہر خلاصہ نگار ہیں جو اردو میں خلاصہ فراہم کرتے ہیں۔"
26
+ # user_prompt = f"براہ کرم مندرجہ ذیل اردو متن کا خلاصہ فراہم کریں:\n\n{transcript}"
27
+ # else:
28
+ # system_prompt = "You are a helpful summarizer."
29
+ # user_prompt = f"Summarize this text:\n\n{transcript}"
30
+
31
+ # response = groq_client.chat.completions.create(
32
+ # model=MODEL_NAME,
33
+ # messages=[
34
+ # {"role": "system", "content": system_prompt},
35
+ # {"role": "user", "content": user_prompt}
36
+ # ]
37
+ # )
38
+ # summary = response.choices[0].message.content.strip()
39
+
40
+ # lang_label = "English" if detected_lang == "en" else "Urdu" if detected_lang == "ur" else detected_lang.upper()
41
+
42
+ # return f"[{lang_label}] {transcript}", f"[{lang_label}] {summary}"
43
+
44
+ # demo = gr.Interface(
45
+ # fn=transcribe_and_summarize,
46
+ # inputs=gr.Audio(type="filepath", label="🎧 Upload Audio (English or Urdu)"),
47
+ # outputs=[
48
+ # gr.Textbox(label="📝 Transcript"),
49
+ # gr.Textbox(label="🧠 Summary")
50
+ # ],
51
+ # title="🗣️ Multilingual Audio Summarizer",
52
+ # description="Upload English or Urdu audio. The app transcribes and summarizes in the same language using Whisper + Groq."
53
+ # )
54
+
55
+ # demo.launch()
56
+
57
+
58
  import gradio as gr
 
59
  import os
60
  from groq import Groq
61
+ from transformers import pipeline
62
+ import torchaudio
63
 
64
+ # 🔐 Groq API key from environment
65
  GROQ_API_KEY = os.getenv("GROQ_API_KEY")
66
  groq_client = Groq(api_key=GROQ_API_KEY)
67
  MODEL_NAME = "llama3-8b-8192"
68
 
69
+ # Use fast Whisper Tiny ASR model from Hugging Face
70
+ asr = pipeline("automatic-speech-recognition", model="openai/whisper-tiny")
71
+
72
+ # Limit audio duration to 5 mins (300 seconds)
73
+ MAX_DURATION = 300
74
+
75
+ def transcribe_and_summarize(audio_path):
76
+ try:
77
+ # Check audio duration
78
+ info = torchaudio.info(audio_path)
79
+ duration = info.num_frames / info.sample_rate
80
+ if duration > MAX_DURATION:
81
+ return "❌ Audio too long. Please upload audio under 5 minutes.", ""
82
+
83
+ # Transcribe using Whisper-tiny
84
+ result = asr(audio_path)
85
+ transcript = result["text"]
86
+ detected_lang = result.get("language", "en") # fallback to 'en' if missing
87
+
88
+ # Summarize with Groq
89
+ if detected_lang == "en":
90
+ system_prompt = "You are an expert English summarizer."
91
+ user_prompt = f"Please summarize the following English text:\n\n{transcript}"
92
+ elif detected_lang == "ur":
93
+ system_prompt = "آپ ایک ماہر خلاصہ نگار ہیں جو اردو میں خلاصہ فراہم کرتے ہیں۔"
94
+ user_prompt = f"براہ کرم مندرجہ ذیل اردو متن کا خلاصہ فراہم کریں:\n\n{transcript}"
95
+ else:
96
+ system_prompt = "You are a helpful summarizer."
97
+ user_prompt = f"Summarize this text:\n\n{transcript}"
98
+
99
+ response = groq_client.chat.completions.create(
100
+ model=MODEL_NAME,
101
+ messages=[
102
+ {"role": "system", "content": system_prompt},
103
+ {"role": "user", "content": user_prompt}
104
+ ]
105
+ )
106
+ summary = response.choices[0].message.content.strip()
107
+ lang_label = "English" if detected_lang == "en" else "Urdu" if detected_lang == "ur" else detected_lang.upper()
108
+ return f"[{lang_label}] {transcript}", f"[{lang_label}] {summary}"
109
+
110
+ except Exception as e:
111
+ return "❌ Error processing audio. Please try again.", str(e)
112
 
113
+ # Gradio UI
114
  demo = gr.Interface(
115
  fn=transcribe_and_summarize,
116
  inputs=gr.Audio(type="filepath", label="🎧 Upload Audio (English or Urdu)"),
 
119
  gr.Textbox(label="🧠 Summary")
120
  ],
121
  title="🗣️ Multilingual Audio Summarizer",
122
+ description="Upload English or Urdu audio (max 5 minutes). The app transcribes and summarizes using Whisper-Tiny + Groq."
123
  )
124
 
125
  demo.launch()