Turbiling commited on
Commit
a64985a
·
verified ·
1 Parent(s): b9dba32

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +86 -66
app.py CHANGED
@@ -3,33 +3,37 @@ import os
3
  import tempfile
4
  import yt_dlp
5
  import subprocess
 
6
  from huggingface_hub import InferenceClient
7
 
8
- # ✅ Initialize clients
9
- whisper_client = InferenceClient("openai/whisper-large-v3")
10
- english_summarizer = InferenceClient("facebook/bart-large-cnn")
11
- urdu_summarizer = InferenceClient("openai/gpt-oss-120b")
 
 
12
 
13
- # --- Helper Functions ---
14
 
15
- def download_youtube_audio(url):
16
- """Download YouTube audio using yt_dlp"""
 
 
17
  try:
18
- with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as tmp:
19
- ydl_opts = {
20
- "format": "bestaudio/best",
21
- "outtmpl": tmp.name,
22
- "quiet": True,
23
- "postprocessors": [{"key": "FFmpegExtractAudio", "preferredcodec": "mp3"}],
24
- }
25
- with yt_dlp.YoutubeDL(ydl_opts) as ydl:
26
- ydl.download([url])
27
- return tmp.name
28
  except Exception as e:
29
- raise RuntimeError(f"❌ Error downloading YouTube audio: {str(e)}")
30
 
31
- def convert_to_wav(audio_path):
32
- """Ensure audio is in .wav format"""
33
  wav_path = tempfile.mktemp(suffix=".wav")
34
  try:
