Spaces:
Sleeping
Sleeping
Improve no-token diagram fallback experience
Browse files- README.md +3 -3
- __pycache__/app.cpython-314.pyc +0 -0
- app.py +133 -22
README.md
CHANGED
|
@@ -1,8 +1,8 @@
|
|
| 1 |
---
|
| 2 |
-
title: Diagram
|
| 3 |
emoji: π
|
| 4 |
-
colorFrom:
|
| 5 |
-
colorTo:
|
| 6 |
sdk: gradio
|
| 7 |
app_file: app.py
|
| 8 |
pinned: false
|
|
|
|
| 1 |
---
|
| 2 |
+
title: Diagram To Code
|
| 3 |
emoji: π
|
| 4 |
+
colorFrom: blue
|
| 5 |
+
colorTo: green
|
| 6 |
sdk: gradio
|
| 7 |
app_file: app.py
|
| 8 |
pinned: false
|
__pycache__/app.cpython-314.pyc
ADDED
|
Binary file (12.1 kB). View file
|
|
|
app.py
CHANGED
|
@@ -15,8 +15,9 @@ import sys
|
|
| 15 |
sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
|
| 16 |
from shared.components import create_method_panel, create_premium_hero
|
| 17 |
|
| 18 |
-
# Initialize Hugging Face Inference Client
|
| 19 |
-
|
|
|
|
| 20 |
|
| 21 |
PROMPTS = {
|
| 22 |
"Database Schema β SQL": """Analyze this database schema diagram and generate SQL code.
|
|
@@ -114,35 +115,145 @@ Then generate clean, commented code based on the drawing. Be creative in interpr
|
|
| 114 |
}
|
| 115 |
|
| 116 |
|
| 117 |
-
def
|
| 118 |
-
"""
|
| 119 |
-
|
| 120 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 121 |
|
| 122 |
-
|
| 123 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 124 |
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 128 |
|
| 129 |
-
|
| 130 |
|
| 131 |
-
|
| 132 |
|
| 133 |
-
##
|
| 134 |
|
| 135 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 136 |
|
| 137 |
-
|
| 138 |
-
2. Identify arrows, relationships, or control flow.
|
| 139 |
-
3. Map each visual element to a code construct.
|
| 140 |
-
4. Write the smallest runnable scaffold first.
|
| 141 |
-
5. Review the generated or manual code before using it.
|
| 142 |
|
| 143 |
-
|
| 144 |
"""
|
| 145 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 146 |
# Convert PIL Image to base64
|
| 147 |
buffered = BytesIO()
|
| 148 |
if isinstance(image, str):
|
|
@@ -195,7 +306,7 @@ This fallback keeps the Space usable as a teaching tool even before private infe
|
|
| 195 |
return formatted
|
| 196 |
|
| 197 |
except Exception as e:
|
| 198 |
-
return
|
| 199 |
|
| 200 |
|
| 201 |
# Gradio Interface
|
|
|
|
| 15 |
sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
|
| 16 |
from shared.components import create_method_panel, create_premium_hero
|
| 17 |
|
| 18 |
+
# Initialize Hugging Face Inference Client. A token improves reliability on Spaces,
|
| 19 |
+
# but public inference may still work depending on provider availability.
|
| 20 |
+
client = InferenceClient(token=os.getenv("HF_TOKEN") or None)
|
| 21 |
|
| 22 |
PROMPTS = {
|
| 23 |
"Database Schema β SQL": """Analyze this database schema diagram and generate SQL code.
|
|
|
|
| 115 |
}
|
| 116 |
|
| 117 |
|
| 118 |
+
def fallback_scaffold(diagram_type, reason):
|
| 119 |
+
"""Return a polished deterministic scaffold when live VLM inference is unavailable."""
|
| 120 |
+
templates = {
|
| 121 |
+
"Database Schema β SQL": """```sql
|
| 122 |
+
-- Diagram interpretation scaffold
|
| 123 |
+
-- Replace the example entities below with the tables, columns, and relationships visible in your diagram.
|
| 124 |
+
|
| 125 |
+
CREATE TABLE users (
|
| 126 |
+
id BIGSERIAL PRIMARY KEY,
|
| 127 |
+
email TEXT NOT NULL UNIQUE,
|
| 128 |
+
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
|
| 129 |
+
);
|
| 130 |
+
|
| 131 |
+
CREATE TABLE projects (
|
| 132 |
+
id BIGSERIAL PRIMARY KEY,
|
| 133 |
+
owner_id BIGINT NOT NULL REFERENCES users(id),
|
| 134 |
+
name TEXT NOT NULL,
|
| 135 |
+
status TEXT NOT NULL DEFAULT 'active',
|
| 136 |
+
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
|
| 137 |
+
);
|
| 138 |
+
|
| 139 |
+
CREATE INDEX idx_projects_owner_id ON projects(owner_id);
|
| 140 |
+
```
|
| 141 |
+
""",
|
| 142 |
+
"Flowchart β Python": """```python
|
| 143 |
+
def run_workflow(input_payload: dict) -> dict:
|
| 144 |
+
\"\"\"Implementation scaffold for a flowchart-style process.\"\"\"
|
| 145 |
+
state = {"status": "started", "steps": []}
|
| 146 |
+
|
| 147 |
+
if not input_payload:
|
| 148 |
+
return {"status": "rejected", "reason": "missing input"}
|
| 149 |
+
|
| 150 |
+
state["steps"].append("validate_input")
|
| 151 |
+
|
| 152 |
+
if input_payload.get("requires_review"):
|
| 153 |
+
state["steps"].append("manual_review")
|
| 154 |
+
state["status"] = "pending_review"
|
| 155 |
+
else:
|
| 156 |
+
state["steps"].append("automatic_processing")
|
| 157 |
+
state["status"] = "completed"
|
| 158 |
+
|
| 159 |
+
return state
|
| 160 |
+
```
|
| 161 |
+
""",
|
| 162 |
+
"UML Class Diagram β Classes": """```python
|
| 163 |
+
from dataclasses import dataclass, field
|
| 164 |
+
|
| 165 |
+
|
| 166 |
+
@dataclass
|
| 167 |
+
class Entity:
|
| 168 |
+
\"\"\"Base class inferred from a UML class diagram.\"\"\"
|
| 169 |
+
id: int
|
| 170 |
+
|
| 171 |
+
|
| 172 |
+
@dataclass
|
| 173 |
+
class Project(Entity):
|
| 174 |
+
name: str
|
| 175 |
+
owner_id: int
|
| 176 |
+
tasks: list[str] = field(default_factory=list)
|
| 177 |
+
|
| 178 |
+
def add_task(self, task: str) -> None:
|
| 179 |
+
self.tasks.append(task)
|
| 180 |
+
```
|
| 181 |
+
""",
|
| 182 |
+
"UI Mockup β HTML/CSS": """```html
|
| 183 |
+
<main class="dashboard-shell">
|
| 184 |
+
<section class="hero-card">
|
| 185 |
+
<p class="eyebrow">Product Insight</p>
|
| 186 |
+
<h1>Readable UI scaffold from a visual mockup</h1>
|
| 187 |
+
<p>Replace these sections with the components visible in your uploaded wireframe.</p>
|
| 188 |
+
<button>Primary action</button>
|
| 189 |
+
</section>
|
| 190 |
+
</main>
|
| 191 |
+
```
|
| 192 |
+
|
| 193 |
+
```css
|
| 194 |
+
.dashboard-shell {
|
| 195 |
+
min-height: 100vh;
|
| 196 |
+
padding: 48px;
|
| 197 |
+
background: linear-gradient(135deg, #f6efe4, #dfefff);
|
| 198 |
+
font-family: ui-sans-serif, system-ui;
|
| 199 |
+
}
|
| 200 |
|
| 201 |
+
.hero-card {
|
| 202 |
+
max-width: 760px;
|
| 203 |
+
padding: 40px;
|
| 204 |
+
border-radius: 28px;
|
| 205 |
+
background: rgba(255, 255, 255, 0.82);
|
| 206 |
+
box-shadow: 0 24px 80px rgba(20, 32, 54, 0.16);
|
| 207 |
+
}
|
| 208 |
+
```
|
| 209 |
+
""",
|
| 210 |
+
"Whiteboard/Napkin Sketch β Code": """```python
|
| 211 |
+
def interpret_sketch(elements: list[dict]) -> dict:
|
| 212 |
+
\"\"\"Starter adapter for turning rough whiteboard elements into implementation tasks.\"\"\"
|
| 213 |
+
entities = [item for item in elements if item.get("kind") == "entity"]
|
| 214 |
+
actions = [item for item in elements if item.get("kind") == "action"]
|
| 215 |
+
relationships = [item for item in elements if item.get("kind") == "relationship"]
|
| 216 |
|
| 217 |
+
return {
|
| 218 |
+
"entities_to_model": entities,
|
| 219 |
+
"functions_to_write": actions,
|
| 220 |
+
"interfaces_to_define": relationships,
|
| 221 |
+
}
|
| 222 |
+
```
|
| 223 |
+
""",
|
| 224 |
+
}
|
| 225 |
+
|
| 226 |
+
return f"""# {diagram_type}
|
| 227 |
+
|
| 228 |
+
## Implementation Scaffold
|
| 229 |
|
| 230 |
+
Live vision inference is temporarily unavailable in this runtime, so the Space is returning a professional starter scaffold for the selected diagram type.
|
| 231 |
|
| 232 |
+
{templates.get(diagram_type, templates["Whiteboard/Napkin Sketch β Code"])}
|
| 233 |
|
| 234 |
+
## How To Use This Result
|
| 235 |
|
| 236 |
+
1. Replace the placeholder names with the labels visible in your diagram.
|
| 237 |
+
2. Map each box to a table, class, function, component, or process step.
|
| 238 |
+
3. Map arrows to dependencies, foreign keys, inheritance, or control flow.
|
| 239 |
+
4. Run the scaffold early, then refine the semantics.
|
| 240 |
+
5. Treat the generated code as a draft and review it like any other model output.
|
| 241 |
|
| 242 |
+
## Runtime Note
|
|
|
|
|
|
|
|
|
|
|
|
|
| 243 |
|
| 244 |
+
Automatic diagram understanding uses a hosted vision-language model when Hugging Face inference is available. Current fallback reason: `{reason}`.
|
| 245 |
"""
|
| 246 |
|
| 247 |
+
|
| 248 |
+
def generate_code(image, diagram_type):
|
| 249 |
+
"""Convert diagram to code using vision model."""
|
| 250 |
+
if image is None:
|
| 251 |
+
return "β Please upload a diagram first!"
|
| 252 |
+
|
| 253 |
+
if diagram_type not in PROMPTS:
|
| 254 |
+
return "β Please select a diagram type!"
|
| 255 |
+
|
| 256 |
+
try:
|
| 257 |
# Convert PIL Image to base64
|
| 258 |
buffered = BytesIO()
|
| 259 |
if isinstance(image, str):
|
|
|
|
| 306 |
return formatted
|
| 307 |
|
| 308 |
except Exception as e:
|
| 309 |
+
return fallback_scaffold(diagram_type, str(e))
|
| 310 |
|
| 311 |
|
| 312 |
# Gradio Interface
|