File size: 7,772 Bytes
4412065
 
 
 
 
 
 
 
 
 
 
380204e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4412065
 
54056c4
 
4412065
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
54056c4
 
 
 
 
 
4412065
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ff86d3d
 
 
 
 
 
 
 
 
 
 
4412065
 
 
 
 
 
 
 
 
 
 
380204e
 
4412065
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
"""Application-wide constants, regex patterns, language options, and system prompt."""

from __future__ import annotations

import re

# ─── App Identity ────────────────────────────────────────────────────────

APP_TITLE = "Fullstack Code Builder"
MODEL_URL = "https://huggingface.co/openbmb/MiniCPM5-1B"

# ─── Model Configs ───────────────────────────────────────────────────────

MODEL_CONFIGS = {
    "minicpm5-1b": {
        "id": "openbmb/MiniCPM5-1B",
        "name": "MiniCPM5-1B",
        "type": "text",
        "description": "Text-only, fast code generation",
        "auto_class": "AutoModelForCausalLM",
        "tokenizer_class": "AutoTokenizer",
        "size_gb": 2.17,
    },
    "minicpm-v-4.6": {
        "id": "openbmb/MiniCPM-V-4.6",
        "name": "MiniCPM-V-4.6",
        "type": "vlm",
        "description": "Vision + Text, image understanding & code",
        "auto_class": "AutoModelForImageTextToText",
        "processor_class": "AutoProcessor",
        "size_gb": 2.8,
    },
}

DEFAULT_MODEL_KEY = "minicpm5-1b"

# Keep backward compat aliases
MODEL_ID = MODEL_CONFIGS[DEFAULT_MODEL_KEY]["id"]

# ─── Runtime Defaults ───────────────────────────────────────────────────

DEFAULT_TEMPERATURE = 0.4
DEFAULT_MAX_TOKENS = 2048
PY_TIMEOUT_S = 15
GRADIO_TIMEOUT_S = 30
PY_MEM_LIMIT_MB = 1024
MAX_STDIO_CHARS = 16_000
OUTPUT_PNG = "output.png"

# ─── Regex Patterns ─────────────────────────────────────────────────────

THINKING_BLOCK_RE = re.compile(
    r"<\s*think\s*>.*?<\s*/\s*think\s*>", re.IGNORECASE | re.DOTALL
)
CODE_BLOCK_RE = re.compile(
    r"```([a-zA-Z0-9_+.#-]*)\s*\n(.*?)```", re.DOTALL
)
FILE_BLOCK_RE = re.compile(
    r"@@FILE:\s*(.+?)@@\s*\n(.*?)(?=@@FILE:|@@END@@)", re.DOTALL
)

# ─── Supported Languages & Frameworks ───────────────────────────────────

LANGUAGE_OPTIONS: list[tuple[str, list[str]]] = [
    ("Python", ["Gradio", "Flask", "Django", "FastAPI", "Streamlit", "Plain Python"]),
    ("JavaScript", ["React", "Vue.js", "Next.js", "Express.js", "Node.js", "Vanilla JS"]),
    ("TypeScript", ["React", "Next.js", "Express.js", "NestJS"]),
    ("HTML/CSS/JS", ["Tailwind CSS", "Bootstrap", "Vanilla"]),
    ("Java", ["Spring Boot", "Maven", "Gradle"]),
    ("Go", ["Gin", "Fiber", "Echo", "Plain Go"]),
    ("Rust", ["Actix", "Axum", "Rocket"]),
    ("PHP", ["Laravel", "Symfony", "Plain PHP"]),
    ("Ruby", ["Rails", "Sinatra"]),
    ("C#", ["ASP.NET", "Blazor"]),
    ("Swift", ["Vapor", "SwiftUI"]),
    ("Kotlin", ["Ktor", "Spring Boot"]),
]

LANGUAGE_MAP: dict[str, list[str]] = {lang: frameworks for lang, frameworks in LANGUAGE_OPTIONS}

# ─── System Prompt ───────────────────────────────────────────────────────

