File size: 12,060 Bytes
2582b22
19627c4
2582b22
 
 
 
 
 
7e568ab
2582b22
 
 
19627c4
7e568ab
2582b22
 
 
 
19627c4
2582b22
19627c4
2582b22
 
19627c4
 
 
 
 
2582b22
 
 
19627c4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2582b22
 
 
afe9aee
2582b22
19627c4
2582b22
19627c4
7e568ab
afe9aee
 
 
 
 
19627c4
 
8ca88cc
afe9aee
 
 
 
 
19627c4
afe9aee
 
 
 
19627c4
 
 
 
 
afe9aee
 
 
 
 
 
19627c4
afe9aee
 
19627c4
afe9aee
 
 
19627c4
 
 
 
 
 
 
 
 
 
 
 
afe9aee
7e568ab
19627c4
 
 
afe9aee
 
 
 
 
 
2582b22
19627c4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2582b22
19627c4
2582b22
19627c4
0717322
 
 
 
 
19627c4
afe9aee
19627c4
 
 
0717322
19627c4
 
 
 
 
 
 
 
 
0717322
19627c4
 
0717322
19627c4
7e568ab
19627c4
 
 
 
 
 
7e568ab
19627c4
0717322
19627c4
0717322
19627c4
 
7e568ab
19627c4
 
0717322
19627c4
 
 
 
0717322
19627c4
 
2582b22
19627c4
 
 
 
8ca88cc
19627c4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8ca88cc
19627c4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
afe9aee
 
19627c4
 
 
 
 
 
 
 
afe9aee
19627c4
 
afe9aee
19627c4
afe9aee
19627c4
 
afe9aee
 
19627c4
 
 
 
 
 
 
afe9aee
 
19627c4
 
afe9aee
19627c4
afe9aee
19627c4
c161064
19627c4
 
 
 
c161064
afe9aee
0717322
19627c4
 
 
c161064
 
 
afe9aee
c161064
afe9aee
c161064
 
19627c4
 
 
 
8ca88cc
19627c4
 
 
 
 
 
 
c161064
2582b22
19627c4
2582b22
19627c4
 
8ca88cc
2582b22
19627c4
2582b22
19627c4
 
2582b22
19627c4
7e568ab
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
import threading
import time
import gradio as gr
import logging
import json
import re
import torch
import tempfile
import os
from pathlib import Path
from typing import Dict, List, Tuple, Optional, Any, Union
from dataclasses import dataclass, field
from enum import Enum
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
from sentence_transformers import SentenceTransformer
import faiss
import numpy as np
from PIL import Image
import black

# Configure logging
logging.basicConfig(
    level=logging.INFO,
    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
    handlers=[
        logging.StreamHandler(),
        logging.FileHandler('gradio_builder.log')
    ]
)
logger = logging.getLogger(__name__)

# Configuration
@dataclass
class Config:
    port: int = 7860
    debug: bool = False
    share: bool = False
    model_name: str = "gpt2"
    embedding_model: str = "all-mpnet-base-v2"
    theme: str = "default"
    max_code_length: int = 5000
    
    @classmethod
    def from_file(cls, path: str) -> "Config":
        try:
            with open(path) as f:
                return cls(**json.load(f))
        except Exception as e:
            logger.warning(f"Failed to load config from {path}: {e}. Using defaults.")
            return cls()

# Constants
CONFIG_PATH = Path("config.json")
MODEL_CACHE_DIR = Path("model_cache")
TEMPLATE_DIR = Path("templates")
TEMP_DIR = Path("temp")
DATABASE_PATH = Path("code_database.json")

# Ensure directories exist
for directory in [MODEL_CACHE_DIR, TEMPLATE_DIR, TEMP_DIR]:
    directory.mkdir(exist_ok=True, parents=True)

@dataclass
class Template:
    code: str
    description: str
    components: List[str] = field(default_factory=list)
    created_at: str = field(default_factory=lambda: time.strftime("%Y-%m-%d %H:%M:%S"))
    tags: List[str] = field(default_factory=list)

