Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -249,101 +249,104 @@ class FileProcessor:
|
|
249 |
logger.error(f"File processing error: {e}")
|
250 |
return []
|
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 |
-
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 process_all_inputs(urls, file, text, notes):
|
288 |
-
"""Process all input types with progress tracking"""
|
289 |
-
try:
|
290 |
-
processor = URLProcessor()
|
291 |
-
file_processor = FileProcessor()
|
292 |
-
results = []
|
293 |
-
|
294 |
-
# Process URLs
|
295 |
-
if urls:
|
296 |
-
url_list = re.split(r'[,\n]', urls)
|
297 |
-
url_list = [url.strip() for url in url_list if url.strip()]
|
298 |
-
|
299 |
-
for url in url_list:
|
300 |
-
validation = processor.validate_url(url)
|
301 |
-
if validation.get('is_valid'):
|
302 |
-
content = processor.fetch_content(url)
|
303 |
-
if content:
|
304 |
-
results.append({
|
305 |
-
'source': 'url',
|
306 |
-
'url': url,
|
307 |
-
'content': content,
|
308 |
-
'timestamp': datetime.now().isoformat()
|
309 |
-
})
|
310 |
-
|
311 |
-
# Process files
|
312 |
-
if file:
|
313 |
-
results.extend(file_processor.process_file(file))
|
314 |
-
|
315 |
-
# Process text input
|
316 |
-
if text:
|
317 |
-
cleaned_text = processor.advanced_text_cleaning(text)
|
318 |
-
results.append({
|
319 |
-
'source': 'direct_input',
|
320 |
-
'content': cleaned_text,
|
321 |
-
'timestamp': datetime.now().isoformat()
|
322 |
-
})
|
323 |
-
|
324 |
-
# Generate output
|
325 |
-
if results:
|
326 |
-
output_dir = Path('output') / datetime.now().strftime('%Y-%m-%d')
|
327 |
-
output_dir.mkdir(parents=True, exist_ok=True)
|
328 |
-
output_path = output_dir / f'processed_{int(time.time())}.json'
|
329 |
-
|
330 |
-
with open(output_path, 'w', encoding='utf-8') as f:
|
331 |
-
json.dump(results, f, ensure_ascii=False, indent=2)
|
332 |
-
|
333 |
-
summary = f"Processed {len(results)} items successfully!"
|
334 |
-
json_data = json.dumps(results, indent=2) # Prepare JSON for QR code
|
335 |
-
return str(output_path), summary, json_data # Return JSON for editor
|
336 |
-
else:
|
337 |
-
return None, "No valid content to process.", ""
|
338 |
|
339 |
-
|
340 |
-
|
341 |
-
|
342 |
|
343 |
-
|
344 |
-
|
345 |
-
|
346 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
347 |
|
348 |
def create_interface():
|
349 |
"""Create a comprehensive Gradio interface with advanced features"""
|
|
|
249 |
logger.error(f"File processing error: {e}")
|
250 |
return []
|
251 |
|
252 |
+
# Move process_all_inputs outside of the FileProcessor class
|
253 |
+
def process_all_inputs(urls, file, text, notes):
|
254 |
+
"""Process all input types with progress tracking"""
|
255 |
+
try:
|
256 |
+
processor = URLProcessor()
|
257 |
+
file_processor = FileProcessor()
|
258 |
+
results = []
|
259 |
+
|
260 |
+
# Process URLs
|
261 |
+
if urls:
|
262 |
+
url_list = re.split(r'[,\n]', urls)
|
263 |
+
url_list = [url.strip() for url in url_list if url.strip()]
|
264 |
+
|
265 |
+
for url in url_list:
|
266 |
+
validation = processor.validate_url(url)
|
267 |
+
if validation.get('is_valid'):
|
268 |
+
content = processor.fetch_content(url)
|
269 |
+
if content:
|
270 |
+
results.append({
|
271 |
+
'source': 'url',
|
272 |
+
'url': url,
|
273 |
+
'content': content,
|
274 |
+
'timestamp': datetime.now().isoformat()
|
275 |
+
})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
276 |
|
277 |
+
# Process files
|
278 |
+
if file:
|
279 |
+
results.extend(file_processor.process_file(file))
|
280 |
|
281 |
+
# Process text input
|
282 |
+
if text:
|
283 |
+
cleaned_text = processor.advanced_text_cleaning(text)
|
284 |
+
results.append({
|
285 |
+
'source': 'direct_input',
|
286 |
+
'content': cleaned_text,
|
287 |
+
'timestamp': datetime.now().isoformat()
|
288 |
+
})
|
289 |
+
|
290 |
+
# Generate output
|
291 |
+
if results:
|
292 |
+
output_dir = Path('output') / datetime.now().strftime('%Y-%m-%d')
|
293 |
+
output_dir.mkdir(parents=True, exist_ok=True)
|
294 |
+
output_path = output_dir / f'processed_{int(time.time())}.json'
|
295 |
+
|
296 |
+
with open(output_path, 'w', encoding='utf-8') as f:
|
297 |
+
json.dump(results, f, ensure_ascii=False, indent=2)
|
298 |
+
|
299 |
+
summary = f"Processed {len(results)} items successfully!"
|
300 |
+
json_data = json.dumps(results, indent=2) # Prepare JSON for QR code
|
301 |
+
return str(output_path), summary, json_data # Return JSON for editor
|
302 |
+
else:
|
303 |
+
return None, "No valid content to process.", ""
|
304 |
+
|
305 |
+
except Exception as e:
|
306 |
+
logger.error(f"Processing error: {e}")
|
307 |
+
return None, f"Error: {str(e)}", ""
|
308 |
+
|
309 |
+
# Also move generate_qr_code outside of the FileProcessor class
|
310 |
+
def generate_qr_code(json_data):
|
311 |
+
"""Generate QR code from JSON data and return the file path."""
|
312 |
+
if json_data:
|
313 |
+
return generate_qr(json_data)
|
314 |
+
|
315 |
+
# Move generate_qr outside of the FileProcessor class as well
|
316 |
+
def generate_qr(json_data):
|
317 |
+
"""Generate QR code from JSON data and return the file path."""
|
318 |
+
try:
|
319 |
+
# Try first with automatic version selection
|
320 |
+
qr = qrcode.QRCode(
|
321 |
+
error_correction=qrcode.constants.ERROR_CORRECT_L,
|
322 |
+
box_size=10,
|
323 |
+
border=4,
|
324 |
+
)
|
325 |
+
qr.add_data(json_data)
|
326 |
+
qr.make(fit=True)
|
327 |
+
|
328 |
+
img = qr.make_image(fill_color="black", back_color="white")
|
329 |
+
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".png")
|
330 |
+
img.save(temp_file.name)
|
331 |
+
return temp_file.name
|
332 |
+
except Exception as e:
|
333 |
+
# If the data is too large for a QR code
|
334 |
+
logger.error(f"QR generation error: {e}")
|
335 |
+
|
336 |
+
# Create a simple QR with error message
|
337 |
+
qr = qrcode.QRCode(
|
338 |
+
version=1,
|
339 |
+
error_correction=qrcode.constants.ERROR_CORRECT_L,
|
340 |
+
box_size=10,
|
341 |
+
border=4,
|
342 |
+
)
|
343 |
+
qr.add_data("Error: Data too large for QR code")
|
344 |
+
qr.make(fit=True)
|
345 |
+
|
346 |
+
img = qr.make_image(fill_color="black", back_color="white")
|
347 |
+
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".png")
|
348 |
+
img.save(temp_file.name)
|
349 |
+
return temp_file.name
|
350 |
|
351 |
def create_interface():
|
352 |
"""Create a comprehensive Gradio interface with advanced features"""
|