Spaces:
Running
Running
Update app2.py
Browse files
app2.py
CHANGED
@@ -3,6 +3,8 @@ import os
|
|
3 |
import re
|
4 |
import logging
|
5 |
import mimetypes
|
|
|
|
|
6 |
from selenium import webdriver
|
7 |
from chromedriver_py import binary_path
|
8 |
import concurrent.futures
|
@@ -21,6 +23,8 @@ from fake_useragent import UserAgent
|
|
21 |
from ratelimit import limits, sleep_and_retry
|
22 |
from cleantext import clean
|
23 |
import qrcode
|
|
|
|
|
24 |
import nest_asyncio
|
25 |
nest_asyncio.apply()
|
26 |
import aiohttp
|
@@ -225,7 +229,7 @@ class FileProcessor:
|
|
225 |
"source": "file",
|
226 |
"filename": filename,
|
227 |
"content": content,
|
228 |
-
"timestamp": datetime.now().isoformat()
|
229 |
})
|
230 |
except Exception as e:
|
231 |
logger.error(f"Error reading file {filename}: {str(e)}")
|
@@ -347,9 +351,54 @@ def generate_qr_code(data: Union[str, Dict], combined: bool = True) -> List[str]
|
|
347 |
logger.error(f"QR generation error: {e}")
|
348 |
return []
|
349 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
350 |
def create_interface():
|
351 |
"""Create a comprehensive Gradio interface with advanced features"""
|
352 |
-
|
353 |
css = """
|
354 |
.container { max-width: 1200px; margin: auto; }
|
355 |
.warning { background-color: #fff3cd; color: #856404; padding: 10px; border-radius: 4px; }
|
@@ -357,158 +406,34 @@ def create_interface():
|
|
357 |
.success { background-color: #d4edda; color: #155724; padding: 10px; border-radius: 4px; }
|
358 |
"""
|
359 |
|
360 |
-
with gr.Blocks(css=css, title="Advanced Data Processor & QR Generator") as interface:
|
361 |
gr.Markdown("# 🌐 Advanced Data Processing & QR Code Generator")
|
362 |
|
363 |
-
with gr.Tab("
|
364 |
-
|
365 |
-
|
366 |
-
|
367 |
-
|
368 |
-
|
369 |
-
|
370 |
-
|
371 |
-
|
372 |
-
|
373 |
-
|
374 |
-
|
375 |
-
|
376 |
-
|
377 |
-
|
378 |
-
|
379 |
-
|
380 |
-
|
381 |
-
|
382 |
-
|
383 |
-
|
384 |
-
|
385 |
-
|
386 |
-
|
387 |
-
|
388 |
-
|
389 |
-
with gr.Row():
|
390 |
-
combine_data = gr.Checkbox(
|
391 |
-
label="Combine all data into single QR code",
|
392 |
-
value=True,
|
393 |
-
info="Generate one QR code for all data, or separate QR codes for each item"
|
394 |
-
)
|
395 |
-
process_btn = gr.Button("🔄 Process & Generate QR", variant="primary", scale=2)
|
396 |
-
|
397 |
-
output_json = gr.JSON(label="Processed JSON Data")
|
398 |
-
output_gallery = gr.Gallery(label="Generated QR Codes", columns=2, height=400)
|
399 |
-
output_text = gr.Textbox(label="Processing Status", interactive=False)
|
400 |
-
|
401 |
-
def load_example():
|
402 |
-
example_json = {
|
403 |
-
"type": "product_catalog",
|
404 |
-
"items": [
|
405 |
-
{
|
406 |
-
"id": "123",
|
407 |
-
"name": "Test Product",
|
408 |
-
"description": "This is a test product description",
|
409 |
-
"price": 29.99,
|
410 |
-
"category": "electronics",
|
411 |
-
"tags": ["test", "sample", "demo"]
|
412 |
-
},
|
413 |
-
{
|
414 |
-
"id": "456",
|
415 |
-
"name": "Another Product",
|
416 |
-
"description": "Another test product description",
|
417 |
-
"price": 49.99,
|
418 |
-
"category": "accessories",
|
419 |
-
"tags": ["sample", "test"]
|
420 |
-
}
|
421 |
-
],
|
422 |
-
"metadata": {
|
423 |
-
"timestamp": datetime.now().isoformat(),
|
424 |
-
"version": "1.0",
|
425 |
-
"source": "example"
|
426 |
-
}
|
427 |
-
}
|
428 |
-
return json.dumps(example_json, indent=2)
|
429 |
-
|
430 |
-
def clear_input():
|
431 |
-
return ""
|
432 |
-
|
433 |
-
def process_all_inputs(urls, file, text, combine):
|
434 |
-
"""Process all input types and generate QR codes"""
|
435 |
-
try:
|
436 |
-
results = []
|
437 |
-
|
438 |
-
if text and text.strip():
|
439 |
-
try:
|
440 |
-
json_data = json.loads(text)
|
441 |
-
if isinstance(json_data, list):
|
442 |
-
results.extend(json_data)
|
443 |
-
else:
|
444 |
-
results.append(json_data)
|
445 |
-
except json.JSONDecodeError as e:
|
446 |
-
return None, [], f"❌ Invalid JSON format: {str(e)}"
|
447 |
-
|
448 |
-
if urls and urls.strip():
|
449 |
-
processor = URLProcessor()
|
450 |
-
url_list = re.split(r'[,\n]', urls)
|
451 |
-
url_list = [url.strip() for url in url_list if url.strip()]
|
452 |
-
|
453 |
-
for url in url_list:
|
454 |
-
validation = processor.validate_url(url)
|
455 |
-
if validation.get('is_valid'):
|
456 |
-
content = processor.fetch_content(url)
|
457 |
-
if content:
|
458 |
-
results.append({
|
459 |
-
'source': 'url',
|
460 |
-
'url': url,
|
461 |
-
'content': content,
|
462 |
-
'timestamp': datetime.now().isoformat()
|
463 |
-
})
|
464 |
-
|
465 |
-
if file:
|
466 |
-
file_processor = FileProcessor()
|
467 |
-
file_results = file_processor.process_file(file)
|
468 |
-
if file_results:
|
469 |
-
results.extend(file_results)
|
470 |
-
|
471 |
-
if results:
|
472 |
-
qr_paths = generate_qr_code(results, combined=combine)
|
473 |
-
if qr_paths:
|
474 |
-
return (
|
475 |
-
results,
|
476 |
-
[str(path) for path in qr_paths],
|
477 |
-
f"✅ Successfully processed {len(results)} items and generated {len(qr_paths)} QR code(s)!"
|
478 |
-
)
|
479 |
-
else:
|
480 |
-
return None, [], "❌ Failed to generate QR codes. Please check the input data."
|
481 |
-
else:
|
482 |
-
return None, [], "⚠️ No valid content to process. Please provide some input data."
|
483 |
-
|
484 |
-
except Exception as e:
|
485 |
-
logger.error(f"Processing error: {e}")
|
486 |
-
return None, [], f"❌ Error: {str(e)}"
|
487 |
-
|
488 |
-
example_btn.click(load_example, outputs=[text_input])
|
489 |
-
clear_btn.click(clear_input, outputs=[text_input])
|
490 |
-
process_btn.click(
|
491 |
-
process_all_inputs,
|
492 |
-
inputs=[url_input, file_input, text_input, combine_data],
|
493 |
-
outputs=[output_json, output_gallery, output_text]
|
494 |
-
)
|
495 |
-
|
496 |
-
gr.Markdown("""
|
497 |
-
### Features
|
498 |
-
- **URL Processing**: Extract content from websites
|
499 |
-
- **File Processing**: Handle text files and archives
|
500 |
-
- **Notepad**: Direct JSON data input/manipulation
|
501 |
-
- **JSON Cleaning**: Automatic JSON validation and formatting
|
502 |
-
- **QR Generation**: Generate QR codes with embedded JSON data
|
503 |
-
- **Flexible Output**: Choose between combined or separate QR codes
|
504 |
-
|
505 |
-
### Usage Tips
|
506 |
-
1. Use the **Notepad** tab for direct JSON input
|
507 |
-
2. Click "Load Example JSON" to see a sample format
|
508 |
-
3. Choose whether to combine all data into a single QR code
|
509 |
-
4. The generated QR codes will contain the complete JSON data
|
510 |
-
""")
|
511 |
-
|
512 |
return interface
|
513 |
|
514 |
def main():
|
|
|
3 |
import re
|
4 |
import logging
|
5 |
import mimetypes
|
6 |
+
import time
|
7 |
+
import io
|
8 |
from selenium import webdriver
|
9 |
from chromedriver_py import binary_path
|
10 |
import concurrent.futures
|
|
|
23 |
from ratelimit import limits, sleep_and_retry
|
24 |
from cleantext import clean
|
25 |
import qrcode
|
26 |
+
from PIL import Image
|
27 |
+
from pyzbar.pyzbar import decode
|
28 |
import nest_asyncio
|
29 |
nest_asyncio.apply()
|
30 |
import aiohttp
|
|
|
229 |
"source": "file",
|
230 |
"filename": filename,
|
231 |
"content": content,
|
232 |
+
"timestamp": datetime.now ().isoformat()
|
233 |
})
|
234 |
except Exception as e:
|
235 |
logger.error(f"Error reading file {filename}: {str(e)}")
|
|
|
351 |
logger.error(f"QR generation error: {e}")
|
352 |
return []
|
353 |
|
354 |
+
def decode_qr_code(image_path: str) -> str:
|
355 |
+
"""Decode QR code from an image file"""
|
356 |
+
try:
|
357 |
+
img = Image.open(image_path)
|
358 |
+
decoded_objects = decode(img if decoded_objects:
|
359 |
+
return decoded_objects[0].data.decode('utf-8')
|
360 |
+
raise ValueError("Unable to decode QR code")
|
361 |
+
except Exception as e:
|
362 |
+
logger.error(f"QR decoding error: {e}")
|
363 |
+
return None
|
364 |
+
|
365 |
+
def datachat_trained(data_input: str, query: str) -> str:
|
366 |
+
"""Handle trained data interaction logic"""
|
367 |
+
data = clean_json(data_input)
|
368 |
+
if not data:
|
369 |
+
return "Invalid JSON data provided."
|
370 |
+
return f"[Trained Mode]\nData: {json.dumps(data, indent=2)}\nQuery: {query}"
|
371 |
+
|
372 |
+
def datachat_simple(data_input: str, query: str) -> str:
|
373 |
+
"""Handle simple chat interaction logic"""
|
374 |
+
data = clean_json(data_input)
|
375 |
+
if not data:
|
376 |
+
return "Invalid JSON data provided."
|
377 |
+
return f"[Chat Mode]\nData: {json.dumps(data, indent=2)}\nQuestion: {query}"
|
378 |
+
|
379 |
+
def datachat_interface(mode: str, data_source: str, json_input: str, qr_image: str, query: str) -> str:
|
380 |
+
"""Interface for DataChat functionality"""
|
381 |
+
data = None
|
382 |
+
if data_source == "JSON Input":
|
383 |
+
data = json_input
|
384 |
+
elif data_source == "QR Code":
|
385 |
+
try:
|
386 |
+
decoded_data = decode_qr_code(qr_image)
|
387 |
+
data = decoded_data
|
388 |
+
except Exception as e:
|
389 |
+
return f"Invalid QR code data provided: {e}"
|
390 |
+
else:
|
391 |
+
return "No valid data source selected."
|
392 |
+
|
393 |
+
if mode == "Trained with Data":
|
394 |
+
return datachat_trained(data, query)
|
395 |
+
elif mode == "Chat about Data":
|
396 |
+
return datachat_simple(data, query)
|
397 |
+
else:
|
398 |
+
return "Invalid mode selected."
|
399 |
+
|
400 |
def create_interface():
|
401 |
"""Create a comprehensive Gradio interface with advanced features"""
|
|
|
402 |
css = """
|
403 |
.container { max-width: 1200px; margin: auto; }
|
404 |
.warning { background-color: #fff3cd; color: #856404; padding: 10px; border-radius: 4px; }
|
|
|
406 |
.success { background-color: #d4edda; color: #155724; padding: 10px; border-radius: 4px; }
|
407 |
"""
|
408 |
|
409 |
+
with gr.Blocks(css=css, title="Advanced Data Processor & QR Code Generator") as interface:
|
410 |
gr.Markdown("# 🌐 Advanced Data Processing & QR Code Generator")
|
411 |
|
412 |
+
with gr.Tab("DataChat"):
|
413 |
+
mode = gr.Radio(["Trained with Data", "Chat about Data"], label="Mode")
|
414 |
+
data_source = gr.Radio(["JSON Input", "QR Code"], label="Data Source")
|
415 |
+
json_input = gr.Textbox(lines=8, label="JSON Data")
|
416 |
+
qr_image = gr.Image(label="QR Code Image", type="filepath")
|
417 |
+
query = gr.Textbox(label="Query")
|
418 |
+
|
419 |
+
submit_btn = gr.Button("Submit")
|
420 |
+
output = gr.Textbox(label="Response")
|
421 |
+
|
422 |
+
submit_btn.click(datachat_interface, [mode, data_source, json_input, qr_image, query], output)
|
423 |
+
|
424 |
+
with gr.Tab("QR Generator"):
|
425 |
+
qr_input = gr.Textbox(lines=8, label="Input JSON for QR")
|
426 |
+
generate_btn = gr.Button("Generate QR")
|
427 |
+
qr_output = gr.Image(label="Generated QR Code")
|
428 |
+
|
429 |
+
def generate_qr(json_data):
|
430 |
+
data = clean_json(json_data)
|
431 |
+
if data:
|
432 |
+
return generate_qr_code(data)
|
433 |
+
return None
|
434 |
+
|
435 |
+
generate_btn.click(generate_qr, qr_input, qr_output)
|
436 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
437 |
return interface
|
438 |
|
439 |
def main():
|