class TemplateManager:
    def __init__(self, template_dir: Path):
        self.template_dir = template_dir
        self.templates: Dict[str, Template] = {}

    def load_templates(self) -> None:
        for file_path in self.template_dir.glob("*.json"):
            try:
                with open(file_path, 'r') as f:
                    template_data = json.load(f)
                    template = Template(**template_data)
                    self.templates[template_data['description']] = template
                logger.info(f"Loaded template: {file_path.stem}")
            except Exception as e:
                logger.error(f"Error loading template from {file_path}: {e}")

    def save_template(self, name: str, template: Template) -> bool:
        file_path = self.template_dir / f"{name}.json"
        try:
            with open(file_path, 'w') as f:
                json.dump(dataclasses.asdict(template), f, indent=2)
            self.templates[name] = template
            return True
        except Exception as e:
            logger.error(f"Error saving template to {file_path}: {e}")
            return False

    def get_template(self, name: str) -> Optional[str]:
        template = self.templates.get(name)
        return template.code if template else ""

    def delete_template(self, name: str) -> bool:
        file_path = self.template_dir / f"{name}.json"
        try:
            file_path.unlink()
            self.templates.pop(name, None)
            return True
        except Exception as e:
            logger.error(f"Error deleting template {name}: {e}")
            return False

class RAGSystem:
    def __init__(self, config: Config):
        self.config = config
        self.device = "cuda" if torch.cuda.is_available() else "cpu"
        self.embedding_model = None
        self.code_embeddings = None
        self.index = None
        self.database = {'codes': [], 'embeddings': []}
        self.pipe = None

        try:
            self.tokenizer = AutoTokenizer.from_pretrained(
                config.model_name, 
                cache_dir=MODEL_CACHE_DIR
            )
            self.model = AutoModelForCausalLM.from_pretrained(
                config.model_name, 
                cache_dir=MODEL_CACHE_DIR
            ).to(self.device)
            self.pipe = pipeline(
                "text-generation", 
                model=self.model, 
                tokenizer=self.tokenizer, 
                device=self.device
            )
            self.embedding_model = SentenceTransformer(config.embedding_model)
            self.load_database()
            logger.info("RAG system initialized successfully.")
        except Exception as e:
            logger.error(f"Error initializing RAG system: {e}")

    def load_database(self) -> None:
        if DATABASE_PATH.exists():
            try:
                with open(DATABASE_PATH, 'r', encoding='utf-8') as f:
                    self.database = json.load(f)
                    self.code_embeddings = np.array(self.database['embeddings'])
                logger.info(f"Loaded {len(self.database['codes'])} code snippets from database.")
                self._build_index()
            except Exception as e:
                logger.error(f"Error loading database: {e}")
                self._initialize_empty_database()
        else:
            logger.info("Creating new database.")
            self._initialize_empty_database()

    def _initialize_empty_database(self) -> None:
        self.database = {'codes': [], 'embeddings': []}
        self.code_embeddings = np.array([])
        self._build_index()

    def _build_index(self) -> None:
        if len(self.code_embeddings) > 0 and self.embedding_model:
            dim = self.code_embeddings.shape[1]
            self.index = faiss.IndexFlatL2(dim)
            self.index.add(self.code_embeddings)
            logger.info(f"Built FAISS index with {len(self.code_embeddings)} vectors")