35
  subprocess.run(
@@ -41,81 +45,97 @@ def convert_to_wav(audio_path):
41
  except subprocess.CalledProcessError as e:
42
  raise RuntimeError(f"❌ ffmpeg conversion failed: {e}")
43
 
 
 
 
44
  def transcribe_audio(audio_file=None, youtube_url=None):
45
- """Transcribe uploaded or YouTube audio"""
46
  try:
47
- if youtube_url:
48
  audio_path = download_youtube_audio(youtube_url)
49
  else:
50
  audio_path = audio_file
 
 
51
 
52
  wav_path = convert_to_wav(audio_path)
53
 
54
- with open(wav_path, "rb") as f:
55
- audio_data = f.read()
56
-
57
- # Correct way to call Whisper for transcription
58
- response = whisper_client.post(json=None, data=audio_data)
59
- return response.get("text", "❌ No transcription returned.")
 
 
 
 
 
 
 
 
 
 
60
  except Exception as e:
61
  return f"❌ Error during transcription: {e}"
62
 
63
- def summarize_text(transcribed_text, language):
64
- """Generate summary in English or Urdu"""
 
 
65
  try:
66
  if language == "English":
67
- response = english_summarizer.text_generation(
68
- prompt=f"Summarize the following text in detail:\n\n{transcribed_text}",
69
- max_new_tokens=1024,
70
- )
71
- return response
72
  else:
73
- response = urdu_summarizer.text_generation(
74
- prompt=f"مندرجہ ذیل انگریزی متن کا جامع اردو خلاصہ لکھیں:\n\n{transcribed_text}",
 
 
75
  max_new_tokens=2048,
76
  )
77
- return response
78
  except Exception as e:
79
  return f"❌ Summarization failed: {e}"
80
 
81
- def generate_tutorial(transcribed_text, summary, language):
82
- """Craft a beginner-friendly tutorial in Urdu or English"""
 
 
83
  try:
84
- model = urdu_summarizer if language == "Urdu" else english_summarizer
85
  prompt = (
86
- f"Write a simple, step-by-step tutorial in {language} for absolute beginners "
87
- f"based on the following transcript and summary.\n\nTranscript:\n{transcribed_text}\n\nSummary:\n{summary}"
88
  )
89
- response = model.text_generation(prompt=prompt, max_new_tokens=2500)
90
- return response
 
91
  except Exception as e:
92
  return f"❌ Error generating tutorial: {e}"
93
 
94
- # --- Gradio UI ---
 
 
 
 
95
 
96
- with gr.Blocks(title="🎙️ AI Audio Transcriber & Tutorial Maker") as demo:
97
- gr.Markdown("## 🎧 AI Audio Transcriber, Summarizer & Tutorial Creator")
 
98
 
99
- with gr.Tab("🎤 Record / Upload Audio"):
100
- with gr.Row():
101
- audio_input = gr.Audio(sources=["microphone", "upload"], type="filepath", label="🎙️ Record or Upload Audio")
102
- youtube_input = gr.Textbox(label="🎥 Or paste YouTube link")
103
- transcribed_output = gr.Textbox(label="📝 Transcription", lines=8)
104
- transcribe_btn = gr.Button("🚀 Transcribe Audio")
105
 
106
- with gr.Tab("🧠 Summarize"):
107
- language_choice = gr.Radio(["English", "Urdu"], label="Choose Summary Language", value="English")
108
- summary_output = gr.Textbox(label="📋 Detailed Summary", lines=10)
109
- summarize_btn = gr.Button(" Generate Summary")
110
 
111
- with gr.Tab("📘 Tutorial Creator"):
112
- tutorial_output = gr.Textbox(label="🎓 Beginner Tutorial", lines=12)
113
- tutorial_btn = gr.Button("📚 Create Tutorial from Summary")
114
 
115
- # --- Button Actions ---
116
- transcribe_btn.click(fn=transcribe_audio, inputs=[audio_input, youtube_input], outputs=transcribed_output)
117
- summarize_btn.click(fn=summarize_text, inputs=[transcribed_output, language_choice], outputs=summary_output)
118
- tutorial_btn.click(fn=generate_tutorial, inputs=[transcribed_output, summary_output, language_choice], outputs=tutorial_output)
119
 
120
- # Launch
121
  demo.launch()
 
3
  import tempfile
4
  import yt_dlp
5
  import subprocess
6
+ import requests
7
  from huggingface_hub import InferenceClient
8
 
9
+ # ----------------------------
10
+ # Initialize Hugging Face clients
11
+ # ----------------------------
12
+ WHISPER_MODEL = "openai/whisper-large-v3"
13
+ EN_SUMMARY_MODEL = "facebook/bart-large-cnn"
14
+ UR_SUMMARY_MODEL = "openai/gpt-oss-120b"
15
 
16
+ client = InferenceClient()
17
 
18
+ # ----------------------------
19
+ # Helpers
20
+ # ----------------------------
21
+ def download_youtube_audio(url: str):
22
  try:
23
+ tmp = tempfile.NamedTemporaryFile(delete=False, suffix=".mp3")
24
+ ydl_opts = {
25
+ "format": "bestaudio/best",
26
+ "outtmpl": tmp.name,
27
+ "quiet": True,
28
+ "postprocessors": [{"key": "FFmpegExtractAudio", "preferredcodec": "mp3"}],
29
+ }
30
+ with yt_dlp.YoutubeDL(ydl_opts) as ydl:
31
+ ydl.download([url])
32
+ return tmp.name
33
  except Exception as e:
34
+ raise RuntimeError(f"❌ Error downloading YouTube audio: {e}")
35
 
36
+ def convert_to_wav(audio_path: str):
 
37
  wav_path = tempfile.mktemp(suffix=".wav")
38
  try:
39
  subprocess.run(
 
45
  except subprocess.CalledProcessError as e:
46
  raise RuntimeError(f"❌ ffmpeg conversion failed: {e}")
47
 
48
+ # ----------------------------
49
+ # Transcription
50
+ # ----------------------------
51
  def transcribe_audio(audio_file=None, youtube_url=None):
 
52
  try:
53
+ if youtube_url and youtube_url.strip():
54
  audio_path = download_youtube_audio(youtube_url)
55
  else:
56
  audio_path = audio_file
57
+ if not audio_path:
58
+ return "❌ Please upload or record audio or provide a YouTube link."
59
 
60
  wav_path = convert_to_wav(audio_path)
61
 
62
+ # --- Try API helper first ---
63
+ try:
64
+ result = client.audio_to_text(model=WHISPER_MODEL, audio=wav_path)
65
+ return result["text"] if isinstance(result, dict) else str(result)
66
+ except Exception:
67
+ # --- fallback to HTTP call ---
68
+ api_url = f"https://api-inference.huggingface.co/models/{WHISPER_MODEL}"
69
+ headers = {"Authorization": f"Bearer {os.environ.get('HUGGINGFACE_API_TOKEN','')}"}
70
+ with open(wav_path, "rb") as f:
71
+ resp = requests.post(api_url, headers=headers, data=f)
72
+ if resp.status_code != 200:
73
+ raise RuntimeError(f"HF API error: {resp.text}")
74
+ data = resp.json()
75
+ if isinstance(data, list) and len(data) and "text" in data[0]:
76
+ return data[0]["text"]
77
+ return str(data)
78
  except Exception as e:
79
  return f"❌ Error during transcription: {e}"
80
 
81
+ # ----------------------------
82
+ # Summarization
83
+ # ----------------------------
84
+ def summarize_text(text, language):
85
  try:
86
  if language == "English":
87
+ result = client.summarization(model=EN_SUMMARY_MODEL, inputs=text, max_new_tokens=1024)
88
+ return result["summary_text"]
 
 
 
89
  else:
90
+ # Urdu summary via large model
91
+ resp = client.text_generation(
92
+ model=UR_SUMMARY_MODEL,
93
+ prompt=f"مندرجہ ذیل انگریزی متن کا جامع اردو خلاصہ لکھیں:\n\n{text}",
94
  max_new_tokens=2048,
95
  )
96
+ return resp
97
  except Exception as e:
98
  return f"❌ Summarization failed: {e}"
99
 
100
+ # ----------------------------
101
+ # Tutorial Creator
102
+ # ----------------------------
103
+ def generate_tutorial(transcription, summary, language):
104
  try:
 
105
  prompt = (
106
+ f"Write a detailed, beginner-friendly tutorial in {language} based on the following transcription and summary.\n\n"
107
+ f"Transcription:\n{transcription}\n\nSummary:\n{summary}"
108
  )
109
+ model = UR_SUMMARY_MODEL if language == "Urdu" else EN_SUMMARY_MODEL
110
+ result = client.text_generation(model=model, prompt=prompt, max_new_tokens=2200)
111
+ return result
112
  except Exception as e:
113
  return f"❌ Error generating tutorial: {e}"
114
 
115
+ # ----------------------------
116
+ # Gradio Interface
117
+ # ----------------------------
118
+ with gr.Blocks(title="🎙️ Smart Transcriber & Tutorial Maker") as demo:
119
+ gr.Markdown("## 🎧 Smart Transcriber & Tutorial Generator")
120
 
121
+ with gr.Row():
122
+ audio_in = gr.Audio(sources=["microphone", "upload"], type="filepath", label="🎙️ Record / Upload Audio")
123
+ yt_link = gr.Textbox(label="🎥 Or paste YouTube link")
124
 
125
+ trans_btn = gr.Button("🚀 Transcribe")
126
+ transcription = gr.Textbox(label="📝 Transcription", lines=8)
 
 
 
 
127
 
128
+ with gr.Row():
129
+ lang = gr.Radio(["English", "Urdu"], value="English", label="Select Summary Language")
130
+ sum_btn = gr.Button("🧠 Generate Detailed Summary")
131
+ summary_box = gr.Textbox(label="📋 Summary", lines=10)
132
 
133
+ tut_btn = gr.Button("📘 Create Beginner Tutorial")
134
+ tutorial_box = gr.Textbox(label="🎓 Tutorial", lines=12)
 
135
 
136
+ # Actions
137
+ trans_btn.click(transcribe_audio, [audio_in, yt_link], transcription)
138
+ sum_btn.click(summarize_text, [transcription, lang], summary_box)
139
+ tut_btn.click(generate_tutorial, [transcription, summary_box, lang], tutorial_box)
140
 
 
141
  demo.launch()