Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,146 +1,128 @@
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
-
import tempfile
|
| 4 |
import yt_dlp
|
| 5 |
import subprocess
|
| 6 |
-
from pydub import AudioSegment
|
| 7 |
from groq import Groq
|
|
|
|
| 8 |
|
| 9 |
-
#
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
raise EnvironmentError("Please set GROQ_API_KEY.")
|
| 13 |
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
def convert_to_wav(input_path):
|
| 18 |
try:
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
["ffmpeg", "-y", "-i", str(input_path), "-ar", "16000", "-ac", "1", out_wav],
|
| 23 |
stdout=subprocess.PIPE,
|
| 24 |
stderr=subprocess.PIPE
|
| 25 |
)
|
| 26 |
-
|
| 27 |
-
audio = AudioSegment.from_file(input_path)
|
| 28 |
-
audio = audio.set_frame_rate(16000).set_channels(1)
|
| 29 |
-
audio.export(out_wav, format="wav")
|
| 30 |
-
return out_wav
|
| 31 |
except Exception as e:
|
| 32 |
-
raise RuntimeError(f"❌
|
| 33 |
-
|
| 34 |
-
# ✅ Split long audio into 5-min chunks
|
| 35 |
-
def split_audio(file_path, max_duration_ms=5*60*1000):
|
| 36 |
-
audio = AudioSegment.from_file(file_path)
|
| 37 |
-
chunks = []
|
| 38 |
-
for i in range(0, len(audio), max_duration_ms):
|
| 39 |
-
chunk = audio[i:i + max_duration_ms]
|
| 40 |
-
temp_chunk = tempfile.NamedTemporaryFile(suffix=".wav", delete=False)
|
| 41 |
-
chunk.export(temp_chunk.name, format="wav")
|
| 42 |
-
chunks.append(temp_chunk.name)
|
| 43 |
-
return chunks
|
| 44 |
|
| 45 |
-
#
|
| 46 |
def download_youtube_audio(youtube_url):
|
| 47 |
try:
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
except Exception as e:
|
| 63 |
raise RuntimeError(f"❌ Error downloading YouTube audio: {e}")
|
| 64 |
|
| 65 |
-
#
|
| 66 |
-
def transcribe_audio(
|
| 67 |
try:
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
|
|
|
|
|
|
|
|
|
| 78 |
except Exception as e:
|
| 79 |
-
return f"❌ Error during transcription: {e}"
|
| 80 |
|
| 81 |
-
#
|
| 82 |
-
def summarize_text(
|
| 83 |
try:
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
|
|
|
|
|
|
| 96 |
except Exception as e:
|
| 97 |
return f"❌ Summarization failed: {e}"
|
| 98 |
|
| 99 |
-
#
|
| 100 |
-
def
|
| 101 |
try:
|
| 102 |
-
if
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
return f"❌ Error downloading YouTube audio: {e}", ""
|
| 107 |
-
elif audio_file:
|
| 108 |
-
audio_path = audio_file
|
| 109 |
else:
|
| 110 |
-
|
| 111 |
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
|
|
|
| 117 |
except Exception as e:
|
| 118 |
-
return f"❌ Error: {e}"
|
| 119 |
|
| 120 |
-
#
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
return summarize_text(transcript, lang)
|
| 125 |
-
|
| 126 |
-
# ✅ Gradio Interface
|
| 127 |
-
with gr.Blocks(title="🎧 Urdu & English Audio Transcriber + Summarizer") as app:
|
| 128 |
-
gr.Markdown("## 🎧 AI Audio & YouTube Transcriber — English & Urdu")
|
| 129 |
|
| 130 |
with gr.Row():
|
| 131 |
-
|
| 132 |
-
|
| 133 |
|
| 134 |
-
|
|
|
|
| 135 |
|
| 136 |
-
transcribe_btn = gr.Button("
|
| 137 |
-
|
|
|
|
| 138 |
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
|
| 143 |
-
transcribe_btn.click(
|
| 144 |
-
|
|
|
|
| 145 |
|
| 146 |
app.launch()
|
|
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
|
|
|
| 3 |
import yt_dlp
|
| 4 |
import subprocess
|
|
|
|
| 5 |
from groq import Groq
|
| 6 |
+
from huggingface_hub import InferenceClient
|
| 7 |
|
| 8 |
+
# Initialize clients
|
| 9 |
+
groq_client = Groq(api_key=os.getenv("GROQ_API_KEY"))
|
| 10 |
+
hf_client = InferenceClient("facebook/bart-large-cnn")
|
|
|
|
| 11 |
|
| 12 |
+
# 🔹 Convert audio to WAV format for transcription
|
| 13 |
+
def convert_to_wav(audio_path):
|
| 14 |
+
wav_path = audio_path.replace(".mp3", ".wav")
|
|
|
|
| 15 |
try:
|
| 16 |
+
subprocess.run(
|
| 17 |
+
["ffmpeg", "-y", "-i", audio_path, "-ar", "16000", "-ac", "1", wav_path],
|
| 18 |
+
check=True,
|
|
|
|
| 19 |
stdout=subprocess.PIPE,
|
| 20 |
stderr=subprocess.PIPE
|
| 21 |
)
|
| 22 |
+
return wav_path
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
except Exception as e:
|
| 24 |
+
raise RuntimeError(f"❌ ffmpeg conversion failed: {e}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
+
# 🔹 Download YouTube audio
|
| 27 |
def download_youtube_audio(youtube_url):
|
| 28 |
try:
|
| 29 |
+
output_path = "youtube_audio.wav"
|
| 30 |
+
ydl_opts = {
|
| 31 |
+
"format": "bestaudio/best",
|
| 32 |
+
"outtmpl": "youtube_audio.%(ext)s",
|
| 33 |
+
"quiet": True,
|
| 34 |
+
"postprocessors": [{
|
| 35 |
+
"key": "FFmpegExtractAudio",
|
| 36 |
+
"preferredcodec": "wav",
|
| 37 |
+
"preferredquality": "192",
|
| 38 |
+
}],
|
| 39 |
+
}
|
| 40 |
+
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
| 41 |
+
ydl.download([youtube_url])
|
| 42 |
+
return output_path
|
| 43 |
except Exception as e:
|
| 44 |
raise RuntimeError(f"❌ Error downloading YouTube audio: {e}")
|
| 45 |
|
| 46 |
+
# 🔹 Transcribe audio
|
| 47 |
+
def transcribe_audio(audio_file=None, youtube_url=None):
|
| 48 |
try:
|
| 49 |
+
if youtube_url:
|
| 50 |
+
audio_file = download_youtube_audio(youtube_url)
|
| 51 |
+
if not audio_file:
|
| 52 |
+
return "❌ Please upload or record audio or provide a YouTube link.", ""
|
| 53 |
+
|
| 54 |
+
wav_file = convert_to_wav(audio_file)
|
| 55 |
+
with open(wav_file, "rb") as f:
|
| 56 |
+
transcription = groq_client.audio.transcriptions.create(
|
| 57 |
+
model="whisper-large-v3",
|
| 58 |
+
file=(wav_file, f, "audio/wav")
|
| 59 |
+
)
|
| 60 |
+
text = transcription.text
|
| 61 |
+
return text, "✅ Transcription complete."
|
| 62 |
except Exception as e:
|
| 63 |
+
return "", f"❌ Error during transcription: {e}"
|
| 64 |
|
| 65 |
+
# 🔹 Summarize text
|
| 66 |
+
def summarize_text(transcription, language):
|
| 67 |
try:
|
| 68 |
+
if not transcription.strip():
|
| 69 |
+
return "❌ Please transcribe audio first."
|
| 70 |
+
if language == "English":
|
| 71 |
+
summary = hf_client.summarization(transcription, max_length=250, min_length=100)
|
| 72 |
+
return summary[0]["summary_text"]
|
| 73 |
+
else:
|
| 74 |
+
# For Urdu, use Groq model
|
| 75 |
+
prompt = f"اس انگریزی متن کا خلاصہ تفصیلی اردو میں لکھیں:\n\n{transcription}"
|
| 76 |
+
response = groq_client.chat.completions.create(
|
| 77 |
+
model="openai/gpt-oss-120b",
|
| 78 |
+
messages=[{"role": "user", "content": prompt}],
|
| 79 |
+
temperature=0.5
|
| 80 |
+
)
|
| 81 |
+
return response.choices[0].message.content
|
| 82 |
except Exception as e:
|
| 83 |
return f"❌ Summarization failed: {e}"
|
| 84 |
|
| 85 |
+
# 🔹 Craft tutorial
|
| 86 |
+
def craft_tutorial(transcription, language):
|
| 87 |
try:
|
| 88 |
+
if not transcription.strip():
|
| 89 |
+
return "❌ Please transcribe or summarize first."
|
| 90 |
+
if language == "English":
|
| 91 |
+
prompt = f"Create a comprehensive beginner-friendly tutorial based on this transcription:\n\n{transcription}"
|
|
|
|
|
|
|
|
|
|
| 92 |
else:
|
| 93 |
+
prompt = f"اس ٹرانسکرپشن کی بنیاد پر اردو میں ایک جامع، آسان، تعلیمی ٹیوٹوریل لکھیں جو بالکل ابتدائی افراد کے لیے ہو۔\n\n{transcription}"
|
| 94 |
|
| 95 |
+
response = groq_client.chat.completions.create(
|
| 96 |
+
model="openai/gpt-oss-120b",
|
| 97 |
+
messages=[{"role": "user", "content": prompt}],
|
| 98 |
+
temperature=0.6
|
| 99 |
+
)
|
| 100 |
+
return response.choices[0].message.content
|
| 101 |
except Exception as e:
|
| 102 |
+
return f"❌ Error creating tutorial: {e}"
|
| 103 |
|
| 104 |
+
# 🔹 Gradio Interface
|
| 105 |
+
with gr.Blocks(title="🎧 AI Audio Transcriber & Educator") as app:
|
| 106 |
+
gr.Markdown("## 🎙️ AI Audio Transcriber + Summarizer + Tutorial Generator")
|
| 107 |
+
gr.Markdown("Transcribe, summarize, and learn from audio (English or Urdu).")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 108 |
|
| 109 |
with gr.Row():
|
| 110 |
+
audio_input = gr.Audio(label="🎧 Upload or Record Audio", type="filepath", sources=["microphone", "upload"])
|
| 111 |
+
youtube_url = gr.Textbox(label="🔗 YouTube Link (optional)")
|
| 112 |
|
| 113 |
+
with gr.Row():
|
| 114 |
+
language = gr.Radio(["English", "Urdu"], label="🌍 Output Language", value="English")
|
| 115 |
|
| 116 |
+
transcribe_btn = gr.Button("🚀 Transcribe Audio")
|
| 117 |
+
summary_btn = gr.Button("🧠 Generate Detailed Summary")
|
| 118 |
+
tutorial_btn = gr.Button("📘 Craft Tutorial")
|
| 119 |
|
| 120 |
+
transcription_output = gr.Textbox(label="📝 Transcription", lines=8)
|
| 121 |
+
summary_output = gr.Textbox(label="🧾 Summary", lines=6)
|
| 122 |
+
tutorial_output = gr.Textbox(label="📚 Tutorial", lines=10)
|
| 123 |
|
| 124 |
+
transcribe_btn.click(transcribe_audio, inputs=[audio_input, youtube_url], outputs=[transcription_output, gr.Textbox()])
|
| 125 |
+
summary_btn.click(summarize_text, inputs=[transcription_output, language], outputs=summary_output)
|
| 126 |
+
tutorial_btn.click(craft_tutorial, inputs=[transcription_output, language], outputs=tutorial_output)
|
| 127 |
|
| 128 |
app.launch()
|