Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,65 +1,66 @@
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
-
import requests
|
| 4 |
import tempfile
|
| 5 |
import yt_dlp
|
|
|
|
| 6 |
from groq import Groq
|
| 7 |
from huggingface_hub import InferenceClient
|
| 8 |
|
| 9 |
-
# ----------------------------
|
| 10 |
# ✅ Environment Variables
|
| 11 |
-
# ----------------------------
|
| 12 |
GROQ_API_KEY = os.getenv("GROQ_API_KEY")
|
| 13 |
HUGGINGFACE_API_TOKEN = os.getenv("HUGGINGFACE_API_TOKEN")
|
| 14 |
|
| 15 |
if not GROQ_API_KEY or not HUGGINGFACE_API_TOKEN:
|
| 16 |
-
raise EnvironmentError("Please set GROQ_API_KEY and HUGGINGFACE_API_TOKEN
|
| 17 |
|
| 18 |
-
# ----------------------------
|
| 19 |
-
# ✅ Initialize Clients
|
| 20 |
-
# ----------------------------
|
| 21 |
groq_client = Groq(api_key=GROQ_API_KEY)
|
| 22 |
hf_client = InferenceClient(token=HUGGINGFACE_API_TOKEN)
|
| 23 |
|
| 24 |
-
# ----------------------------
|
| 25 |
# ✅ Download YouTube Audio
|
| 26 |
-
# ----------------------------
|
| 27 |
def download_youtube_audio(youtube_url):
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
def transcribe_audio(audio_path):
|
| 50 |
try:
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
except Exception as e:
|
| 58 |
return f"❌ Error during transcription: {e}"
|
| 59 |
|
| 60 |
-
#
|
| 61 |
-
# ✅ Summarize in English or Urdu
|
| 62 |
-
# ----------------------------
|
| 63 |
def summarize_text(text, lang):
|
| 64 |
try:
|
| 65 |
if lang == "English":
|
|
@@ -67,71 +68,49 @@ def summarize_text(text, lang):
|
|
| 67 |
prompt = f"Summarize the following text in English:\n\n{text}"
|
| 68 |
else:
|
| 69 |
model = "facebook/mbart-large-50-many-to-many-mmt"
|
| 70 |
-
prompt = f"مندرجہ ذیل ا
|
| 71 |
|
| 72 |
output = hf_client.text_generation(
|
| 73 |
model=model,
|
| 74 |
prompt=prompt,
|
| 75 |
max_new_tokens=250,
|
| 76 |
temperature=0.7,
|
| 77 |
-
do_sample=False,
|
| 78 |
)
|
| 79 |
return output
|
| 80 |
except Exception as e:
|
| 81 |
return f"❌ Error during summarization: {e}"
|
| 82 |
|
| 83 |
-
#
|
| 84 |
-
# ✅ Main Function: YouTube or File
|
| 85 |
-
# ----------------------------
|
| 86 |
def process_input(youtube_url, audio_file, lang):
|
| 87 |
-
audio_path = None
|
| 88 |
-
|
| 89 |
-
# Step 1: Determine source
|
| 90 |
if youtube_url:
|
| 91 |
audio_path = download_youtube_audio(youtube_url)
|
| 92 |
-
if "❌" in audio_path:
|
| 93 |
-
return audio_path, "", ""
|
| 94 |
elif audio_file:
|
| 95 |
audio_path = audio_file
|
| 96 |
else:
|
| 97 |
-
return "❌ Please upload audio or paste a YouTube link.", "", ""
|
| 98 |
|
| 99 |
-
# Step 2: Transcription
|
| 100 |
transcript = transcribe_audio(audio_path)
|
| 101 |
if transcript.startswith("❌"):
|
| 102 |
return transcript, "", ""
|
| 103 |
|
| 104 |
-
# Step 3: Summarization
|
| 105 |
summary = summarize_text(transcript, lang)
|
| 106 |
-
|
| 107 |
return "✅ Transcription Completed!", transcript, summary
|
| 108 |
|
| 109 |
-
#
|
| 110 |
-
|
| 111 |
-
#
|
| 112 |
-
with gr.Blocks(title="🎧 Audio & YouTube Transcriber + Summarizer") as app:
|
| 113 |
-
gr.Markdown("## 🎧 English/Urdu Audio Summarizer\nUpload an audio file **or** paste a YouTube link below:")
|
| 114 |
|
| 115 |
with gr.Row():
|
| 116 |
youtube_link = gr.Textbox(label="📺 YouTube Link (optional)")
|
| 117 |
-
language_choice = gr.Dropdown(
|
| 118 |
-
["English", "Urdu"],
|
| 119 |
-
label="🌐 Choose Summary Language",
|
| 120 |
-
value="English"
|
| 121 |
-
)
|
| 122 |
|
| 123 |
audio_input = gr.Audio(type="filepath", label="🎙️ Upload Audio File (optional)")
|
| 124 |
btn = gr.Button("🚀 Transcribe & Summarize")
|
| 125 |
|
| 126 |
-
|
| 127 |
-
status = gr.Textbox(label="Status")
|
| 128 |
transcript_box = gr.Textbox(label="📝 Transcription", lines=8)
|
| 129 |
summary_box = gr.Textbox(label="🧩 Summary", lines=8)
|
| 130 |
|
| 131 |
-
btn.click(
|
| 132 |
-
fn=process_input,
|
| 133 |
-
inputs=[youtube_link, audio_input, language_choice],
|
| 134 |
-
outputs=[status, transcript_box, summary_box]
|
| 135 |
-
)
|
| 136 |
|
| 137 |
app.launch()
|
|
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
|
|
|
| 3 |
import tempfile
|
| 4 |
import yt_dlp
|
| 5 |
+
from pydub import AudioSegment
|
| 6 |
from groq import Groq
|
| 7 |
from huggingface_hub import InferenceClient
|
| 8 |
|
|
|
|
| 9 |
# ✅ Environment Variables
|
|
|
|
| 10 |
GROQ_API_KEY = os.getenv("GROQ_API_KEY")
|
| 11 |
HUGGINGFACE_API_TOKEN = os.getenv("HUGGINGFACE_API_TOKEN")
|
| 12 |
|
| 13 |
if not GROQ_API_KEY or not HUGGINGFACE_API_TOKEN:
|
| 14 |
+
raise EnvironmentError("Please set GROQ_API_KEY and HUGGINGFACE_API_TOKEN.")
|
| 15 |
|
|
|
|
|
|
|
|
|
|
| 16 |
groq_client = Groq(api_key=GROQ_API_KEY)
|
| 17 |
hf_client = InferenceClient(token=HUGGINGFACE_API_TOKEN)
|
| 18 |
|
|
|
|
| 19 |
# ✅ Download YouTube Audio
|
|
|
|
| 20 |
def download_youtube_audio(youtube_url):
|
| 21 |
+
with tempfile.NamedTemporaryFile(suffix=".mp3", delete=False) as tmp_file:
|
| 22 |
+
ydl_opts = {
|
| 23 |
+
"format": "bestaudio/best",
|
| 24 |
+
"outtmpl": tmp_file.name,
|
| 25 |
+
"quiet": True,
|
| 26 |
+
"postprocessors": [{
|
| 27 |
+
"key": "FFmpegExtractAudio",
|
| 28 |
+
"preferredcodec": "mp3",
|
| 29 |
+
"preferredquality": "192",
|
| 30 |
+
}],
|
| 31 |
+
}
|
| 32 |
+
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
| 33 |
+
ydl.download([youtube_url])
|
| 34 |
+
return tmp_file.name
|
| 35 |
+
|
| 36 |
+
# ✅ Split long audio into chunks (max 5 mins)
|
| 37 |
+
def split_audio(file_path, max_duration_ms=5*60*1000):
|
| 38 |
+
audio = AudioSegment.from_file(file_path)
|
| 39 |
+
chunks = []
|
| 40 |
+
for i in range(0, len(audio), max_duration_ms):
|
| 41 |
+
chunk = audio[i:i + max_duration_ms]
|
| 42 |
+
temp_chunk = tempfile.NamedTemporaryFile(suffix=".mp3", delete=False)
|
| 43 |
+
chunk.export(temp_chunk.name, format="mp3")
|
| 44 |
+
chunks.append(temp_chunk.name)
|
| 45 |
+
return chunks
|
| 46 |
+
|
| 47 |
+
# ✅ Transcribe with Groq (chunk-wise)
|
| 48 |
def transcribe_audio(audio_path):
|
| 49 |
try:
|
| 50 |
+
chunks = split_audio(audio_path)
|
| 51 |
+
transcript = ""
|
| 52 |
+
for i, chunk in enumerate(chunks):
|
| 53 |
+
with open(chunk, "rb") as f:
|
| 54 |
+
response = groq_client.audio.transcriptions.create(
|
| 55 |
+
model="whisper-large-v3",
|
| 56 |
+
file=f
|
| 57 |
+
)
|
| 58 |
+
transcript += response.text + "\n"
|
| 59 |
+
return transcript.strip()
|
| 60 |
except Exception as e:
|
| 61 |
return f"❌ Error during transcription: {e}"
|
| 62 |
|
| 63 |
+
# ✅ Summarize
|
|
|
|
|
|
|
| 64 |
def summarize_text(text, lang):
|
| 65 |
try:
|
| 66 |
if lang == "English":
|
|
|
|
| 68 |
prompt = f"Summarize the following text in English:\n\n{text}"
|
| 69 |
else:
|
| 70 |
model = "facebook/mbart-large-50-many-to-many-mmt"
|
| 71 |
+
prompt = f"مندرجہ ذیل عبارت کا جامع اردو خلاصہ تحریر کریں:\n\n{text}"
|
| 72 |
|
| 73 |
output = hf_client.text_generation(
|
| 74 |
model=model,
|
| 75 |
prompt=prompt,
|
| 76 |
max_new_tokens=250,
|
| 77 |
temperature=0.7,
|
|
|
|
| 78 |
)
|
| 79 |
return output
|
| 80 |
except Exception as e:
|
| 81 |
return f"❌ Error during summarization: {e}"
|
| 82 |
|
| 83 |
+
# ✅ Main Pipeline
|
|
|
|
|
|
|
| 84 |
def process_input(youtube_url, audio_file, lang):
|
|
|
|
|
|
|
|
|
|
| 85 |
if youtube_url:
|
| 86 |
audio_path = download_youtube_audio(youtube_url)
|
|
|
|
|
|
|
| 87 |
elif audio_file:
|
| 88 |
audio_path = audio_file
|
| 89 |
else:
|
| 90 |
+
return "❌ Please upload an audio file or paste a YouTube link.", "", ""
|
| 91 |
|
|
|
|
| 92 |
transcript = transcribe_audio(audio_path)
|
| 93 |
if transcript.startswith("❌"):
|
| 94 |
return transcript, "", ""
|
| 95 |
|
|
|
|
| 96 |
summary = summarize_text(transcript, lang)
|
|
|
|
| 97 |
return "✅ Transcription Completed!", transcript, summary
|
| 98 |
|
| 99 |
+
# ✅ Gradio Interface
|
| 100 |
+
with gr.Blocks(title="🎧 Urdu/English Audio Summarizer") as app:
|
| 101 |
+
gr.Markdown("## 🎧 Urdu & English Audio Summarizer\nUpload audio or paste YouTube link below:")
|
|
|
|
|
|
|
| 102 |
|
| 103 |
with gr.Row():
|
| 104 |
youtube_link = gr.Textbox(label="📺 YouTube Link (optional)")
|
| 105 |
+
language_choice = gr.Dropdown(["English", "Urdu"], value="English", label="🌐 Summary Language")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 106 |
|
| 107 |
audio_input = gr.Audio(type="filepath", label="🎙️ Upload Audio File (optional)")
|
| 108 |
btn = gr.Button("🚀 Transcribe & Summarize")
|
| 109 |
|
| 110 |
+
status = gr.Textbox(label="Status")
|
|
|
|
| 111 |
transcript_box = gr.Textbox(label="📝 Transcription", lines=8)
|
| 112 |
summary_box = gr.Textbox(label="🧩 Summary", lines=8)
|
| 113 |
|
| 114 |
+
btn.click(process_input, [youtube_link, audio_input, language_choice], [status, transcript_box, summary_box])
|
|
|
|
|
|
|
|
|
|
|
|
|
| 115 |
|
| 116 |
app.launch()
|