-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgradio_ui.py
More file actions
319 lines (281 loc) · 11 KB
/
gradio_ui.py
File metadata and controls
319 lines (281 loc) · 11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
import gradio as gr
from fetch_from_file import process_links_from_file
from learn_video_helper import VideoDownloader
from url_generator import generate_urls_from_pattern
import tempfile
import os
import io
import sys
from tqdm import tqdm # keep tqdm for CLI
choices = [
"en-us",
"ru-ru",
"zh-cn",
"zh-tw",
"ko-kr",
"it-it",
"pt-pt",
"ja-jp",
"pl-pl",
"de-de",
"cs-cz",
"es-es",
"fr-fr"
]
default_download_types = ["Video", "Audio", "Subtitles"]
def process_manual_urls_stream(urls_text, languages, download_types):
"""Process manual URLs with streaming updates"""
urls = [url.strip() for url in urls_text.strip().splitlines() if url.strip()]
total = len(urls)
if not urls:
yield "❌ No URLs provided", 0
return
download_high_quality = "Video" in download_types
download_audio = "Audio" in download_types
download_captions = "Subtitles" in download_types
for idx, url in enumerate(urls, 1):
log_lines = [f"🔗 Processing URL {idx}/{total}: {url}"]
yield "\n".join(log_lines), int((idx - 1) / total * 100)
def callback(msg):
log_lines.append(msg)
try:
downloader = VideoDownloader(url)
for percent in run_with_yield_callback(
downloader,
languages,
download_high_quality,
download_audio,
download_captions,
callback
):
yield "\n".join(log_lines), max(percent, int((idx - 1) / total * 100))
except Exception as e:
log_lines.append(f"❌ Error: {e}")
yield "\n".join(log_lines), int((idx - 1) / total * 100)
log_lines.append("✅ Done")
yield "\n".join(log_lines), int(idx / total * 100)
def run_with_yield_callback(
downloader,
languages,
download_high_quality,
download_audio,
download_captions,
callback_fn
):
"""Run downloader with streaming progress updates"""
last_percent = 0
def progress(msg):
nonlocal last_percent
if "—" in msg:
try:
_, percent = msg.rsplit("—", 1)
last_percent = float(percent.strip("% "))
except Exception:
pass
callback_fn(msg)
return last_percent
def yield_msg(msg):
percent = progress(msg)
yield percent
def wrapped():
def inner_callback(msg):
for p in yield_msg(msg):
yield p
# Fixed: proper callback handling
try:
downloader.run_with_callback(
download_high_quality=download_high_quality,
download_medium_quality=False, # Always False - smart fallback in VideoDownloader
download_low_quality=False, # Always False - smart fallback in VideoDownloader
download_audio=download_audio,
download_captions=download_captions,
preferred_languages=languages,
progress_callback=lambda msg: [inner_callback(msg)] # Fixed callback
)
yield 100
except Exception as e:
callback_fn(f"❌ Download failed: {e}")
yield 0
yield from wrapped()
def process_generated_urls_stream(sample_url1, sample_url2, num_links, languages, download_types):
"""Generate URLs and process with streaming updates"""
# First generate URLs
urls = generate_urls_from_pattern(sample_url1.strip(), sample_url2.strip(), num_links)
if not urls:
yield "❌ Error: Could not generate URLs. Please check that the sample URLs have a changing numeric pattern.", 0
return
# Show generated URLs
log_lines = [f"✅ Generated {len(urls)} URLs:"]
for i, url in enumerate(urls, 1):
log_lines.append(f" {i}. {url}")
log_lines.append("") # Empty line
yield "\n".join(log_lines), 5
# Now process them
download_high_quality = "Video" in download_types
download_audio = "Audio" in download_types
download_captions = "Subtitles" in download_types
total = len(urls)
for idx, url in enumerate(urls, 1):
log_lines.append(f"🔗 Processing URL {idx}/{total}: {url}")
yield "\n".join(log_lines), int(5 + (idx - 1) / total * 95)
def callback(msg):
log_lines.append(msg)
try:
downloader = VideoDownloader(url)
for percent in run_with_yield_callback(
downloader,
languages,
download_high_quality,
download_audio,
download_captions,
callback
):
current_progress = int(5 + (idx - 1) / total * 95 + percent / total * 0.95)
yield "\n".join(log_lines), current_progress
except Exception as e:
log_lines.append(f"❌ Error: {e}")
yield "\n".join(log_lines), int(5 + (idx - 1) / total * 95)
log_lines.append("✅ Done")
log_lines.append("") # Empty line for readability
yield "\n".join(log_lines), int(5 + idx / total * 95)
def process_from_file_stream(file_obj, languages, download_types):
"""Process file with streaming updates"""
if not file_obj:
yield "❌ No file uploaded", 0
return
# Create temp file and read URLs
with tempfile.NamedTemporaryFile(delete=False, mode='w', encoding='utf-8') as temp_file:
with open(file_obj.name, 'r', encoding='utf-8') as f:
content = f.read()
try:
urls = [line.strip() for line in content.splitlines() if line.strip()]
if not urls:
yield "❌ No valid URLs found in file", 0
return
log_lines = [f"📁 Processing file: {file_obj.name}"]
log_lines.append(f"📋 Found {len(urls)} URLs")
log_lines.append("")
yield "\n".join(log_lines), 5
download_high_quality = "Video" in download_types
download_audio = "Audio" in download_types
download_captions = "Subtitles" in download_types
total = len(urls)
for idx, url in enumerate(urls, 1):
log_lines.append(f"🔗 Processing URL {idx}/{total}: {url}")
yield "\n".join(log_lines), int(5 + (idx - 1) / total * 95)
def callback(msg):
log_lines.append(msg)
try:
downloader = VideoDownloader(url)
for percent in run_with_yield_callback(
downloader,
languages,
download_high_quality,
download_audio,
download_captions,
callback
):
current_progress = int(5 + (idx - 1) / total * 95 + percent / total * 0.95)
yield "\n".join(log_lines), current_progress
except Exception as e:
log_lines.append(f"❌ Error: {e}")
yield "\n".join(log_lines), int(5 + (idx - 1) / total * 95)
log_lines.append("✅ Done")
log_lines.append("") # Empty line
yield "\n".join(log_lines), int(5 + idx / total * 95)
except Exception as e:
yield f"❌ Error processing file: {e}", 0
finally:
os.remove(temp_path)
# Create interface - KEEP SAME LAYOUT AS CURRENT
with gr.Blocks() as demo:
gr.Markdown("# Microsoft Learn Downloader Interface\nChoose a method to download videos, audio, and subtitles.")
with gr.Tab("Manual URL List"):
urls_input = gr.Textbox(
label="Enter URLs (one per line)",
lines=5,
placeholder="https://learn.microsoft.com/...\nhttps://learn.microsoft.com/..."
)
langs_input1 = gr.CheckboxGroup(
label="Preferred Subtitle Languages",
choices=choices,
value=["en-us"]
)
download_types1 = gr.CheckboxGroup(
label="What to download?",
choices=["Video", "Audio", "Subtitles"],
value=default_download_types
)
log_output1 = gr.Textbox(label="Logs", lines=2, max_lines=20)
progress1 = gr.Slider(minimum=0, maximum=100, label="Overall Progress")
btn1 = gr.Button("Download")
btn1.click(
fn=process_manual_urls_stream,
inputs=[urls_input, langs_input1, download_types1],
outputs=[log_output1, progress1]
)
with gr.Tab("Generate from Base URL"):
gr.Markdown(
"### Enter two sample URLs that show the pattern\n"
"For example:\n"
" https://learn.microsoft.com/.../module-4\n"
" https://learn.microsoft.com/.../module-5\n"
"The tool will detect the changing number pattern and generate a sequence starting from 1.\n"
"For example, if you enter module-4 and module-5 and request 3 URLs, you'll get:\n"
" .../module-1\n"
" .../module-2\n"
" .../module-3"
)
sample_url1_input = gr.Textbox(
label="First Sample URL",
placeholder="https://learn.microsoft.com/.../module-4"
)
sample_url2_input = gr.Textbox(
label="Second Sample URL",
placeholder="https://learn.microsoft.com/.../module-5"
)
count_input = gr.Number(
label="Number of URLs to Generate (starting from 1)",
precision=0,
value=5
)
langs_input2 = gr.CheckboxGroup(
label="Preferred Subtitle Languages",
choices=choices,
value=["en-us"]
)
download_types2 = gr.CheckboxGroup(
label="What to download?",
choices=["Video", "Audio", "Subtitles"],
value=default_download_types
)
log_output2 = gr.Textbox(label="Logs", lines=2, max_lines=20) # Fixed: more lines
progress2 = gr.Slider(minimum=0, maximum=100, label="Overall Progress")
btn2 = gr.Button("Generate and Download")
btn2.click(
fn=process_generated_urls_stream,
inputs=[sample_url1_input, sample_url2_input, count_input, langs_input2, download_types2],
outputs=[log_output2, progress2]
)
with gr.Tab("Upload File"):
file_input = gr.File(label="Upload .txt file with URLs")
langs_input3 = gr.CheckboxGroup(
label="Preferred Subtitle Languages",
choices=choices,
value=["en-us"]
)
download_types3 = gr.CheckboxGroup(
label="What to download?",
choices=["Video", "Audio", "Subtitles"],
value=default_download_types
)
log_output3 = gr.Textbox(label="Logs", lines=2, max_lines=20) # Fixed: more lines
progress3 = gr.Slider(minimum=0, maximum=100, label="Overall Progress")
btn3 = gr.Button("Download from File")
btn3.click(
fn=process_from_file_stream,
inputs=[file_input, langs_input3, download_types3],
outputs=[log_output3, progress3]
)
if __name__ == "__main__":
demo.launch()