Spaces:
Running
on
Zero
Running
on
Zero
zinojeng
commited on
Commit
Β·
efe5aeb
1
Parent(s):
f3dcd28
Fix PDF upload validation error
Browse files- Add null check for pdf_file parameter
- Validate file has 'name' attribute
- Check if file exists before opening
- Check for empty PDFs (0 pages)
- Better error messages with emoji
- Add traceback for debugging
Fixes: 'NoneType' object has no attribute 'name' error
app.py
CHANGED
|
@@ -113,9 +113,25 @@ def process_pdf(pdf_file, prompt_text):
|
|
| 113 |
str: Extracted text from PDF pages
|
| 114 |
"""
|
| 115 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 116 |
# Open PDF
|
| 117 |
pdf_document = fitz.open(pdf_file.name)
|
| 118 |
total_pages = len(pdf_document)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 119 |
results = []
|
| 120 |
|
| 121 |
# Process first 3 pages (to avoid timeout)
|
|
@@ -151,7 +167,9 @@ def process_pdf(pdf_file, prompt_text):
|
|
| 151 |
return "\n".join(results)
|
| 152 |
|
| 153 |
except Exception as e:
|
| 154 |
-
|
|
|
|
|
|
|
| 155 |
|
| 156 |
# Create Gradio Interface
|
| 157 |
with gr.Blocks(title="DeepSeek-OCR Studio", theme=gr.themes.Soft()) as demo:
|
|
|
|
| 113 |
str: Extracted text from PDF pages
|
| 114 |
"""
|
| 115 |
try:
|
| 116 |
+
# Validate file upload
|
| 117 |
+
if pdf_file is None:
|
| 118 |
+
return "β Please upload a PDF file first."
|
| 119 |
+
|
| 120 |
+
if not hasattr(pdf_file, 'name'):
|
| 121 |
+
return "β Invalid file upload. Please try again."
|
| 122 |
+
|
| 123 |
+
# Check if file exists
|
| 124 |
+
if not os.path.exists(pdf_file.name):
|
| 125 |
+
return f"β File not found: {pdf_file.name}"
|
| 126 |
+
|
| 127 |
# Open PDF
|
| 128 |
pdf_document = fitz.open(pdf_file.name)
|
| 129 |
total_pages = len(pdf_document)
|
| 130 |
+
|
| 131 |
+
if total_pages == 0:
|
| 132 |
+
pdf_document.close()
|
| 133 |
+
return "β PDF file is empty (0 pages)."
|
| 134 |
+
|
| 135 |
results = []
|
| 136 |
|
| 137 |
# Process first 3 pages (to avoid timeout)
|
|
|
|
| 167 |
return "\n".join(results)
|
| 168 |
|
| 169 |
except Exception as e:
|
| 170 |
+
import traceback
|
| 171 |
+
error_details = traceback.format_exc()
|
| 172 |
+
return f"β Error processing PDF: {str(e)}\n\nPlease make sure you uploaded a valid PDF file."
|
| 173 |
|
| 174 |
# Create Gradio Interface
|
| 175 |
with gr.Blocks(title="DeepSeek-OCR Studio", theme=gr.themes.Soft()) as demo:
|