Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -85,17 +85,27 @@ def process_uploaded_file(file):
|
|
| 85 |
|
| 86 |
if file is None:
|
| 87 |
return "No file uploaded. Please upload a file."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
doc_state.doc_type = 'pdf'
|
| 93 |
try:
|
| 94 |
doc_state.current_doc_images, doc_state.current_doc_text = process_pdf_file(file_path)
|
| 95 |
return f"PDF processed successfully. Total pages: {len(doc_state.current_doc_images)}. You can now ask questions about the content."
|
| 96 |
except Exception as e:
|
| 97 |
return f"Error processing PDF: {str(e)}. Please try a different PDF file."
|
| 98 |
-
|
| 99 |
doc_state.doc_type = 'image'
|
| 100 |
try:
|
| 101 |
img = Image.open(file_path).convert("RGB")
|
|
@@ -108,6 +118,8 @@ def process_uploaded_file(file):
|
|
| 108 |
return "Image loaded successfully. You can now ask questions about the content."
|
| 109 |
except Exception as e:
|
| 110 |
return f"Error processing image: {str(e)}. Please try a different image file."
|
|
|
|
|
|
|
| 111 |
except Exception as e:
|
| 112 |
logger.error(f"Error in process_file: {str(e)}")
|
| 113 |
return "An error occurred while processing the file. Please try again."
|
|
@@ -175,12 +187,12 @@ def clear_context():
|
|
| 175 |
# Create the Gradio interface
|
| 176 |
with gr.Blocks() as demo:
|
| 177 |
gr.Markdown("# Document Analyzer with Chat Support")
|
| 178 |
-
gr.Markdown("Upload a PDF or image
|
| 179 |
|
| 180 |
with gr.Row():
|
| 181 |
file_upload = gr.File(
|
| 182 |
-
label="Upload Document
|
| 183 |
-
file_types=["pdf", "
|
| 184 |
)
|
| 185 |
upload_status = gr.Textbox(
|
| 186 |
label="Upload Status",
|
|
|
|
| 85 |
|
| 86 |
if file is None:
|
| 87 |
return "No file uploaded. Please upload a file."
|
| 88 |
+
|
| 89 |
+
# Get the file path and extension
|
| 90 |
+
if isinstance(file, dict):
|
| 91 |
+
file_path = file["name"]
|
| 92 |
+
else:
|
| 93 |
+
file_path = file.name
|
| 94 |
|
| 95 |
+
# Get file extension
|
| 96 |
+
file_ext = file_path.lower().split('.')[-1]
|
| 97 |
+
|
| 98 |
+
# Define allowed extensions
|
| 99 |
+
image_extensions = {'png', 'jpg', 'jpeg', 'gif', 'bmp', 'webp'}
|
| 100 |
+
|
| 101 |
+
if file_ext == 'pdf':
|
| 102 |
doc_state.doc_type = 'pdf'
|
| 103 |
try:
|
| 104 |
doc_state.current_doc_images, doc_state.current_doc_text = process_pdf_file(file_path)
|
| 105 |
return f"PDF processed successfully. Total pages: {len(doc_state.current_doc_images)}. You can now ask questions about the content."
|
| 106 |
except Exception as e:
|
| 107 |
return f"Error processing PDF: {str(e)}. Please try a different PDF file."
|
| 108 |
+
elif file_ext in image_extensions:
|
| 109 |
doc_state.doc_type = 'image'
|
| 110 |
try:
|
| 111 |
img = Image.open(file_path).convert("RGB")
|
|
|
|
| 118 |
return "Image loaded successfully. You can now ask questions about the content."
|
| 119 |
except Exception as e:
|
| 120 |
return f"Error processing image: {str(e)}. Please try a different image file."
|
| 121 |
+
else:
|
| 122 |
+
return f"Unsupported file type: {file_ext}. Please upload a PDF or image file (PNG, JPG, JPEG, GIF, BMP, WEBP)."
|
| 123 |
except Exception as e:
|
| 124 |
logger.error(f"Error in process_file: {str(e)}")
|
| 125 |
return "An error occurred while processing the file. Please try again."
|
|
|
|
| 187 |
# Create the Gradio interface
|
| 188 |
with gr.Blocks() as demo:
|
| 189 |
gr.Markdown("# Document Analyzer with Chat Support")
|
| 190 |
+
gr.Markdown("Upload a PDF or image (PNG, JPG, JPEG, GIF, BMP, WEBP) and chat about its contents.")
|
| 191 |
|
| 192 |
with gr.Row():
|
| 193 |
file_upload = gr.File(
|
| 194 |
+
label="Upload Document",
|
| 195 |
+
file_types=[".pdf", ".png", ".jpg", ".jpeg", ".gif", ".bmp", ".webp"]
|
| 196 |
)
|
| 197 |
upload_status = gr.Textbox(
|
| 198 |
label="Upload Status",
|