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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +55 -49
app.py CHANGED
@@ -6,19 +6,20 @@ 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 = {
@@ -34,60 +35,63 @@ def download_youtube_audio(url: str):
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(
40
  ["ffmpeg", "-y", "-i", audio_path, "-ar", "16000", "-ac", "1", wav_path],
41
  check=True,
42
- capture_output=True
43
  )
44
  return wav_path
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}",
@@ -97,14 +101,15 @@ def summarize_text(text, language):
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)
@@ -112,30 +117,31 @@ def generate_tutorial(transcription, summary, language):
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()
 
6
  import requests
7
  from huggingface_hub import InferenceClient
8
 
9
+ # ---------------------------
10
+ # Model setup
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
+ # Helper Functions
20
+ # ---------------------------
21
  def download_youtube_audio(url: str):
22
+ """Download YouTube audio as mp3"""
23
  try:
24
  tmp = tempfile.NamedTemporaryFile(delete=False, suffix=".mp3")
25
  ydl_opts = {
 
35
  raise RuntimeError(f"❌ Error downloading YouTube audio: {e}")
36
 
37
  def convert_to_wav(audio_path: str):
38
+ """Convert any audio to 16kHz mono WAV"""
39
  wav_path = tempfile.mktemp(suffix=".wav")
40
  try:
41
  subprocess.run(
42
  ["ffmpeg", "-y", "-i", audio_path, "-ar", "16000", "-ac", "1", wav_path],
43
  check=True,
44
+ capture_output=True,
45
  )
46
  return wav_path
47
  except subprocess.CalledProcessError as e:
48
  raise RuntimeError(f"❌ ffmpeg conversion failed: {e}")
49
 
50
+ # ---------------------------
51
  # Transcription
52
+ # ---------------------------
53
  def transcribe_audio(audio_file=None, youtube_url=None):
54
  try:
55
  if youtube_url and youtube_url.strip():
56
  audio_path = download_youtube_audio(youtube_url)
57
+ elif audio_file:
58
  audio_path = audio_file
59
+ else:
60
+ return "❌ Please record, upload, or provide a YouTube link."
61
 
62
  wav_path = convert_to_wav(audio_path)
63
 
64
+ headers = {
65
+ "Authorization": f"Bearer {os.environ.get('HUGGINGFACE_API_TOKEN','')}",
66
+ "Content-Type": "audio/wav"
67
+ }
68
+ api_url = f"https://api-inference.huggingface.co/models/{WHISPER_MODEL}"
69
+
70
+ with open(wav_path, "rb") as f:
71
+ response = requests.post(api_url, headers=headers, data=f.read())
72
+
73
+ if response.status_code != 200:
74
+ raise RuntimeError(f"HF API error: {response.text}")
75
+
76
+ data = response.json()
77
+ if isinstance(data, dict) and "text" in data:
78
+ return data["text"]
79
+ elif isinstance(data, list) and "text" in data[0]:
80
+ return data[0]["text"]
81
+ else:
82
  return str(data)
83
  except Exception as e:
84
  return f"❌ Error during transcription: {e}"
85
 
86
+ # ---------------------------
87
  # Summarization
88
+ # ---------------------------
89
  def summarize_text(text, language):
90
  try:
91
  if language == "English":
92
  result = client.summarization(model=EN_SUMMARY_MODEL, inputs=text, max_new_tokens=1024)
93
  return result["summary_text"]
94
  else:
 
95
  resp = client.text_generation(
96
  model=UR_SUMMARY_MODEL,
97
  prompt=f"مندرجہ ذیل انگریزی متن کا جامع اردو خلاصہ لکھیں:\n\n{text}",
 
101
  except Exception as e:
102
  return f"❌ Summarization failed: {e}"
103
 
104
+ # ---------------------------
105
+ # Tutorial Generator
106
+ # ---------------------------
107
  def generate_tutorial(transcription, summary, language):
108
  try:
109
  prompt = (
110
+ f"Create a comprehensive, beginner-friendly tutorial in {language} "
111
+ f"based on the following transcript and summary.\n\n"
112
+ f"Transcript:\n{transcription}\n\nSummary:\n{summary}"
113
  )
114
  model = UR_SUMMARY_MODEL if language == "Urdu" else EN_SUMMARY_MODEL
115
  result = client.text_generation(model=model, prompt=prompt, max_new_tokens=2200)
 
117
  except Exception as e:
118
  return f"❌ Error generating tutorial: {e}"
119
 
120
+ # ---------------------------
121
+ # Gradio UI
122
+ # ---------------------------
123
  with gr.Blocks(title="🎙️ Smart Transcriber & Tutorial Maker") as demo:
124
+ gr.Markdown("## 🎧 Smart Transcriber, Summarizer & Tutorial Creator")
125
 
126
  with gr.Row():
127
+ audio_input = gr.Audio(sources=["microphone", "upload"], type="filepath", label="🎙️ Record or Upload Audio")
128
+ yt_input = gr.Textbox(label="🎥 Or paste YouTube link")
129
 
130
  trans_btn = gr.Button("🚀 Transcribe")
131
+ transcript_output = gr.Textbox(label="📝 Transcription", lines=8)
132
 
133
  with gr.Row():
134
+ lang_choice = gr.Radio(["English", "Urdu"], label="Select Summary Language", value="English")
135
+
136
  sum_btn = gr.Button("🧠 Generate Detailed Summary")
137
+ summary_output = gr.Textbox(label="📋 Summary", lines=10)
138
 
139
  tut_btn = gr.Button("📘 Create Beginner Tutorial")
140
+ tutorial_output = gr.Textbox(label="🎓 Tutorial", lines=12)
141
 
142
+ # Button actions
143
+ trans_btn.click(transcribe_audio, [audio_input, yt_input], transcript_output)
144
+ sum_btn.click(summarize_text, [transcript_output, lang_choice], summary_output)
145
+ tut_btn.click(generate_tutorial, [transcript_output, summary_output, lang_choice], tutorial_output)
146
 
147
  demo.launch()