Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,161 +1,137 @@
|
|
| 1 |
-
# =========================
|
| 2 |
-
# SmartTranscribe - Updated Version (For Hugging Face Spaces)
|
| 3 |
-
# =========================
|
| 4 |
-
|
| 5 |
import os
|
| 6 |
import gradio as gr
|
| 7 |
import requests
|
| 8 |
-
from groq import Groq
|
| 9 |
-
from datetime import datetime
|
| 10 |
-
from pathlib import Path
|
| 11 |
import tempfile
|
|
|
|
|
|
|
| 12 |
from huggingface_hub import InferenceClient
|
| 13 |
|
| 14 |
-
# -------------------------
|
| 15 |
-
# Environment Variables
|
| 16 |
-
# -------------------------
|
| 17 |
-
GROQ_API_KEY = os.
|
| 18 |
-
HUGGINGFACE_API_TOKEN = os.
|
| 19 |
|
| 20 |
if not GROQ_API_KEY or not HUGGINGFACE_API_TOKEN:
|
| 21 |
-
raise EnvironmentError(
|
| 22 |
-
"Environment variables GROQ_API_KEY and HUGGINGFACE_API_TOKEN must be set."
|
| 23 |
-
)
|
| 24 |
|
| 25 |
-
#
|
|
|
|
|
|
|
| 26 |
groq_client = Groq(api_key=GROQ_API_KEY)
|
| 27 |
-
|
| 28 |
-
# Initialize Hugging Face Inference Client
|
| 29 |
hf_client = InferenceClient(token=HUGGINGFACE_API_TOKEN)
|
| 30 |
|
| 31 |
-
# -------------------------
|
| 32 |
-
#
|
| 33 |
-
# -------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
"""
|
| 40 |
try:
|
| 41 |
-
with open(audio_path, "rb") as
|
| 42 |
-
|
| 43 |
-
model="whisper-large-v3
|
| 44 |
-
file=
|
| 45 |
-
response_format="text"
|
| 46 |
)
|
| 47 |
-
|
| 48 |
-
transcript = response.strip()
|
| 49 |
-
|
| 50 |
-
# Urdu-English post-processing (convert English words in Urdu audio into Urdu script)
|
| 51 |
-
transcript = normalize_transcription(transcript)
|
| 52 |
-
|
| 53 |
-
return transcript
|
| 54 |
-
|
| 55 |
except Exception as e:
|
| 56 |
-
return f"❌ Error during transcription: {
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
You can enhance this later with a proper transliteration module.
|
| 63 |
-
"""
|
| 64 |
-
replacements = {
|
| 65 |
-
"school": "اسکول",
|
| 66 |
-
"teacher": "ٹیچر",
|
| 67 |
-
"student": "سٹوڈنٹ",
|
| 68 |
-
"education": "ایجوکیشن",
|
| 69 |
-
"university": "یونیورسٹی",
|
| 70 |
-
"computer": "کمپیوٹر",
|
| 71 |
-
"mobile": "موبائل",
|
| 72 |
-
"class": "کلاس",
|
| 73 |
-
}
|
| 74 |
-
for eng, urdu in replacements.items():
|
| 75 |
-
text = text.replace(eng, urdu)
|
| 76 |
-
return text
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
def summarize_text(text):
|
| 80 |
-
"""
|
| 81 |
-
Summarize text using openai/gpt-oss-120b model from Hugging Face.
|
| 82 |
-
"""
|
| 83 |
try:
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
max_new_tokens=250,
|
| 89 |
-
temperature=0.
|
|
|
|
| 90 |
)
|
| 91 |
-
return
|
| 92 |
except Exception as e:
|
| 93 |
-
return f"❌ Error during summarization: {
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
transcript = transcribe_audio(audio_path)
|
| 102 |
if transcript.startswith("❌"):
|
| 103 |
-
return transcript, ""
|
| 104 |
-
|
| 105 |
-
summary = summarize_text(transcript)
|
| 106 |
-
return transcript, summary
|
| 107 |
|
|
|
|
|
|
|
| 108 |
|
| 109 |
-
|
| 110 |
-
# Gradio Interface
|
| 111 |
-
# -------------------------
|
| 112 |
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
with gr.Tab("🎤 Upload or Record"):
|
| 127 |
-
audio_input = gr.Audio(
|
| 128 |
-
sources=["microphone", "upload"], # ✅ Updated syntax
|
| 129 |
-
type="filepath",
|
| 130 |
-
label="Upload or Record audio/video"
|
| 131 |
)
|
| 132 |
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
summary_output = gr.Textbox(label="📄 Summary", lines=8)
|
| 136 |
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
)
|
| 142 |
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
- Post-processes mixed Urdu-English text into clean, grammatically correct Urdu
|
| 149 |
-
- Generates summaries with **openai/gpt-oss-120b**
|
| 150 |
-
|
| 151 |
-
### 🔒 Privacy
|
| 152 |
-
Your files and text are **not stored** after processing.
|
| 153 |
-
All processing happens temporarily in memory.
|
| 154 |
-
"""
|
| 155 |
-
)
|
| 156 |
|
| 157 |
-
|
| 158 |
-
# Launch App
|
| 159 |
-
# -------------------------
|
| 160 |
-
if __name__ == "__main__":
|
| 161 |
-
app.launch(server_name="0.0.0.0", server_port=7860)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 in Hugging Face settings.")
|
|
|
|
|
|
|
| 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 |
+
try:
|
| 29 |
+
with tempfile.NamedTemporaryFile(suffix=".mp3", delete=False) as tmp_file:
|
| 30 |
+
ydl_opts = {
|
| 31 |
+
'format': 'bestaudio/best',
|
| 32 |
+
'outtmpl': tmp_file.name,
|
| 33 |
+
'quiet': True,
|
| 34 |
+
'postprocessors': [{
|
| 35 |
+
'key': 'FFmpegExtractAudio',
|
| 36 |
+
'preferredcodec': 'mp3',
|
| 37 |
+
'preferredquality': '192',
|
| 38 |
+
}],
|
| 39 |
+
}
|
| 40 |
+
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
| 41 |
+
ydl.download([youtube_url])
|
| 42 |
+
return tmp_file.name
|
| 43 |
+
except Exception as e:
|
| 44 |
+
return f"❌ Error downloading audio: {e}"
|
| 45 |
|
| 46 |
+
# ----------------------------
|
| 47 |
+
# ✅ Transcribe with Groq Whisper
|
| 48 |
+
# ----------------------------
|
| 49 |
+
def transcribe_audio(audio_path):
|
|
|
|
| 50 |
try:
|
| 51 |
+
with open(audio_path, "rb") as f:
|
| 52 |
+
transcription = groq_client.audio.transcriptions.create(
|
| 53 |
+
model="whisper-large-v3",
|
| 54 |
+
file=f
|
|
|
|
| 55 |
)
|
| 56 |
+
return transcription.text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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":
|
| 66 |
+
model = "facebook/bart-large-cnn"
|
| 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"مندرجہ ذیل انگریزی متن کا جامع اردو خلاصہ لکھیں:\n\n{text}"
|
| 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 |
+
# ✅ Gradio UI
|
| 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 |
+
with gr.Row():
|
| 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()
|
|
|
|
|
|
|
|
|
|
|
|