CosmickVisions commited on
Commit
cdae590
·
verified ·
1 Parent(s): 7d47701

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -38
app.py CHANGED
@@ -39,19 +39,6 @@ if not os.path.exists(FAISS_INDEX_DIR):
39
  # Dictionary to store user-specific vectorstores
40
  user_vectorstores = {}
41
 
42
- # Load SmolDocling model for image analysis
43
- def load_docling_model():
44
- try:
45
- processor = AutoProcessor.from_pretrained("ds4sd/SmolDocling-256M-preview")
46
- model = AutoModelForVision2Seq.from_pretrained("ds4sd/SmolDocling-256M-preview")
47
- return processor, model
48
- except Exception as e:
49
- print(f"Error loading SmolDocling model: {e}")
50
- return None, None
51
-
52
- # Initialize SmolDocling model
53
- docling_processor, docling_model = load_docling_model()
54
-
55
  # Custom CSS for Tech theme
56
  custom_css = """
57
  :root {
@@ -278,39 +265,40 @@ def process_excel(excel_file):
278
 
279
  # Function to analyze image using SmolDocling
280
  def analyze_image(image_file):
 
 
 
281
  if image_file is None:
282
  return "No image uploaded. Please upload an image to analyze."
283
 
284
- if docling_processor is None or docling_model is None:
285
- return "SmolDocling model not loaded. Please check your installation."
286
-
287
  try:
288
- # Process the image - image_file is a filepath string from Gradio
289
  image = Image.open(image_file)
 
 
 
290
 
291
- # Use the SmolDocling model
292
- inputs = docling_processor(images=image, return_tensors="pt")
293
- with torch.no_grad():
294
- outputs = docling_model.generate(
295
- **inputs,
296
- max_new_tokens=512,
297
- temperature=0.1,
298
- do_sample=False
299
- )
300
-
301
- # Decode the output
302
- result = docling_processor.batch_decode(outputs, skip_special_tokens=True)[0]
303
-
304
- # Format the result for display with technical emphasis
305
- analysis = f"## Technical Document Analysis Results\n\n{result}\n\n"
306
- analysis += "### Technical Insights\n\n"
307
- analysis += "* The analysis provides technical information extracted from the document image.\n"
308
- analysis += "* Consider this information as a starting point for further technical investigation.\n"
309
- analysis += "* For code snippets or technical specifications, verify accuracy before implementation.\n"
310
-
311
  return analysis
312
  except Exception as e:
313
- return f"Error analyzing image: {str(e)}"
314
 
315
  # Function to handle different file types
316
  def process_file(file_data, file_type):
 
39
  # Dictionary to store user-specific vectorstores
40
  user_vectorstores = {}
41
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42
  # Custom CSS for Tech theme
43
  custom_css = """
44
  :root {
 
265
 
266
  # Function to analyze image using SmolDocling
267
  def analyze_image(image_file):
268
+ """
269
+ Basic image analysis function that doesn't rely on external models
270
+ """
271
  if image_file is None:
272
  return "No image uploaded. Please upload an image to analyze."
273
 
 
 
 
274
  try:
 
275
  image = Image.open(image_file)
276
+ width, height = image.size
277
+ format = image.format
278
+ mode = image.mode
279
 
280
+ analysis = f"""## Technical Document Analysis
281
+
282
+ **Image Properties:**
283
+ - Dimensions: {width}x{height} pixels
284
+ - Format: {format}
285
+ - Color Mode: {mode}
286
+
287
+ **Technical Analysis:**
288
+ 1. Document Quality:
289
+ - Resolution: {'High' if width > 2000 or height > 2000 else 'Medium' if width > 1000 or height > 1000 else 'Low'}
290
+ - Color Depth: {mode}
291
+
292
+ 2. Recommendations:
293
+ - For text extraction, consider using PDF format
294
+ - For technical diagrams, ensure high resolution
295
+ - Consider OCR for text content
296
+
297
+ **Note:** For detailed technical analysis, please convert to PDF format
298
+ """
 
299
  return analysis
300
  except Exception as e:
301
+ return f"Error analyzing image: {str(e)}\n\nPlease try using PDF format instead."
302
 
303
  # Function to handle different file types
304
  def process_file(file_data, file_type):