Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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()
|