Sebbe33 commited on
Commit
6c64ea6
·
verified ·
1 Parent(s): 91b6e57

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -34
app.py CHANGED
@@ -18,7 +18,7 @@ Only return the list, nothing else. Example:
18
  def parse_list_boxes(text):
19
  """Improved parsing with better error handling"""
20
  try:
21
- return eval(text) # Safer alternative: Use ast.literal_eval
22
  except:
23
  matches = re.findall(r'\[([\d\.]+),\s*([\d\.]+),\s*([\d\.]+),\s*([\d\.]+)\]', text)
24
  return [[float(x) for x in m] for m in matches]
@@ -50,42 +50,50 @@ uploaded_file = st.file_uploader("Upload PDF", type=["pdf"])
50
  if uploaded_file and st.button("Analyze"):
51
  with st.spinner("Processing..."):
52
  try:
53
- images = convert_from_bytes(uploaded_file.read(), dpi=300) # Increased DPI
54
- client = genai.Client(api_key=os.getenv("KEY")) # Verify env var name
55
 
56
- for idx, image in enumerate(images):
57
- with st.expander(f"Page {idx+1}", expanded=True):
58
- img_byte_arr = io.BytesIO()
59
- image.save(img_byte_arr, format='PNG')
60
-
61
-
62
- # Get bounding boxes
63
- response = client.models.generate_content(
64
- model="gemini-2.0-flash-exp",
65
- contents=[
66
- DETECTION_PROMPT,
67
- types.Part.from_bytes(
68
- data=img_byte_arr.getvalue(),
69
- mime_type="image/png"
70
- )
71
- ]
72
- )
73
-
74
-
75
- # Debug output
76
- with st.expander("Raw API Response"):
77
- st.code(response.text)
78
 
79
- # Parse and draw
80
- boxes = parse_list_boxes(response.text)
81
- annotated = draw_bounding_boxes(image.copy(), boxes)
82
 
83
- # Display
84
- cols = st.columns(2)
85
- cols[0].image(image, caption="Original", use_column_width=True)
86
- cols[1].image(annotated,
87
- caption=f"Detected {len(boxes)} text regions",
88
- use_column_width=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
89
 
90
  except Exception as e:
91
  st.error(f"Error: {str(e)}")
 
18
  def parse_list_boxes(text):
19
  """Improved parsing with better error handling"""
20
  try:
21
+ return eval(text)
22
  except:
23
  matches = re.findall(r'\[([\d\.]+),\s*([\d\.]+),\s*([\d\.]+),\s*([\d\.]+)\]', text)
24
  return [[float(x) for x in m] for m in matches]
 
50
  if uploaded_file and st.button("Analyze"):
51
  with st.spinner("Processing..."):
52
  try:
53
+ images = convert_from_bytes(uploaded_file.read(), dpi=300)
54
+ client = genai.Client(api_key=os.getenv("GOOGLE_API_KEY"))
55
 
56
+ # Create tabs for pages
57
+ tabs = st.tabs([f"Page {i+1}" for i in range(len(images))])
58
+
59
+ for idx, (tab, image) in enumerate(zip(tabs, images)):
60
+ with tab:
61
+ col1, col2 = st.columns(2)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
62
 
63
+ with col1:
64
+ st.image(image, caption="Original", use_column_width=True)
 
65
 
66
+ with col2:
67
+ img_byte_arr = io.BytesIO()
68
+ image.save(img_byte_arr, format='PNG')
69
+
70
+ # Get bounding boxes
71
+ response = client.models.generate_content(
72
+ model="gemini-2.0-flash-exp",
73
+ contents=[
74
+ DETECTION_PROMPT,
75
+ types.Part.from_bytes(
76
+ data=img_byte_arr.getvalue(),
77
+ mime_type="image/png"
78
+ )
79
+ ]
80
+ )
81
+
82
+ # Parse and draw
83
+ boxes = parse_list_boxes(response.text)
84
+ annotated = draw_bounding_boxes(image.copy(), boxes)
85
+
86
+ st.image(annotated,
87
+ caption=f"Detected {len(boxes)} text regions",
88
+ use_column_width=True)
89
+
90
+ # Debug section
91
+ debug_expander = st.expander("Debug Details")
92
+ with debug_expander:
93
+ st.write("**Raw API Response:**")
94
+ st.code(response.text)
95
+ st.write("**Parsed Boxes:**")
96
+ st.write(boxes)
97
 
98
  except Exception as e:
99
  st.error(f"Error: {str(e)}")