mohonplugins commited on
Commit
dbc99b9
·
verified ·
1 Parent(s): 60f27d1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -10
app.py CHANGED
@@ -1,16 +1,36 @@
1
  import gradio as gr
2
- from PIL import Image
 
 
3
 
4
- def process_image(img):
5
- # Future: Convert image to Figma-compatible format (like SVG or .fig)
6
- return "Image received. Converting to Figma design..."
 
7
 
8
- demo = gr.Interface(
9
- fn=process_image,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
  inputs=gr.Image(type="pil"),
11
- outputs="text",
12
- title="Image to Figma Layers",
13
- description="Upload a PNG or JPEG image. This will be converted into editable Figma design layers."
14
  )
15
 
16
- demo.launch()
 
1
  import gradio as gr
2
+ from transformers import DetrImageProcessor, DetrForObjectDetection
3
+ import torch
4
+ from PIL import Image, ImageDraw
5
 
6
+ # Load the DETR layout model
7
+ model_name = "cmarkea/detr-layout-detection"
8
+ processor = DetrImageProcessor.from_pretrained(model_name)
9
+ model = DetrForObjectDetection.from_pretrained(model_name)
10
 
11
+ def detect_layout(image):
12
+ inputs = processor(images=image, return_tensors="pt")
13
+ outputs = model(**inputs)
14
+ target_sizes = torch.tensor([image.size[::-1]]) # H x W
15
+ results = processor.post_process_object_detection(outputs, target_sizes=target_sizes, threshold=0.7)[0]
16
+
17
+ draw = ImageDraw.Draw(image)
18
+ labels = []
19
+ for score, label, box in zip(results["scores"], results["labels"], results["boxes"]):
20
+ box = [round(i, 2) for i in box.tolist()]
21
+ draw.rectangle(box, outline="red", width=2)
22
+ label_name = model.config.id2label[label.item()]
23
+ draw.text((box[0] + 4, box[1]), f"{label_name} ({round(score.item(), 2)})", fill="red")
24
+ labels.append({"label": label_name, "score": round(score.item(), 2), "box": box})
25
+
26
+ return image, labels
27
+
28
+ iface = gr.Interface(
29
+ fn=detect_layout,
30
  inputs=gr.Image(type="pil"),
31
+ outputs=[gr.Image(type="pil"), gr.JSON()],
32
+ title="Image to Figma Layers (with DETR)",
33
+ description="Upload a PNG or JPEG UI image to detect editable layers using a layout-aware DETR model."
34
  )
35
 
36
+ iface.launch()