Spaces:
Runtime error
Runtime error
acecalisto3
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -4,286 +4,525 @@ import random
|
|
4 |
import json
|
5 |
import os
|
6 |
import tempfile
|
7 |
-
import time
|
8 |
from pathlib import Path
|
9 |
from datetime import datetime
|
10 |
import logging
|
11 |
-
from typing import Dict, List, Any
|
|
|
12 |
|
13 |
# Configure logging
|
14 |
-
logging.basicConfig(
|
15 |
-
level=logging.INFO,
|
16 |
-
format='%(asctime)s - %(levelname)s - %(message)s'
|
17 |
-
)
|
18 |
logger = logging.getLogger(__name__)
|
19 |
|
20 |
-
#
|
21 |
-
|
22 |
-
"
|
23 |
-
"
|
24 |
-
|
25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
},
|
27 |
-
"
|
28 |
-
"
|
29 |
-
|
30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
31 |
},
|
32 |
-
"Image": {
|
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 |
-
5: "Deployment"
|
84 |
}
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
99 |
|
100 |
-
def
|
101 |
-
|
102 |
-
|
103 |
-
code += " " + Component(**component).render() + "\n"
|
104 |
-
code += f"\n{self.app_name}.launch()\n"
|
105 |
-
return code
|
106 |
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
"Basic": "basic_app.json",
|
112 |
-
"Dashboard": "dashboard_template.json",
|
113 |
-
"Data Analysis": "data_analysis_template.json",
|
114 |
-
"ML Interface": "ml_interface_template.json"
|
115 |
-
}
|
116 |
|
117 |
-
|
118 |
-
"
|
119 |
-
"
|
120 |
-
"
|
121 |
-
"
|
|
|
122 |
}
|
123 |
|
124 |
-
self.
|
125 |
-
|
126 |
-
"properties": {
|
127 |
-
"type": ["line", "bar", "scatter", "histogram"],
|
128 |
-
"data_source": "",
|
129 |
-
"x_axis": "",
|
130 |
-
"y_axis": ""
|
131 |
-
},
|
132 |
-
"code_snippet": 'gr.Plot(value=plot_{id}, label="{label}")'
|
133 |
-
},
|
134 |
-
"FileUploader": {
|
135 |
-
"properties": {
|
136 |
-
"file_types": [".csv", ".txt", ".pdf", ".png", ".jpg"],
|
137 |
-
"multiple": False
|
138 |
-
},
|
139 |
-
"code_snippet": 'gr.File(file_types={file_types}, label="{label}")'
|
140 |
-
}
|
141 |
-
}
|
142 |
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
except Exception as e:
|
154 |
-
return False, f"Validation error: {str(e)}"
|
155 |
|
156 |
-
|
157 |
-
class EnhancedAppCreationProcess(AppCreationProcess):
|
158 |
def __init__(self):
|
159 |
-
|
160 |
-
self.
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
172 |
}
|
173 |
-
self.history.append(snapshot)
|
174 |
|
175 |
-
|
176 |
-
|
177 |
-
|
178 |
-
import pandas as pd
|
179 |
-
import numpy as np
|
180 |
-
from pathlib import Path
|
181 |
-
import logging
|
182 |
|
183 |
-
|
184 |
-
|
185 |
-
|
186 |
-
|
187 |
-
|
188 |
-
|
189 |
-
|
190 |
-
|
191 |
-
|
192 |
-
|
193 |
-
|
194 |
-
for
|
195 |
-
code += f" {Component(**component).render()}\n"
|
196 |
-
|
197 |
-
code += f"\n{self.app_name}.launch(share=True, debug=True)\n"
|
198 |
-
|
199 |
-
return code, []
|
200 |
|
201 |
-
|
202 |
-
|
203 |
-
|
204 |
-
|
205 |
-
|
206 |
-
|
207 |
-
|
208 |
-
|
209 |
-
|
210 |
-
|
|
|
|
|
|
|
|
|
211 |
|
212 |
-
|
213 |
-
with gr.
|
214 |
-
|
215 |
-
|
216 |
-
|
217 |
-
|
218 |
-
|
219 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
220 |
)
|
221 |
-
|
222 |
-
|
223 |
-
|
224 |
-
with gr.TabItem("📱 App Configuration"):
|
225 |
-
app_name_input = gr.Textbox(label="App Name")
|
226 |
-
template_dropdown = gr.Dropdown(
|
227 |
-
choices=list(app_process.builder.TEMPLATES.keys()),
|
228 |
-
label="Template"
|
229 |
)
|
230 |
-
|
231 |
-
|
232 |
-
|
233 |
-
|
234 |
-
|
235 |
-
|
236 |
-
|
237 |
-
|
238 |
-
|
239 |
-
|
240 |
-
|
241 |
-
|
242 |
-
|
243 |
-
|
244 |
-
|
245 |
-
|
246 |
-
|
247 |
-
|
248 |
-
|
249 |
-
|
250 |
-
|
251 |
-
|
252 |
-
|
253 |
-
|
254 |
-
|
255 |
-
|
256 |
-
|
257 |
-
|
258 |
-
|
259 |
-
|
260 |
-
|
261 |
-
|
262 |
-
|
263 |
-
|
264 |
-
|
265 |
-
|
266 |
-
|
267 |
-
|
268 |
-
|
269 |
-
|
270 |
-
|
271 |
-
|
272 |
-
try:
|
273 |
-
code, lint_results = await app_process.generate_enhanced_code()
|
274 |
-
return code, "Code generated successfully"
|
275 |
-
except Exception as e:
|
276 |
-
return str(e), f"❌ Error in code generation: {str(e)}"
|
277 |
-
|
278 |
-
# Connect event handlers
|
279 |
-
theme_dropdown.change(update_theme, inputs=[theme_dropdown], outputs=[init_status])
|
280 |
-
init_app_button.click(init_app, inputs=[app_name_input, template_dropdown], outputs=[init_status])
|
281 |
-
add_component_button.click(add_component, inputs=[component_type], outputs=[components_display, init_status])
|
282 |
-
generate_code_button.click(generate_code, outputs=[generated_code, lint_results])
|
283 |
-
|
284 |
-
return iface
|
285 |
-
|
286 |
-
# Launch the application
|
287 |
if __name__ == "__main__":
|
288 |
-
|
289 |
-
|
|
|
|
4 |
import json
|
5 |
import os
|
6 |
import tempfile
|
|
|
7 |
from pathlib import Path
|
8 |
from datetime import datetime
|
9 |
import logging
|
10 |
+
from typing import Dict, List, Any, Optional
|
11 |
+
import yaml
|
12 |
|
13 |
# Configure logging
|
14 |
+
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
|
|
|
|
|
|
15 |
logger = logging.getLogger(__name__)
|
16 |
|
17 |
+
# Template Categories and Configurations
|
18 |
+
TEMPLATE_CATEGORIES = {
|
19 |
+
"Chat Applications": {
|
20 |
+
"Basic Chat": {
|
21 |
+
"components": [
|
22 |
+
{"type": "Chatbot", "label": "Chat History"},
|
23 |
+
{"type": "Textbox", "label": "User Input", "placeholder": "Type your message..."},
|
24 |
+
{"type": "Button", "label": "Send", "variant": "primary"},
|
25 |
+
{"type": "Radio", "label": "Model", "choices": ["GPT-3.5", "GPT-4", "Claude", "Custom"]},
|
26 |
+
{"type": "Slider", "label": "Temperature", "minimum": 0, "maximum": 1, "step": 0.1},
|
27 |
+
{"type": "Checkbox", "label": "Stream Output"},
|
28 |
+
],
|
29 |
+
"layout": "vertical",
|
30 |
+
"css": """
|
31 |
+
.chat-container { max-width: 800px; margin: auto; }
|
32 |
+
.message { padding: 10px; margin: 5px; border-radius: 5px; }
|
33 |
+
.user { background: #e3f2fd; }
|
34 |
+
.bot { background: #f5f5f5; }
|
35 |
+
"""
|
36 |
+
},
|
37 |
+
"Advanced Chat": {
|
38 |
+
"components": [
|
39 |
+
{"type": "Chatbot", "label": "Chat History"},
|
40 |
+
{"type": "Textbox", "label": "User Input", "placeholder": "Type your message..."},
|
41 |
+
{"type": "Button", "label": "Send", "variant": "primary"},
|
42 |
+
{"type": "Dropdown", "label": "Model", "choices": ["GPT-3.5", "GPT-4", "Claude", "Custom"]},
|
43 |
+
{"type": "Slider", "label": "Temperature", "minimum": 0, "maximum": 1, "step": 0.1},
|
44 |
+
{"type": "Slider", "label": "Max Length", "minimum": 100, "maximum": 2000, "step": 100},
|
45 |
+
{"type": "Checkbox", "label": "Stream Output"},
|
46 |
+
{"type": "File", "label": "Upload Context", "file_types": [".txt", ".pdf"]},
|
47 |
+
{"type": "Dropdown", "label": "Language", "choices": ["English", "Spanish", "French", "German"]},
|
48 |
+
{"type": "Radio", "label": "Mode", "choices": ["Chat", "Completion", "QA"]},
|
49 |
+
],
|
50 |
+
"layout": "vertical",
|
51 |
+
"dependencies": ["openai", "transformers", "langchain"]
|
52 |
+
}
|
53 |
},
|
54 |
+
"Audio Generation": {
|
55 |
+
"Music Generator": {
|
56 |
+
"components": [
|
57 |
+
{"type": "Audio", "label": "Generated Audio", "source": "microphone"},
|
58 |
+
{"type": "Textbox", "label": "Prompt", "placeholder": "Describe the music..."},
|
59 |
+
{"type": "Dropdown", "label": "Style", "choices": ["Classical", "Jazz", "Rock", "Electronic"]},
|
60 |
+
{"type": "Slider", "label": "Duration (seconds)", "minimum": 5, "maximum": 60},
|
61 |
+
{"type": "Button", "label": "Generate", "variant": "primary"},
|
62 |
+
{"type": "File", "label": "Download", "file_types": [".mp3", ".wav"]},
|
63 |
+
],
|
64 |
+
"layout": "vertical",
|
65 |
+
"dependencies": ["audiocraft", "torchaudio"]
|
66 |
+
}
|
67 |
},
|
68 |
+
"Image Processing": {
|
69 |
+
"Image Generator": {
|
70 |
+
"components": [
|
71 |
+
{"type": "Image", "label": "Generated Image"},
|
72 |
+
{"type": "Textbox", "label": "Prompt", "placeholder": "Describe the image..."},
|
73 |
+
{"type": "Dropdown", "label": "Model", "choices": ["Stable Diffusion", "DALL-E", "Midjourney"]},
|
74 |
+
{"type": "Slider", "label": "Steps", "minimum": 20, "maximum": 100},
|
75 |
+
{"type": "Button", "label": "Generate", "variant": "primary"},
|
76 |
+
],
|
77 |
+
"layout": "vertical",
|
78 |
+
"dependencies": ["diffusers", "torch"]
|
79 |
+
},
|
80 |
+
"Image Editor": {
|
81 |
+
"components": [
|
82 |
+
{"type": "Image", "label": "Input Image", "source": "upload"},
|
83 |
+
{"type": "Image", "label": "Output Image"},
|
84 |
+
{"type": "Dropdown", "label": "Effect", "choices": ["Blur", "Sharpen", "Style Transfer"]},
|
85 |
+
{"type": "Slider", "label": "Intensity", "minimum": 0, "maximum": 1, "step": 0.1},
|
86 |
+
{"type": "Button", "label": "Apply", "variant": "primary"},
|
87 |
+
],
|
88 |
+
"layout": "horizontal",
|
89 |
+
"dependencies": ["pillow", "opencv-python"]
|
90 |
+
}
|
91 |
},
|
92 |
+
"3D Processing": {
|
93 |
+
"3D Model Viewer": {
|
94 |
+
"components": [
|
95 |
+
{"type": "Model3D", "label": "3D Model"},
|
96 |
+
{"type": "File", "label": "Upload Model", "file_types": [".obj", ".glb", ".gltf"]},
|
97 |
+
{"type": "Checkbox", "label": "Show Wireframe"},
|
98 |
+
{"type": "ColorPicker", "label": "Background Color"},
|
99 |
+
{"type": "Slider", "label": "Camera Distance", "minimum": 1, "maximum": 10},
|
100 |
+
],
|
101 |
+
"layout": "vertical",
|
102 |
+
"dependencies": ["pygltflib", "trimesh"]
|
103 |
+
}
|
104 |
},
|
105 |
+
"Data Analysis": {
|
106 |
+
"Data Explorer": {
|
107 |
+
"components": [
|
108 |
+
{"type": "File", "label": "Upload Dataset", "file_types": [".csv", ".xlsx"]},
|
109 |
+
{"type": "Dataframe", "label": "Data Preview"},
|
110 |
+
{"type": "Dropdown", "label": "Chart Type", "choices": ["Line", "Bar", "Scatter", "Histogram"]},
|
111 |
+
{"type": "Dropdown", "label": "X Axis"},
|
112 |
+
{"type": "Dropdown", "label": "Y Axis"},
|
113 |
+
{"type": "Plot", "label": "Visualization"},
|
114 |
+
],
|
115 |
+
"layout": "vertical",
|
116 |
+
"dependencies": ["pandas", "plotly"]
|
117 |
+
}
|
118 |
+
}
|
119 |
}
|
120 |
|
121 |
+
# Enhanced Component Registry with Categories
|
122 |
+
COMPONENT_CATEGORIES = {
|
123 |
+
"Basic Input": {
|
124 |
+
"Textbox": {
|
125 |
+
"properties": {
|
126 |
+
"label": "Text Input",
|
127 |
+
"placeholder": "Enter text...",
|
128 |
+
"lines": 1,
|
129 |
+
"type": "text"
|
130 |
+
},
|
131 |
+
"code_snippet": 'gr.Textbox(label="{label}", placeholder="{placeholder}", lines={lines})'
|
132 |
+
},
|
133 |
+
"Number": {
|
134 |
+
"properties": {
|
135 |
+
"label": "Number Input",
|
136 |
+
"value": 0,
|
137 |
+
"minimum": None,
|
138 |
+
"maximum": None
|
139 |
+
},
|
140 |
+
"code_snippet": 'gr.Number(label="{label}", value={value}, minimum={minimum}, maximum={maximum})'
|
141 |
+
},
|
142 |
+
"Slider": {
|
143 |
+
"properties": {
|
144 |
+
"label": "Slider",
|
145 |
+
"minimum": 0,
|
146 |
+
"maximum": 100,
|
147 |
+
"step": 1,
|
148 |
+
"value": 50
|
149 |
+
},
|
150 |
+
"code_snippet": 'gr.Slider(label="{label}", minimum={minimum}, maximum={maximum}, step={step}, value={value})'
|
151 |
}
|
152 |
+
},
|
153 |
+
"Media Input": {
|
154 |
+
"Image": {
|
155 |
+
"properties": {
|
156 |
+
"label": "Image Input",
|
157 |
+
"source": "upload",
|
158 |
+
"tool": "select",
|
159 |
+
"type": "filepath"
|
160 |
+
},
|
161 |
+
"code_snippet": 'gr.Image(label="{label}", source="{source}", tool="{tool}", type="{type}")'
|
162 |
+
},
|
163 |
+
"Audio": {
|
164 |
+
"properties": {
|
165 |
+
"label": "Audio Input",
|
166 |
+
"source": "upload",
|
167 |
+
"type": "filepath"
|
168 |
+
},
|
169 |
+
"code_snippet": 'gr.Audio(label="{label}", source="{source}", type="{type}")'
|
170 |
+
},
|
171 |
+
"Video": {
|
172 |
+
"properties": {
|
173 |
+
"label": "Video Input",
|
174 |
+
"source": "upload",
|
175 |
+
"format": "mp4"
|
176 |
+
},
|
177 |
+
"code_snippet": 'gr.Video(label="{label}", source="{source}", format="{format}")'
|
|
|
178 |
}
|
179 |
+
},
|
180 |
+
"Control Elements": {
|
181 |
+
"Button": {
|
182 |
+
"properties": {
|
183 |
+
"label": "Click Me",
|
184 |
+
"variant": "primary",
|
185 |
+
"size": "sm"
|
186 |
+
},
|
187 |
+
"code_snippet": 'gr.Button(value="{label}", variant="{variant}", size="{size}")'
|
188 |
+
},
|
189 |
+
"Checkbox": {
|
190 |
+
"properties": {
|
191 |
+
"label": "Check this",
|
192 |
+
"value": False
|
193 |
+
},
|
194 |
+
"code_snippet": 'gr.Checkbox(label="{label}", value={value})'
|
195 |
+
},
|
196 |
+
"Radio": {
|
197 |
+
"properties": {
|
198 |
+
"label": "Select One",
|
199 |
+
"choices": ["Option 1", "Option 2", "Option 3"],
|
200 |
+
"value": "Option 1"
|
201 |
+
},
|
202 |
+
"code_snippet": 'gr.Radio(label="{label}", choices={choices}, value="{value}")'
|
203 |
+
}
|
204 |
+
},
|
205 |
+
"Advanced Input": {
|
206 |
+
"JSON": {
|
207 |
+
"properties": {
|
208 |
+
"label": "JSON Input",
|
209 |
+
"value": "{}"
|
210 |
+
},
|
211 |
+
"code_snippet": 'gr.JSON(label="{label}", value={value})'
|
212 |
+
},
|
213 |
+
"File": {
|
214 |
+
"properties": {
|
215 |
+
"label": "File Upload",
|
216 |
+
"file_types": [".txt", ".pdf", ".csv"],
|
217 |
+
"multiple": False
|
218 |
+
},
|
219 |
+
"code_snippet": 'gr.File(label="{label}", file_types={file_types}, multiple={multiple})'
|
220 |
+
},
|
221 |
+
"Dataframe": {
|
222 |
+
"properties": {
|
223 |
+
"label": "Data Table",
|
224 |
+
"headers": ["Column 1", "Column 2"],
|
225 |
+
"interactive": True
|
226 |
+
},
|
227 |
+
"code_snippet": 'gr.Dataframe(label="{label}", headers={headers}, interactive={interactive})'
|
228 |
+
}
|
229 |
+
},
|
230 |
+
"Output Display": {
|
231 |
+
"Label": {
|
232 |
+
"properties": {
|
233 |
+
"label": "Output Label",
|
234 |
+
"value": "Result will appear here..."
|
235 |
+
},
|
236 |
+
"code_snippet": 'gr.Label(label="{label}", value="{value}")'
|
237 |
+
},
|
238 |
+
"Plot": {
|
239 |
+
"properties": {
|
240 |
+
"label": "Plot Output",
|
241 |
+
"type": "line"
|
242 |
+
},
|
243 |
+
"code_snippet": 'gr.Plot(label="{label}")'
|
244 |
+
},
|
245 |
+
"HTML": {
|
246 |
+
"properties": {
|
247 |
+
"label": "HTML Output",
|
248 |
+
"value": "<div>HTML content here</div>"
|
249 |
+
},
|
250 |
+
"code_snippet": 'gr.HTML(label="{label}", value="""{value}""")'
|
251 |
+
}
|
252 |
+
}
|
253 |
+
}
|
254 |
+
class ComponentManager:
|
255 |
+
def __init__(self):
|
256 |
+
self.categories = COMPONENT_CATEGORIES
|
257 |
+
self.templates = TEMPLATE_CATEGORIES
|
258 |
+
self.active_components = []
|
259 |
+
self.dependencies = set()
|
260 |
|
261 |
+
def get_component_info(self, category: str, component_type: str) -> Dict:
|
262 |
+
"""Get component information from registry"""
|
263 |
+
return self.categories[category][component_type]
|
|
|
|
|
|
|
264 |
|
265 |
+
def add_component(self, category: str, component_type: str, properties: Dict = None) -> Dict:
|
266 |
+
"""Add a component with specified properties"""
|
267 |
+
component_info = self.get_component_info(category, component_type)
|
268 |
+
component_id = f"{component_type}_{len(self.active_components)}"
|
|
|
|
|
|
|
|
|
|
|
269 |
|
270 |
+
new_component = {
|
271 |
+
"id": component_id,
|
272 |
+
"type": component_type,
|
273 |
+
"category": category,
|
274 |
+
"properties": properties or component_info["properties"].copy(),
|
275 |
+
"code_snippet": component_info["code_snippet"]
|
276 |
}
|
277 |
|
278 |
+
self.active_components.append(new_component)
|
279 |
+
return new_component
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
280 |
|
281 |
+
def remove_component(self, component_id: str):
|
282 |
+
"""Remove a component by ID"""
|
283 |
+
self.active_components = [c for c in self.active_components if c["id"] != component_id]
|
284 |
+
|
285 |
+
def update_component(self, component_id: str, properties: Dict):
|
286 |
+
"""Update component properties"""
|
287 |
+
for component in self.active_components:
|
288 |
+
if component["id"] == component_id:
|
289 |
+
component["properties"].update(properties)
|
290 |
+
break
|
|
|
|
|
291 |
|
292 |
+
class TemplateManager:
|
|
|
293 |
def __init__(self):
|
294 |
+
self.templates = TEMPLATE_CATEGORIES
|
295 |
+
self.component_manager = ComponentManager()
|
296 |
+
|
297 |
+
def load_template(self, category: str, template_name: str) -> Dict:
|
298 |
+
"""Load a template configuration"""
|
299 |
+
template = self.templates[category][template_name]
|
300 |
+
self.component_manager.active_components = []
|
301 |
+
self.component_manager.dependencies = set(template.get("dependencies", []))
|
302 |
+
|
303 |
+
# Add components from template
|
304 |
+
for component_config in template["components"]:
|
305 |
+
component_type = component_config.pop("type")
|
306 |
+
# Find category for component type
|
307 |
+
for cat, components in COMPONENT_CATEGORIES.items():
|
308 |
+
if component_type in components:
|
309 |
+
self.component_manager.add_component(cat, component_type, component_config)
|
310 |
+
break
|
311 |
+
|
312 |
+
return {
|
313 |
+
"layout": template.get("layout", "vertical"),
|
314 |
+
"css": template.get("css", ""),
|
315 |
+
"dependencies": list(self.component_manager.dependencies)
|
316 |
}
|
|
|
317 |
|
318 |
+
class CodeGenerator:
|
319 |
+
def __init__(self, template_manager: TemplateManager):
|
320 |
+
self.template_manager = template_manager
|
|
|
|
|
|
|
|
|
321 |
|
322 |
+
def generate_imports(self) -> str:
|
323 |
+
"""Generate import statements"""
|
324 |
+
imports = [
|
325 |
+
"import gradio as gr",
|
326 |
+
"import os",
|
327 |
+
"import logging",
|
328 |
+
"from pathlib import Path",
|
329 |
+
]
|
330 |
+
|
331 |
+
# Add template-specific imports
|
332 |
+
if self.template_manager.component_manager.dependencies:
|
333 |
+
imports.extend([f"import {dep}" for dep in self.template_manager.component_manager.dependencies])
|
|
|
|
|
|
|
|
|
|
|
334 |
|
335 |
+
return "\n".join(imports) + "\n\n"
|
336 |
+
|
337 |
+
def generate_component_code(self, component: Dict) -> str:
|
338 |
+
"""Generate code for a single component"""
|
339 |
+
try:
|
340 |
+
return component["code_snippet"].format(**component["properties"])
|
341 |
+
except KeyError as e:
|
342 |
+
logger.error(f"Missing property {e} for component {component['id']}")
|
343 |
+
return f"# Error generating code for {component['id']}"
|
344 |
+
|
345 |
+
def generate_layout_code(self, layout: str, components: List[Dict]) -> str:
|
346 |
+
"""Generate layout code"""
|
347 |
+
indent = " "
|
348 |
+
code = []
|
349 |
|
350 |
+
if layout == "horizontal":
|
351 |
+
code.append("with gr.Row():")
|
352 |
+
indent = " "
|
353 |
+
elif layout == "vertical":
|
354 |
+
code.append("with gr.Column():")
|
355 |
+
indent = " "
|
356 |
+
|
357 |
+
for component in components:
|
358 |
+
code.append(f"{indent}{self.generate_component_code(component)}")
|
359 |
+
|
360 |
+
return "\n".join(code)
|
361 |
+
|
362 |
+
def generate_full_code(self, app_name: str, template_config: Dict) -> str:
|
363 |
+
"""Generate complete application code"""
|
364 |
+
code_parts = [
|
365 |
+
self.generate_imports(),
|
366 |
+
"# Configure logging",
|
367 |
+
'logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s")',
|
368 |
+
'logger = logging.getLogger(__name__)\n',
|
369 |
+
]
|
370 |
+
|
371 |
+
# Add CSS if present
|
372 |
+
if template_config.get("css"):
|
373 |
+
code_parts.extend([
|
374 |
+
"# Custom CSS",
|
375 |
+
f'custom_css = """{template_config["css"]}"""',
|
376 |
+
""
|
377 |
+
])
|
378 |
+
|
379 |
+
# Start app definition
|
380 |
+
code_parts.extend([
|
381 |
+
f"def create_{app_name}():",
|
382 |
+
" with gr.Blocks(css=custom_css) as app:",
|
383 |
+
f' gr.Markdown("# {app_name}")\n'
|
384 |
+
])
|
385 |
+
|
386 |
+
# Add layout code
|
387 |
+
code_parts.append(self.generate_layout_code(
|
388 |
+
template_config["layout"],
|
389 |
+
self.template_manager.component_manager.active_components
|
390 |
+
))
|
391 |
+
|
392 |
+
# Add launch code
|
393 |
+
code_parts.extend([
|
394 |
+
"\n return app",
|
395 |
+
"\nif __name__ == '__main__':",
|
396 |
+
f" app = create_{app_name}()",
|
397 |
+
" app.launch(debug=True)"
|
398 |
+
])
|
399 |
+
|
400 |
+
return "\n".join(code_parts)
|
401 |
+
|
402 |
+
class AppBuilder:
|
403 |
+
def __init__(self):
|
404 |
+
self.template_manager = TemplateManager()
|
405 |
+
self.code_generator = CodeGenerator(self.template_manager)
|
406 |
+
self.current_app_name = ""
|
407 |
+
self.current_template = None
|
408 |
+
|
409 |
+
def create_interface(self):
|
410 |
+
with gr.Blocks(theme=gr.themes.Default()) as interface:
|
411 |
+
gr.Markdown("# 🚀 Advanced Gradio App Builder")
|
412 |
+
|
413 |
+
with gr.Tabs():
|
414 |
+
# Template Selection Tab
|
415 |
+
with gr.TabItem("📋 Templates"):
|
416 |
+
with gr.Row():
|
417 |
+
template_category = gr.Dropdown(
|
418 |
+
choices=list(TEMPLATE_CATEGORIES.keys()),
|
419 |
+
label="Category"
|
420 |
+
)
|
421 |
+
template_name = gr.Dropdown(
|
422 |
+
label="Template"
|
423 |
+
)
|
424 |
+
|
425 |
+
app_name = gr.Textbox(label="Application Name")
|
426 |
+
load_template_btn = gr.Button("Load Template", variant="primary")
|
427 |
+
template_info = gr.JSON(label="Template Information")
|
428 |
+
|
429 |
+
# Component Editor Tab
|
430 |
+
with gr.TabItem("🔧 Components"):
|
431 |
+
with gr.Row():
|
432 |
+
with gr.Column(scale=1):
|
433 |
+
component_category = gr.Dropdown(
|
434 |
+
choices=list(COMPONENT_CATEGORIES.keys()),
|
435 |
+
label="Component Category"
|
436 |
+
)
|
437 |
+
component_type = gr.Dropdown(
|
438 |
+
label="Component Type"
|
439 |
+
)
|
440 |
+
add_component_btn = gr.Button("Add Component")
|
441 |
+
|
442 |
+
with gr.Column(scale=2):
|
443 |
+
component_list = gr.JSON(label="Active Components")
|
444 |
+
component_properties = gr.JSON(label="Component Properties")
|
445 |
+
|
446 |
+
# Code Preview Tab
|
447 |
+
with gr.TabItem("💻 Code"):
|
448 |
+
generate_code_btn = gr.Button("Generate Code")
|
449 |
+
code_output = gr.Code(language="python")
|
450 |
+
|
451 |
+
# Preview Tab
|
452 |
+
with gr.TabItem("👁️ Preview"):
|
453 |
+
preview_btn = gr.Button("Update Preview")
|
454 |
+
preview_area = gr.HTML()
|
455 |
+
|
456 |
+
# Event handlers
|
457 |
+
def update_template_choices(category):
|
458 |
+
return gr.Dropdown(choices=list(TEMPLATE_CATEGORIES[category].keys()))
|
459 |
+
|
460 |
+
def update_component_choices(category):
|
461 |
+
return gr.Dropdown(choices=list(COMPONENT_CATEGORIES[category].keys()))
|
462 |
+
|
463 |
+
def load_template(category, template_name, app_name):
|
464 |
+
if not app_name:
|
465 |
+
return "Please enter an application name", None
|
466 |
+
|
467 |
+
self.current_app_name = app_name
|
468 |
+
template_config = self.template_manager.load_template(category, template_name)
|
469 |
+
self.current_template = template_config
|
470 |
+
return "Template loaded successfully", template_config
|
471 |
+
|
472 |
+
def add_component(category, component_type):
|
473 |
+
if not self.current_template:
|
474 |
+
return "Please load a template first", None
|
475 |
+
|
476 |
+
new_component = self.template_manager.component_manager.add_component(
|
477 |
+
category, component_type
|
478 |
)
|
479 |
+
return (
|
480 |
+
"Component added successfully",
|
481 |
+
self.template_manager.component_manager.active_components
|
|
|
|
|
|
|
|
|
|
|
482 |
)
|
483 |
+
|
484 |
+
def generate_code():
|
485 |
+
if not self.current_template or not self.current_app_name:
|
486 |
+
return "Please load a template first"
|
487 |
+
|
488 |
+
return self.code_generator.generate_full_code(
|
489 |
+
self.current_app_name,
|
490 |
+
self.current_template
|
491 |
+
)
|
492 |
+
|
493 |
+
# Connect events
|
494 |
+
template_category.change(
|
495 |
+
update_template_choices,
|
496 |
+
inputs=[template_category],
|
497 |
+
outputs=[template_name]
|
498 |
+
)
|
499 |
+
|
500 |
+
component_category.change(
|
501 |
+
update_component_choices,
|
502 |
+
inputs=[component_category],
|
503 |
+
outputs=[component_type]
|
504 |
+
)
|
505 |
+
|
506 |
+
load_template_btn.click(
|
507 |
+
load_template,
|
508 |
+
inputs=[template_category, template_name, app_name],
|
509 |
+
outputs=[template_info, component_list]
|
510 |
+
)
|
511 |
+
|
512 |
+
add_component_btn.click(
|
513 |
+
add_component,
|
514 |
+
inputs=[component_category, component_type],
|
515 |
+
outputs=[component_list, component_properties]
|
516 |
+
)
|
517 |
+
|
518 |
+
generate_code_btn.click(
|
519 |
+
generate_code,
|
520 |
+
outputs=[code_output]
|
521 |
+
)
|
522 |
+
|
523 |
+
return interface
|
524 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
525 |
if __name__ == "__main__":
|
526 |
+
builder = AppBuilder()
|
527 |
+
interface = builder.create_interface()
|
528 |
+
interface.launch(server_port=7860, share=True, debug=True)
|