Turbiling commited on
Commit
022d09b
·
verified ·
1 Parent(s): b16b9a6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +90 -108
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
- # Environment Variable Check
10
- GROQ_API_KEY = os.getenv("GROQ_API_KEY")
11
- if not GROQ_API_KEY:
12
- raise EnvironmentError("Please set GROQ_API_KEY.")
13
 
14
- groq_client = Groq(api_key=GROQ_API_KEY)
15
-
16
- # Convert audio to 16kHz mono WAV
17
- def convert_to_wav(input_path):
18
  try:
19
- tmp_wav = tempfile.NamedTemporaryFile(suffix=".wav", delete=False, prefix="conv_")
20
- out_wav = tmp_wav.name
21
- result = subprocess.run(
22
- ["ffmpeg", "-y", "-i", str(input_path), "-ar", "16000", "-ac", "1", out_wav],
23
  stdout=subprocess.PIPE,
24
  stderr=subprocess.PIPE
25
  )
26
- if result.returncode != 0:
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"❌ Error converting to WAV: {e}")
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
- # Download YouTube Audio (with clear error messages)
46
  def download_youtube_audio(youtube_url):
47
  try:
48
- with tempfile.NamedTemporaryFile(suffix=".mp3", delete=False) as tmp_file:
49
- ydl_opts = {
50
- "format": "bestaudio/best",
51
- "outtmpl": tmp_file.name,
52
- "quiet": True,
53
- "postprocessors": [{
54
- "key": "FFmpegExtractAudio",
55
- "preferredcodec": "mp3",
56
- "preferredquality": "192",
57
- }],
58
- }
59
- with yt_dlp.YoutubeDL(ydl_opts) as ydl:
60
- ydl.download([youtube_url])
61
- return tmp_file.name
62
  except Exception as e:
63
  raise RuntimeError(f"❌ Error downloading YouTube audio: {e}")
64
 
65
- # Transcription using Groq Whisper
66
- def transcribe_audio(audio_path):
67
  try:
68
- chunks = split_audio(audio_path)
69
- transcript = ""
70
- for chunk in chunks:
71
- with open(chunk, "rb") as f:
72
- response = groq_client.audio.transcriptions.create(
73
- model="whisper-large-v3",
74
- file=f
75
- )
76
- transcript += response.text + "\n"
77
- return transcript.strip()
 
 
 
78
  except Exception as e:
79
- return f"❌ Error during transcription: {e}"
80
 
81
- # Summarization with “Detailed Mode”
82
- def summarize_text(text, lang):
83
  try:
84
- prompt = (
85
- f"Create a detailed, structured and comprehensive English summary of the following text. "
86
- f"Cover key points, ideas, examples and conclusions clearly:\n\n{text}"
87
- if lang == "English"
88
- else f"مندرجہ ذیل عبار�� کا تفصیلی، منظم اور جامع اردو خلاصہ تحریر کریں۔ خلاصے میں اہم نکات، مثالیں اور نتائج کو واضح طور پر بیان کریں:\n\n{text}"
89
- )
90
- response = groq_client.chat.completions.create(
91
- model="openai/gpt-oss-120b",
92
- messages=[{"role": "user", "content": prompt}],
93
- temperature=0.6,
94
- )
95
- return response.choices[0].message.content.strip()
 
 
96
  except Exception as e:
97
  return f"❌ Summarization failed: {e}"
98
 
99
- # Step 1: Transcription
100
- def process_transcription(youtube_url, audio_file):
101
  try:
102
- if youtube_url:
103
- try:
104
- audio_path = download_youtube_audio(youtube_url)
105
- except Exception as e:
106
- return f"❌ Error downloading YouTube audio: {e}", ""
107
- elif audio_file:
108
- audio_path = audio_file
109
  else:
110
- return " Please upload an audio or paste YouTube link.", ""
111
 
112
- wav_path = convert_to_wav(audio_path)
113
- transcript = transcribe_audio(wav_path)
114
- if transcript.startswith(""):
115
- return transcript, ""
116
- return "✅ Transcription Completed!", transcript
 
117
  except Exception as e:
118
- return f"❌ Error: {e}", ""
119
 
120
- # Step 2: Generate Detailed Summary
121
- def process_summary(transcript, lang):
122
- if not transcript or transcript.startswith("❌"):
123
- return " Please transcribe audio first."
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
- youtube_link = gr.Textbox(label="📺 YouTube Link (optional)")
132
- lang_choice = gr.Dropdown(["English", "Urdu"], value="English", label="🌐 Summary Language")
133
 
134
- audio_input = gr.Audio(type="filepath", label="🎙️ Upload Audio (optional)")
 
135
 
136
- transcribe_btn = gr.Button("📝 Step 1: Transcribe Audio / Video")
137
- summarize_btn = gr.Button("🧩 Step 2: Generate Comprehensive Summary")
 
138
 
139
- status = gr.Textbox(label="Status")
140
- transcript_box = gr.Textbox(label="📝 Transcription", lines=8)
141
- summary_box = gr.Textbox(label="📘 Detailed Summary", lines=8)
142
 
143
- transcribe_btn.click(process_transcription, [youtube_link, audio_input], [status, transcript_box])
144
- summarize_btn.click(process_summary, [transcript_box, lang_choice], [summary_box])
 
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()