Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -249,179 +249,40 @@ class FileProcessor:
|
|
249 |
logger.error(f"File processing error: {e}")
|
250 |
return []
|
251 |
|
252 |
-
def generate_qr(json_data):
|
253 |
-
|
254 |
-
try:
|
255 |
-
# Try first with automatic version selection
|
256 |
-
qr = qrcode.QRCode(
|
257 |
-
error_correction=qrcode.constants.ERROR_CORRECT_L,
|
258 |
-
box_size=10,
|
259 |
-
border=4,
|
260 |
-
)
|
261 |
-
qr.add_data(json_data)
|
262 |
-
qr.make(fit=True)
|
263 |
-
|
264 |
-
img = qr.make_image(fill_color="black", back_color="white")
|
265 |
-
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".png")
|
266 |
-
img.save(temp_file.name)
|
267 |
-
return temp_file.name
|
268 |
-
except Exception as e:
|
269 |
-
# If the data is too large for a QR code
|
270 |
-
logger.error(f"QR generation error: {e}")
|
271 |
-
|
272 |
-
# Create a simple QR with error message
|
273 |
-
qr = qrcode.QRCode(
|
274 |
-
version=1,
|
275 |
-
error_correction=qrcode.constants.ERROR_CORRECT_L,
|
276 |
-
box_size=10,
|
277 |
-
border=4,
|
278 |
-
)
|
279 |
-
qr.add_data("Error: Data too large for QR code")
|
280 |
-
qr.make(fit=True)
|
281 |
-
|
282 |
-
img = qr.make_image(fill_color="black", back_color="white")
|
283 |
-
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".png")
|
284 |
-
img.save(temp_file.name)
|
285 |
-
return temp_file.name
|
286 |
-
|
287 |
-
def create_interface():
|
288 |
-
"""Create a comprehensive Gradio interface with advanced features"""
|
289 |
-
|
290 |
-
css = """
|
291 |
-
.container { max-width: 1200px; margin: auto; }
|
292 |
-
.warning { background-color: #fff3cd; color: #856404; }
|
293 |
-
.error { background-color: #f8d7da; color: #721c24; }
|
294 |
-
"""
|
295 |
-
|
296 |
-
with gr.Blocks(css=css, title="Advanced Text & URL Processor") as interface:
|
297 |
-
gr.Markdown("# 🌐 Advanced URL & Text Processing Toolkit")
|
298 |
-
|
299 |
-
with gr.Tab("URL Processing"):
|
300 |
-
url_input = gr.Textbox(
|
301 |
-
label="Enter URLs (comma or newline separated)",
|
302 |
-
lines=5,
|
303 |
-
placeholder="https://example1.com\nhttps://example2.com"
|
304 |
-
)
|
305 |
-
|
306 |
-
with gr.Tab("File Input"):
|
307 |
-
file_input = gr.File(
|
308 |
-
label="Upload text file or ZIP archive",
|
309 |
-
file_types=[".txt", ".zip", ".md", ".csv", ".json", ".xml"]
|
310 |
-
)
|
311 |
-
|
312 |
-
with gr.Tab("Text Input"):
|
313 |
-
text_input = gr.Textbox(
|
314 |
-
label="Raw Text Input",
|
315 |
-
lines=5,
|
316 |
-
placeholder="Paste your text here..."
|
317 |
-
)
|
318 |
-
|
319 |
-
with gr.Tab("JSON Editor"):
|
320 |
-
json_editor = gr.Textbox(
|
321 |
-
label="JSON Editor",
|
322 |
-
lines=20,
|
323 |
-
placeholder="View and edit your JSON data here...",
|
324 |
-
interactive=True,
|
325 |
-
elem_id="json-editor" # Optional: for custom styling
|
326 |
-
)
|
327 |
-
|
328 |
-
with gr.Tab("Scratchpad"):
|
329 |
-
scratchpad = gr.Textbox(
|
330 |
-
label="Scratchpad",
|
331 |
-
lines=10,
|
332 |
-
placeholder="Quick notes or text collections...",
|
333 |
-
interactive=True
|
334 |
-
)
|
335 |
-
|
336 |
-
process_btn = gr.Button("Process Input", variant="primary")
|
337 |
-
qr_btn = gr.Button("Generate QR Code", variant="secondary")
|
338 |
-
|
339 |
-
output_text = gr.Textbox(label="Processing Results", interactive=False)
|
340 |
-
output_file = gr.File(label="Processed Output")
|
341 |
-
qr_output = gr.Image(label="QR Code", type="filepath") # To display the generated QR code
|
342 |
-
|
343 |
-
def process_all_inputs(urls, file, text, notes):
|
344 |
-
"""Process all input types with progress tracking"""
|
345 |
try:
|
346 |
-
|
347 |
-
|
348 |
-
|
349 |
-
|
350 |
-
|
351 |
-
|
352 |
-
|
353 |
-
|
354 |
-
|
355 |
-
|
356 |
-
|
357 |
-
|
358 |
-
|
359 |
-
'source': 'url',
|
360 |
-
'url': url,
|
361 |
-
'content': content,
|
362 |
-
'timestamp': datetime.now().isoformat()
|
363 |
-
})
|
364 |
-
|
365 |
-
# Process files
|
366 |
-
if file:
|
367 |
-
results.extend(file_processor.process_file(file))
|
368 |
-
|
369 |
-
# Process text input
|
370 |
-
if text:
|
371 |
-
cleaned_text = processor.advanced_text_cleaning(text)
|
372 |
-
results.append({
|
373 |
-
'source': 'direct_input',
|
374 |
-
'content': cleaned_text,
|
375 |
-
'timestamp': datetime.now().isoformat()
|
376 |
-
})
|
377 |
-
|
378 |
-
# Generate output
|
379 |
-
if results:
|
380 |
-
output_dir = Path('output') / datetime.now().strftime('%Y-%m-%d')
|
381 |
-
output_dir.mkdir(parents=True, exist_ok=True)
|
382 |
-
output_path = output_dir / f'processed_{int(time.time())}.json'
|
383 |
-
|
384 |
-
with open(output_path, 'w', encoding='utf-8') as f:
|
385 |
-
json.dump(results, f, ensure_ascii=False, indent=2)
|
386 |
-
|
387 |
-
summary = f"Processed {len(results)} items successfully!"
|
388 |
-
json_data = json.dumps(results, indent=2) # Prepare JSON for QR code
|
389 |
-
return str(output_path), summary, json_data # Return JSON for editor
|
390 |
-
else:
|
391 |
-
return None, "No valid content to process.", ""
|
392 |
-
|
393 |
except Exception as e:
|
394 |
-
|
395 |
-
|
396 |
-
|
397 |
-
|
398 |
-
|
399 |
-
|
400 |
-
|
401 |
-
|
402 |
-
|
403 |
-
|
404 |
-
|
405 |
-
|
406 |
-
|
407 |
-
|
408 |
-
|
409 |
-
|
410 |
-
|
411 |
-
inputs=json_editor,
|
412 |
-
outputs=qr_output
|
413 |
-
)
|
414 |
-
|
415 |
-
gr.Markdown("""
|
416 |
-
### Usage Guidelines
|
417 |
-
- **URL Processing**: Enter valid HTTP/HTTPS URLs
|
418 |
-
- **File Input**: Upload text files or ZIP archives
|
419 |
-
- **Text Input**: Direct text processing
|
420 |
-
- **JSON Editor**: View and edit your JSON data
|
421 |
-
- **Scratchpad**: Quick notes or text collections
|
422 |
-
- Advanced cleaning and validation included
|
423 |
-
""")
|
424 |
-
return interface
|
425 |
|
426 |
def create_interface():
|
427 |
"""Create a comprehensive Gradio interface with advanced features"""
|
@@ -478,68 +339,6 @@ def create_interface():
|
|
478 |
output_file = gr.File(label="Processed Output")
|
479 |
qr_output = gr.Image(label="QR Code", type="filepath") # To display the generated QR code
|
480 |
|
481 |
-
def process_all_inputs(urls, file, text, notes):
|
482 |
-
"""Process all input types with progress tracking"""
|
483 |
-
try:
|
484 |
-
processor = URLProcessor()
|
485 |
-
file_processor = FileProcessor()
|
486 |
-
results = []
|
487 |
-
|
488 |
-
# Process URLs
|
489 |
-
if urls:
|
490 |
-
url_list = re.split(r'[,\n]', urls)
|
491 |
-
url_list = [url.strip() for url in url_list if url.strip()]
|
492 |
-
|
493 |
-
for url in url_list:
|
494 |
-
validation = processor.validate_url(url)
|
495 |
-
if validation.get('is_valid'):
|
496 |
-
content = processor.fetch_content(url)
|
497 |
-
if content:
|
498 |
-
results.append({
|
499 |
-
'source': 'url',
|
500 |
-
'url': url,
|
501 |
-
'content': content,
|
502 |
-
'timestamp': datetime.now().isoformat()
|
503 |
-
})
|
504 |
-
|
505 |
-
# Process files
|
506 |
-
if file:
|
507 |
-
results.extend(file_processor.process_file(file))
|
508 |
-
|
509 |
-
# Process text input
|
510 |
-
if text:
|
511 |
-
cleaned_text = processor.advanced_text_cleaning(text)
|
512 |
-
results.append({
|
513 |
-
'source': 'direct_input',
|
514 |
-
'content': cleaned_text,
|
515 |
-
'timestamp': datetime.now().isoformat()
|
516 |
-
})
|
517 |
-
|
518 |
-
# Generate output
|
519 |
-
if results:
|
520 |
-
output_dir = Path('output') / datetime.now().strftime('%Y-%m-%d')
|
521 |
-
output_dir.mkdir(parents=True, exist_ok=True)
|
522 |
-
output_path = output_dir / f'processed_{int(time.time())}.json'
|
523 |
-
|
524 |
-
with open(output_path, 'w', encoding='utf-8') as f:
|
525 |
-
json.dump(results, f, ensure_ascii=False, indent=2)
|
526 |
-
|
527 |
-
summary = f"Processed {len(results)} items successfully!"
|
528 |
-
json_data = json.dumps(results, indent=2) # Prepare JSON for QR code
|
529 |
-
return str(output_path), summary, json_data # Return JSON for editor
|
530 |
-
else:
|
531 |
-
return None, "No valid content to process.", ""
|
532 |
-
|
533 |
-
except Exception as e:
|
534 |
-
logger.error(f"Processing error: {e}")
|
535 |
-
return None, f"Error: {str(e)}", ""
|
536 |
-
|
537 |
-
def generate_qr_code(json_data):
|
538 |
-
"""Generate QR code from JSON data and return the file path."""
|
539 |
-
if json_data:
|
540 |
-
return generate_qr(json_data)
|
541 |
-
return None
|
542 |
-
|
543 |
process_btn.click(
|
544 |
process_all_inputs,
|
545 |
inputs=[url_input, file_input, text_input, scratchpad],
|
|
|
249 |
logger.error(f"File processing error: {e}")
|
250 |
return []
|
251 |
|
252 |
+
def generate_qr(json_data):
|
253 |
+
"""Generate QR code from JSON data and return the file path."""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
254 |
try:
|
255 |
+
# Try first with automatic version selection
|
256 |
+
qr = qrcode.QRCode(
|
257 |
+
error_correction=qrcode.constants.ERROR_CORRECT_L,
|
258 |
+
box_size=10,
|
259 |
+
border=4,
|
260 |
+
)
|
261 |
+
qr.add_data(json_data)
|
262 |
+
qr.make(fit=True)
|
263 |
+
|
264 |
+
img = qr.make_image(fill_color="black", back_color="white")
|
265 |
+
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".png")
|
266 |
+
img.save(temp_file.name)
|
267 |
+
return temp_file.name
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
268 |
except Exception as e:
|
269 |
+
# If the data is too large for a QR code
|
270 |
+
logger.error(f"QR generation error: {e}")
|
271 |
+
|
272 |
+
# Create a simple QR with error message
|
273 |
+
qr = qrcode.QRCode(
|
274 |
+
version=1,
|
275 |
+
error_correction=qrcode.constants.ERROR_CORRECT_L,
|
276 |
+
box_size=10,
|
277 |
+
border=4,
|
278 |
+
)
|
279 |
+
qr.add_data("Error: Data too large for QR code")
|
280 |
+
qr.make(fit=True)
|
281 |
+
|
282 |
+
img = qr.make_image(fill_color="black", back_color="white")
|
283 |
+
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".png")
|
284 |
+
img.save(temp_file.name)
|
285 |
+
return temp_file.name
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
286 |
|
287 |
def create_interface():
|
288 |
"""Create a comprehensive Gradio interface with advanced features"""
|
|
|
339 |
output_file = gr.File(label="Processed Output")
|
340 |
qr_output = gr.Image(label="QR Code", type="filepath") # To display the generated QR code
|
341 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
342 |
process_btn.click(
|
343 |
process_all_inputs,
|
344 |
inputs=[url_input, file_input, text_input, scratchpad],
|