SYSTEM_PROMPT = """You are a code generator. Output ONLY the code. No thinking, no explanation, no commentary.

CRITICAL RULES:
- Do NOT use <think> or <thinking> tags. Do NOT reason aloud. Just output code directly.
- Do NOT write explanations before or after code. Just output the code.
- If you must explain something, keep it to ONE short sentence.

When the user asks you to build an application:
1. Generate complete, working code - not snippets or pseudocode
2. Include all necessary files for the project to run
3. Add proper error handling and comments
4. For web apps, make the UI responsive and modern
5. For Gradio apps, use gradio library and create a complete working app with gr.Interface or gr.Blocks

FILE OUTPUT FORMAT - IMPORTANT:
When generating multi-file projects, wrap each file in this format:
@@FILE: path/to/file.ext@@
(file content here)
@@FILE: path/to/another/file.ext@@
(another file content here)
@@END@@

For single-file code, use standard markdown fenced blocks:
```python for Python
```html for HTML/CSS/JS
```javascript for JavaScript
```typescript for TypeScript
etc.

JAVASCRIPT / TYPESCRIPT PROJECTS:
For React, Next.js, Vue.js, Express, NestJS, or any JS/TS framework:
- ALWAYS use the @@FILE: multi-file format
- Include a package.json with name, version, scripts, and dependencies
- Include all source files (src/App.jsx, src/index.js, etc.)
- For React+Vite: include vite.config.js and index.html
- For Next.js: include next.config.js with output: 'standalone'
- For Express: main entry is index.js with app.listen(7860)
- Server ports MUST be 7860 and bind to 0.0.0.0
- Do NOT include node_modules or lock files

When generating web apps with HTML/CSS/JS, return a single self-contained HTML document with all CSS and JavaScript inline. Make the page fully responsive: html/body at margin:0 and 100% width/height, use flexbox/grid layouts, and size any canvas to its container.

When generating Gradio apps, create a complete app.py with:
- import gradio as gr
- Define the interface using gr.Interface() or gr.Blocks()
- Call iface.launch(server_name="0.0.0.0", server_port=7860) at the end
- Include all necessary processing logic inline

For Python, prefer standard library or common packages. Do not use network calls, subprocesses, shell commands, or long-running loops in demo code (except Gradio apps which are server-based).

If web search results are provided in the context, use them to inform your code generation. Incorporate relevant information from the search results into the generated code.

If the user provides an image, analyze it and generate code based on what you see in the image. For example: replicate a UI from a screenshot, generate code from a wireframe, or build an app described in a document.
"""

# ─── Example Prompts ────────────────────────────────────────────────────

EXAMPLE_PROMPTS: list[tuple[str, str, str, str]] = [
    (
        "🎨 Gradio Image Filter",
        "Create a Gradio app that lets users upload an image and apply filters like grayscale, blur, sepia, and edge detection using PIL. Show the original and filtered images side by side.",
        "Python",
        "Gradio",
    ),
    (
        "πŸ€– Gradio Chat App",
        "Build a Gradio chatbot app with gr.Blocks that has a chat interface, a text input, and a send button. Include a simple echo bot that repeats the user's message with a fun twist.",
        "Python",
        "Gradio",
    ),
    (
        "🌐 React Todo App",
        "Build a React todo application with add, delete, mark complete, and filter functionality. Use modern hooks and a clean responsive UI.",
        "JavaScript",
        "React",
    ),
    (
        "🐍 Flask API",
        "Create a Flask REST API for a book library with CRUD operations, in-memory storage, and proper error handling.",
        "Python",
        "Flask",
    ),
    (
        "🎨 Landing Page",
        "Build a modern landing page for a SaaS product with a hero section, features grid, pricing cards, and a footer. Use Tailwind-style CSS.",
        "HTML/CSS/JS",
        "Vanilla",
    ),
    (
        "πŸ“Š Dashboard",
        "Create an interactive data dashboard with charts (bar, line, pie), a sidebar navigation, and summary cards. All in a single HTML file.",
        "HTML/CSS/JS",
        "Vanilla",
    ),
]