class GradioInterface:
    def __init__(self, config: Config):
        self.config = config
        self.template_manager = TemplateManager(TEMPLATE_DIR)
        self.template_manager.load_templates()
        self.rag_system = RAGSystem(config)

    def format_code(self, code: str) -> str:
        try:
            return black.format_str(code, mode=black.FileMode())
        except Exception as e:
            logger.warning(f"Code formatting failed: {e}")
            return code

    def _extract_components(self, code: str) -> List[str]:
        components = []
        try:
            function_matches = re.findall(r'def (\w+)\(', code)
            class_matches = re.findall(r'class (\w+):', code)
            components.extend(function_matches)
            components.extend(class_matches)
        except Exception as e:
            logger.error(f"Error extracting components: {e}")
        return list(set(components))

    def launch(self) -> None:
        with gr.Blocks(theme=gr.themes.Base()) as interface:
            # Custom CSS
            gr.Markdown(
                """
                <style>
                .header {
                    text-align: center;
                    background-color: #f0f0f0;
                    padding: 20px;
                    border-radius: 10px;
                    margin-bottom: 20px;
                }
                .container {
                    max-width: 1200px;
                    margin: 0 auto;
                }
                </style>
                <div class="header">
                <h1>Code Generation Interface</h1>
                <p>Generate and manage code templates easily</p>
                </div>
                """
            )

            with gr.Row():
                with gr.Column(scale=2):
                    description_input = gr.Textbox(
                        label="Description",
                        placeholder="Enter a description for the code you want to generate",
                        lines=3
                    )
                    template_choice = gr.Dropdown(
                        label="Select Template",
                        choices=list(self.template_manager.templates.keys()),
                        value=None
                    )

            with gr.Row():
                generate_button = gr.Button("Generate Code", variant="primary")
                save_button = gr.Button("Save as Template", variant="secondary")
                clear_button = gr.Button("Clear", variant="stop")

            with gr.Row():
                code_output = gr.Code(
                    label="Generated Code",
                    language="python",
                    interactive=True
                )

            status_output = gr.Textbox(
                label="Status",
                interactive=False
            )

            def generate_code_wrapper(description: str, template_choice: str) -> Tuple[str, str]:
                if not description.strip():
                    return "", "Please provide a description"
                
                try:
                    template_code = self.template_manager.get_template(template_choice) if template_choice else ""
                    generated_code = self.rag_system.generate_code(description, template_code)
                    formatted_code = self.format_code(generated_code)
                    
                    if not formatted_code:
                        return "", "Failed to generate code. Please try again."
                        
                    return formatted_code, "Code generated successfully."
                    
                except Exception as e:
                    logger.error(f"Error in code generation: {str(e)}")
                    return "", f"Error: {str(e)}"

            def save_template_wrapper(code: str, name: str, description: str) -> Tuple[str, str]:
                try:
                    if not name or not code:
                        return code, "Template name and code are required."

                    components = self._extract_components(code)
                    template = Template(
                        code=code,
                        description=name,
                        components=components,
                        tags=[t.strip() for t in description.split(',') if t.strip()]
                    )

                    if self.template_manager.save_template(name, template):
                        self.rag_system.add_to_database(code)
                        template_choice.choices = list(self.template_manager.templates.keys())
                        return code, f"Template '{name}' saved successfully."
                    else:
                        return code, "Failed to save template."
                except Exception as e:
                    return code, f"Error saving template: {e}"

            def clear_outputs() -> Tuple[str, str, str]:
                return "", "", ""

            # Event handlers
            generate_button.click(
                fn=generate_code_wrapper,
                inputs=[description_input, template_choice],
                outputs=[code_output, status_output],
                api_name="generate_code",
                show_progress=True
            )

            save_button.click(
                fn=save_template_wrapper,
                inputs=[code_output, template_choice, description_input],
                outputs=[code_output, status_output]
            )

            clear_button.click(
                fn=clear_outputs,
                inputs=[],
                outputs=[description_input, code_output, status_output]
            )

        # Launch the interface
        interface.launch(
            server_port=self.config.port,
            share=self.config.share,
            debug=self.config.debug
        )

def main():
    logger.info("=== Application Startup ===")
    try:
        config = Config.from_file(CONFIG_PATH)
        interface = GradioInterface(config)
        interface.launch()
    except Exception as e:
        logger.error(f"Application error: {e}")
        raise
    finally:
        logger.info("=== Application Shutdown ===")

if __name__ == "__main__":
    main()