Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -192,29 +192,81 @@ class GradioInterface:
|
|
| 192 |
self.rag_system = RAGSystem()
|
| 193 |
self.preview_manager = PreviewManager()
|
| 194 |
|
| 195 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 196 |
|
| 197 |
-
def
|
| 198 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 199 |
try:
|
| 200 |
components = self._extract_components(code)
|
| 201 |
template = Template(code=code, description=description, components=components)
|
| 202 |
if self.template_manager.save_template(name, template):
|
| 203 |
-
|
| 204 |
-
|
| 205 |
else:
|
| 206 |
-
|
| 207 |
except Exception as e:
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
return self._get_template_choices(), error_msg
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
def launch(self, **kwargs):
|
| 214 |
-
with gr.Blocks() as interface:
|
| 215 |
-
# ... (Interface remains largely the same) ...
|
| 216 |
-
|
| 217 |
-
interface.launch(**kwargs)
|
| 218 |
|
| 219 |
|
| 220 |
def main():
|
|
|
|
| 192 |
self.rag_system = RAGSystem()
|
| 193 |
self.preview_manager = PreviewManager()
|
| 194 |
|
| 195 |
+
def _extract_components(self, code: str) -> List[str]:
|
| 196 |
+
"""Extract components from the code."""
|
| 197 |
+
# This logic should analyze the code and extract components.
|
| 198 |
+
# For example, you might look for function definitions, classes, etc.
|
| 199 |
+
components = []
|
| 200 |
+
# Simple regex to find function definitions
|
| 201 |
+
function_matches = re.findall(r'def (\w+)', code)
|
| 202 |
+
components.extend(function_matches)
|
| 203 |
+
|
| 204 |
+
# Simple regex to find class definitions
|
| 205 |
+
class_matches = re.findall(r'class (\w+)', code)
|
| 206 |
+
components.extend(class_matches)
|
| 207 |
+
|
| 208 |
+
# You can add more sophisticated logic here as needed
|
| 209 |
+
return components
|
| 210 |
+
|
| 211 |
+
def _get_template_choices(self) -> List[str]:
|
| 212 |
+
"""Get available template choices."""
|
| 213 |
+
return list(self.template_manager.templates.keys())
|
| 214 |
|
| 215 |
+
def launch(self, **kwargs):
|
| 216 |
+
with gr.Blocks() as interface:
|
| 217 |
+
gr.Markdown("## Code Generation Interface")
|
| 218 |
+
description_input = gr.Textbox(label="Description", placeholder="Enter a description for the code you want to generate.")
|
| 219 |
+
code_output = gr.Textbox(label="Generated Code", interactive=False)
|
| 220 |
+
generate_button = gr.Button("Generate Code")
|
| 221 |
+
template_choice = gr.Dropdown(label="Select Template", choices=self._get_template_choices(), value=None)
|
| 222 |
+
save_button = gr.Button("Save as Template")
|
| 223 |
+
|
| 224 |
+
# Generate code button action
|
| 225 |
+
generate_button.click(
|
| 226 |
+
fn=self.generate_code,
|
| 227 |
+
inputs=description_input,
|
| 228 |
+
outputs=code_output
|
| 229 |
+
)
|
| 230 |
+
|
| 231 |
+
# Save template button action
|
| 232 |
+
save_button.click(
|
| 233 |
+
fn=self.save_template,
|
| 234 |
+
inputs=[code_output, template_choice, description_input],
|
| 235 |
+
outputs=code_output
|
| 236 |
+
)
|
| 237 |
+
|
| 238 |
+
# Additional UI elements can be added here
|
| 239 |
+
gr.Markdown("### Preview")
|
| 240 |
+
preview_output = gr.Textbox(label="Preview", interactive=False)
|
| 241 |
+
self.preview_manager.update_preview(code_output) # Update preview with generated code
|
| 242 |
+
|
| 243 |
+
# Update preview when code is generated
|
| 244 |
+
generate_button.click(
|
| 245 |
+
fn=lambda code: self.preview_manager.update_preview(code),
|
| 246 |
+
inputs=code_output,
|
| 247 |
+
outputs=preview_output
|
| 248 |
+
)
|
| 249 |
+
|
| 250 |
+
interface.launch(**kwargs)
|
| 251 |
+
|
| 252 |
+
def generate_code(self, description: str) -> str:
|
| 253 |
+
"""Generate code based on the description."""
|
| 254 |
+
template_code = "" # Placeholder for template code
|
| 255 |
+
return self.rag_system.generate_code(description, template_code)
|
| 256 |
+
|
| 257 |
+
def save_template(self, code: str, name: str, description: str) -> str:
|
| 258 |
+
"""Save the generated code as a template."""
|
| 259 |
try:
|
| 260 |
components = self._extract_components(code)
|
| 261 |
template = Template(code=code, description=description, components=components)
|
| 262 |
if self.template_manager.save_template(name, template):
|
| 263 |
+
self.rag_system.add_to_database(code) # Add code to the database
|
| 264 |
+
return f"✅ Template '{name}' saved successfully."
|
| 265 |
else:
|
| 266 |
+
return "❌ Failed to save template."
|
| 267 |
except Exception as e:
|
| 268 |
+
logger.error(f"Error saving template: {e}")
|
| 269 |
+
return f"❌ Error saving template: {str(e)}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 270 |
|
| 271 |
|
| 272 |
def main():
|