Spaces:
Running
Running
Update app2.py
Browse files
app2.py
CHANGED
@@ -4,33 +4,20 @@ import re
|
|
4 |
import logging
|
5 |
import mimetypes
|
6 |
import time
|
7 |
-
import zbar
|
8 |
from PIL import Image
|
9 |
from pyzbar.pyzbar import decode
|
10 |
import io
|
11 |
-
from selenium.webdriver.common.by import By
|
12 |
-
import concurrent.futures
|
13 |
-
import string
|
14 |
import zipfile
|
15 |
import tempfile
|
16 |
from datetime import datetime
|
17 |
from typing import List, Dict, Optional, Union
|
18 |
from pathlib import Path
|
19 |
-
from urllib.parse import urlparse
|
20 |
import requests
|
21 |
import validators
|
22 |
import gradio as gr
|
23 |
from bs4 import BeautifulSoup
|
24 |
from fake_useragent import UserAgent
|
25 |
-
from ratelimit import limits, sleep_and_retry
|
26 |
from cleantext import clean
|
27 |
-
import qrcode
|
28 |
-
from PIL import Image
|
29 |
-
import nest_asyncio
|
30 |
-
nest_asyncio.apply()
|
31 |
-
import aiohttp
|
32 |
-
|
33 |
-
|
34 |
|
35 |
# Setup logging
|
36 |
logging.basicConfig(
|
@@ -271,50 +258,68 @@ class FileProcessor:
|
|
271 |
logger.error(f"File processing error: {e}")
|
272 |
return []
|
273 |
|
274 |
-
def clean_json(data: Union[str, Dict]) -> Optional[Dict]:
|
275 |
-
|
276 |
-
|
277 |
-
|
278 |
-
|
279 |
-
|
280 |
-
|
281 |
-
|
282 |
-
|
283 |
-
|
284 |
-
|
285 |
-
|
286 |
-
|
287 |
-
|
288 |
-
|
289 |
-
|
290 |
-
def generate_qr_code(data: Union[str, Dict], combined: bool = True) -> List[str]:
|
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 |
if cleaned_item:
|
319 |
qr = qrcode.QRCode(
|
320 |
version=None,
|
@@ -327,44 +332,26 @@ def generate_qr_code(data: Union[str, Dict], combined: bool = True) -> List[str]
|
|
327 |
qr.make(fit=True)
|
328 |
|
329 |
img = qr.make_image(fill_color="black", back_color="white")
|
330 |
-
output_path = output_dir / f'
|
331 |
img.save(str(output_path))
|
332 |
-
|
333 |
-
|
334 |
-
|
335 |
-
|
336 |
-
|
337 |
-
qr = qrcode.QRCode(
|
338 |
-
version=None,
|
339 |
-
error_correction=qrcode.constants.ERROR_CORRECT_L,
|
340 |
-
box_size=10,
|
341 |
-
border=4,
|
342 |
-
)
|
343 |
-
json_str = json.dumps(cleaned_item, ensure_ascii=False)
|
344 |
-
qr.add_data(json_str)
|
345 |
-
qr.make(fit=True)
|
346 |
-
|
347 |
-
img = qr.make_image(fill_color="black", back_color="white")
|
348 |
-
output_path = output_dir / f'single_qr_{int(time.time())}.png'
|
349 |
-
img.save(str(output_path))
|
350 |
-
return [str(output_path)]
|
351 |
-
|
352 |
return []
|
353 |
-
|
354 |
-
|
355 |
-
|
356 |
-
|
357 |
-
|
358 |
-
|
359 |
-
|
360 |
-
|
361 |
-
|
362 |
-
|
363 |
-
|
364 |
-
|
365 |
-
except Exception as e:
|
366 |
-
logger.error(f"QR decoding error: {e}")
|
367 |
-
return None, None # Return None for both data and resolution in case of error
|
368 |
|
369 |
def datachat_trained(data_input: str, query: str) -> str:
|
370 |
"""Handle trained data interaction logic"""
|
|
|
4 |
import logging
|
5 |
import mimetypes
|
6 |
import time
|
|
|
7 |
from PIL import Image
|
8 |
from pyzbar.pyzbar import decode
|
9 |
import io
|
|
|
|
|
|
|
10 |
import zipfile
|
11 |
import tempfile
|
12 |
from datetime import datetime
|
13 |
from typing import List, Dict, Optional, Union
|
14 |
from pathlib import Path
|
|
|
15 |
import requests
|
16 |
import validators
|
17 |
import gradio as gr
|
18 |
from bs4 import BeautifulSoup
|
19 |
from fake_useragent import UserAgent
|
|
|
20 |
from cleantext import clean
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
|
22 |
# Setup logging
|
23 |
logging.basicConfig(
|
|
|
258 |
logger.error(f"File processing error: {e}")
|
259 |
return []
|
260 |
|
261 |
+
def clean_json(data: Union[str, Dict]) -> Optional[Dict]:
|
262 |
+
"""Clean and validate JSON data"""
|
263 |
+
try:
|
264 |
+
if isinstance(data, str):
|
265 |
+
data = data.strip()
|
266 |
+
data = json.loads(data)
|
267 |
+
|
268 |
+
cleaned = json.loads(json.dumps(data))
|
269 |
+
return cleaned
|
270 |
+
except json.JSONDecodeError as e:
|
271 |
+
logger.error(f"JSON cleaning error: {e}")
|
272 |
+
return None
|
273 |
+
except Exception as e:
|
274 |
+
logger.error(f"Unexpected error while cleaning JSON: {e}")
|
275 |
+
return None
|
276 |
+
|
277 |
+
def generate_qr_code(data: Union[str, Dict], combined: bool = True) -> List[str]:
|
278 |
+
"""Generate QR code(s) from data"""
|
279 |
+
try:
|
280 |
+
output_dir = Path('output/qr_codes')
|
281 |
+
output_dir.mkdir(parents=True, exist_ok=True)
|
282 |
+
|
283 |
+
if combined:
|
284 |
+
cleaned_data = clean_json(data)
|
285 |
+
if cleaned_data:
|
286 |
+
qr = qrcode.QRCode(
|
287 |
+
version=None,
|
288 |
+
error_correction=qrcode.constants.ERROR_CORRECT_L,
|
289 |
+
box_size=10,
|
290 |
+
border=4,
|
291 |
+
)
|
292 |
+
json_str = json.dumps(cleaned_data, ensure_ascii=False)
|
293 |
+
qr.add_data(json_str)
|
294 |
+
qr.make(fit=True)
|
295 |
+
|
296 |
+
img = qr.make_image(fill_color="black", back_color="white")
|
297 |
+
output_path = output_dir / f'combined_qr_{int(time.time())}.png'
|
298 |
+
img.save(str(output_path))
|
299 |
+
return [str(output_path)]
|
300 |
+
else:
|
301 |
+
if isinstance(data, list):
|
302 |
+
paths = []
|
303 |
+
for idx, item in enumerate(data):
|
304 |
+
cleaned_item = clean_json(item)
|
305 |
+
if cleaned_item:
|
306 |
+
qr = qrcode.QRCode(
|
307 |
+
version=None,
|
308 |
+
error_correction=qrcode.constants.ERROR_CORRECT_L,
|
309 |
+
box_size=10,
|
310 |
+
border=4,
|
311 |
+
)
|
312 |
+
json_str = json.dumps(cleaned_item, ensure_ascii=False)
|
313 |
+
qr.add_data(json_str)
|
314 |
+
qr.make(fit=True)
|
315 |
+
|
316 |
+
img = qr.make_image(fill_color="black", back_color="white")
|
317 |
+
output_path = output_dir / f'item_{idx}_qr_{int(time.time())}.png'
|
318 |
+
img.save(str(output_path))
|
319 |
+
paths.append(str(output_path))
|
320 |
+
return paths
|
321 |
+
else:
|
322 |
+
cleaned_item = clean_json(data)
|
323 |
if cleaned_item:
|
324 |
qr = qrcode.QRCode(
|
325 |
version=None,
|
|
|
332 |
qr.make(fit=True)
|
333 |
|
334 |
img = qr.make_image(fill_color="black", back_color="white")
|
335 |
+
output_path = output_dir / f'single_qr_{int(time.time())}.png'
|
336 |
img.save(str(output_path))
|
337 |
+
return [str(output_path)]
|
338 |
+
|
339 |
+
return []
|
340 |
+
except Exception as e:
|
341 |
+
logger.error(f"QR generation error: {e}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
342 |
return []
|
343 |
+
|
344 |
+
def decode_qr(image):
|
345 |
+
decoded_objects = decode(image)
|
346 |
+
results = []
|
347 |
+
for obj in decoded_objects:
|
348 |
+
results.append(obj.data.decode('utf-8'))
|
349 |
+
return results
|
350 |
+
|
351 |
+
raise ValueError("Unable to decode QR code")
|
352 |
+
except Exception as e:
|
353 |
+
logger.error(f"QR decoding error: {e}")
|
354 |
+
return None, None # Return None for both data and resolution in case of error
|
|
|
|
|
|
|
355 |
|
356 |
def datachat_trained(data_input: str, query: str) -> str:
|
357 |
"""Handle trained data interaction logic"""
|