davanstrien HF Staff commited on
Commit
42423b1
Β·
verified Β·
1 Parent(s): 94afef5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -7
app.py CHANGED
@@ -58,35 +58,55 @@ def extract_metadata(image):
58
 
59
  try:
60
  # Ensure image is PIL Image
 
61
  if not isinstance(image, PILImage.Image):
62
  image = PILImage.open(image).convert("RGB")
 
 
63
 
64
  # Set format (required by Outlines Image class)
65
  if not image.format:
66
  image.format = "PNG"
 
 
 
 
 
 
67
 
68
  # Create Chat prompt with Image
 
 
 
 
 
 
 
 
69
  prompt = Chat(
70
  messages=[
71
  {
72
  "role": "user",
73
- "content": [
74
- {"type": "text", "text": EXTRACTION_PROMPT},
75
- {"type": "image", "image": Image(image)},
76
- ],
77
  }
78
  ]
79
  )
 
80
 
81
  # Generate with structured output - guaranteed valid JSON
 
82
  result = model(prompt, CatalogCardMetadata, max_new_tokens=512)
 
83
 
84
  # Parse and format (always valid JSON with Outlines)
85
  metadata = CatalogCardMetadata.model_validate_json(result)
86
- return str(json.dumps(metadata.model_dump(exclude_none=True), indent=2))
87
 
88
  except Exception as e:
89
- return f"Error during extraction: {str(e)}"
 
 
 
90
 
91
 
92
  # Create Gradio interface
@@ -111,7 +131,7 @@ with gr.Blocks(title="Library Card Metadata Extractor") as demo:
111
 
112
  with gr.Column(scale=1):
113
  gr.Markdown("### πŸ“‹ Extracted Metadata (JSON)")
114
- output = gr.Text()
115
 
116
  submit_btn.click(fn=extract_metadata, inputs=image_input, outputs=output)
117
 
 
58
 
59
  try:
60
  # Ensure image is PIL Image
61
+ print(f"DEBUG: Received image type: {type(image)}")
62
  if not isinstance(image, PILImage.Image):
63
  image = PILImage.open(image).convert("RGB")
64
+ print(f"DEBUG: After conversion, image type: {type(image)}")
65
+ print(f"DEBUG: Image format before setting: {image.format}")
66
 
67
  # Set format (required by Outlines Image class)
68
  if not image.format:
69
  image.format = "PNG"
70
+ print(f"DEBUG: Image format after setting: {image.format}")
71
+
72
+ # Wrap in Outlines Image
73
+ outlines_image = Image(image)
74
+ print(f"DEBUG: Outlines Image created: {type(outlines_image)}")
75
+ print(f"DEBUG: Outlines Image.image type: {type(outlines_image.image)}")
76
 
77
  # Create Chat prompt with Image
78
+ content_list = [
79
+ {"type": "text", "text": EXTRACTION_PROMPT},
80
+ {"type": "image", "image": outlines_image},
81
+ ]
82
+ print(f"DEBUG: Content list created: {len(content_list)} items")
83
+ print(f"DEBUG: Content[0] type: {content_list[0]['type']}")
84
+ print(f"DEBUG: Content[1] type: {content_list[1]['type']}, image type: {type(content_list[1]['image'])}")
85
+
86
  prompt = Chat(
87
  messages=[
88
  {
89
  "role": "user",
90
+ "content": content_list,
 
 
 
91
  }
92
  ]
93
  )
94
+ print(f"DEBUG: Chat prompt created successfully")
95
 
96
  # Generate with structured output - guaranteed valid JSON
97
+ print(f"DEBUG: Starting generation...")
98
  result = model(prompt, CatalogCardMetadata, max_new_tokens=512)
99
+ print(f"DEBUG: Generation complete, result type: {type(result)}")
100
 
101
  # Parse and format (always valid JSON with Outlines)
102
  metadata = CatalogCardMetadata.model_validate_json(result)
103
+ return json.dumps(metadata.model_dump(exclude_none=True), indent=2)
104
 
105
  except Exception as e:
106
+ import traceback
107
+ error_msg = f"Error during extraction: {str(e)}\n\nTraceback:\n{traceback.format_exc()}"
108
+ print(error_msg)
109
+ return error_msg
110
 
111
 
112
  # Create Gradio interface
 
131
 
132
  with gr.Column(scale=1):
133
  gr.Markdown("### πŸ“‹ Extracted Metadata (JSON)")
134
+ output = gr.Code(label="Metadata", language="json", lines=15)
135
 
136
  submit_btn.click(fn=extract_metadata, inputs=image_input, outputs=output)
137