File size: 9,959 Bytes
2582b22
 
 
 
 
 
 
 
 
 
7e568ab
 
2582b22
 
 
 
7e568ab
2582b22
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7e568ab
 
2582b22
 
 
7e568ab
 
2582b22
 
 
 
 
 
 
 
 
 
7e568ab
2582b22
 
7e568ab
 
2582b22
7e568ab
 
 
 
 
 
2582b22
7e568ab
 
 
 
2582b22
 
7e568ab
 
 
2582b22
7e568ab
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2582b22
7e568ab
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2582b22
 
 
 
 
 
7e568ab
 
2582b22
7e568ab
 
 
 
2582b22
7e568ab
 
2582b22
7e568ab
 
2582b22
 
 
 
 
 
 
 
 
7e568ab
 
 
 
 
2582b22
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import threading
import time
import gradio as gr
import logging
import json
import re
import torch
import tempfile
import subprocess
import ast
import os
import dataclasses
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

# 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__)

# Constants
DEFAULT_PORT = 7860
MODEL_CACHE_DIR = Path("model_cache")
TEMPLATE_DIR = Path("templates")
TEMP_DIR = Path("temp")
DATABASE_PATH = Path("code_database.json") #Path for our simple database


# 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]
    metadata: Dict[str, Any] = field(default_factory=dict)
    version: str = "1.0"

class TemplateManager:
    # ... (TemplateManager remains the same) ...


class RAGSystem:
    def __init__(self, model_name: str = "gpt2", device: str = "cuda" if torch.cuda.is_available() else "cpu", embedding_model="all-mpnet-base-v2"):
        try:
            self.tokenizer = AutoTokenizer.from_pretrained(model_name)
            self.model = AutoModelForCausalLM.from_pretrained(model_name).to(device)
            self.device = device
            self.pipe = pipeline("text-generation", model=self.model, tokenizer=self.tokenizer, device=self.device)
            self.embedding_model = SentenceTransformer(embedding_model)
            self.load_database()
        except Exception as e:
            logger.error(f"Error loading language model or embedding model: {e}.  Falling back to placeholder generation.")
            self.pipe = None
            self.embedding_model = None
            self.code_embeddings = None


    def load_database(self):
      """Loads or creates the code database"""
      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("Loaded code database from file")
        except (json.JSONDecodeError, KeyError) as e:
          logger.error(f"Error loading code database: {e}.  Creating new database.")
          self.database = {'codes': [], 'embeddings': []}
          self.code_embeddings = np.array([])

      else:
        logger.info("Code database does not exist. Creating new database.")
        self.database = {'codes': [], 'embeddings': []}
        self.code_embeddings = np.array([])

      if self.embedding_model and len(self.database['codes']) != len(self.database['embeddings']):
        logger.warning("Mismatch between number of codes and embeddings, rebuilding embeddings")
        self.rebuild_embeddings()
      elif self.embedding_model is None:
        logger.warning("Embeddings are not supported in this context. ")
      #Index the embeddings for efficient searching
      if len(self.code_embeddings) > 0 and self.embedding_model:
        self.index = faiss.IndexFlatL2(self.code_embeddings.shape[1]) #L2 distance
        self.index.add(self.code_embeddings)

    def add_to_database(self, code: str):
        """Adds a code snippet to the database"""
        try:
          embedding = self.embedding_model.encode(code)
          self.database['codes'].append(code)
          self.database['embeddings'].append(embedding.tolist())
          self.code_embeddings = np.vstack((self.code_embeddings, embedding))
          self.index.add(np.array([embedding])) # update FAISS index
          self.save_database()
          logger.info(f"Added code snippet to database. Total size:{len(self.database['codes'])}")
        except Exception as e:
          logger.error(f"Error adding to database: {e}")


    def save_database(self):
      """Saves the database to a file"""
      try:
        with open(DATABASE_PATH, 'w', encoding='utf-8') as f:
          json.dump(self.database, f, indent=2)
        logger.info(f"Saved database to {DATABASE_PATH}")
      except Exception as e:
        logger.error(f"Error saving database: {e}")

    def rebuild_embeddings(self):
      """rebuilds embeddings from the codes"""
      try:
        embeddings = self.embedding_model.encode(self.database['codes'])
        self.code_embeddings = embeddings
        self.database['embeddings'] = embeddings.tolist()
        self.index = faiss.IndexFlatL2(embeddings.shape[1]) #L2 distance
        self.index.add(embeddings)
        self.save_database()
        logger.info("Rebuilt and saved embeddings to the database")
      except Exception as e:
        logger.error(f"Error rebuilding embeddings: {e}")


    def retrieve_similar_code(self, description: str, top_k: int = 3) -> List[str]:
      """Retrieves similar code snippets from the database"""
      if self.embedding_model is None:
        return []
      try:
        embedding = self.embedding_model.encode(description)
        D, I = self.index.search(np.array([embedding]), top_k)
        return [self.database['codes'][i] for i in I[0]]
      except Exception as e:
        logger.error(f"Error retrieving similar code: {e}")
        return []

    def generate_code(self, description: str, template_code: str) -> str:
        retrieved_codes = self.retrieve_similar_code(description)
        prompt = f"Description: {description}\nRetrieved Code Snippets:\n{''.join([f'```python\n{code}\n```\n' for code in retrieved_codes])}\nTemplate:\n```python\n{template_code}\n```\nGenerated Code:\n```python\n"
        if self.pipe:
            try:
                generated_text = self.pipe(prompt, max_length=500, num_return_sequences=1)[0]['generated_text']
                generated_code = generated_text.split("Generated Code:")[1].strip().split('```')[0]
                return generated_code
            except Exception as e:
                logger.error(f"Error generating code with language model: {e}. Returning template code.")
                return template_code
        else:
            return f"# Placeholder code generation. Description: {description}\n{template_code}"

    def generate_interface(self, screenshot: Optional[Image.Image], description: str) -> str:
        retrieved_codes = self.retrieve_similar_code(description)
        prompt = f"Create a Gradio interface based on this description: {description}\nRetrieved Code Snippets:\n{''.join([f'```python\n{code}\n```\n' for code in retrieved_codes])}"
        if screenshot:
            prompt += "\nThe interface should resemble the provided screenshot."
        prompt += "\n```python\n"
        if self.pipe:
            try:
                generated_text = self.pipe(prompt, max_length=500, num_return_sequences=1)[0]['generated_text']
                generated_code = generated_text.split("```")[1].strip()
                return generated_code
            except Exception as e:
                logger.error(f"Error generating interface with language model: {e}. Returning placeholder.")
                return "import gradio as gr\n\ndemo = gr.Interface(fn=lambda x:x, inputs='text', outputs='text')\ndemo.launch()"
        else:
            return "import gradio as gr\n\ndemo = gr.Interface(fn=lambda x:x, inputs='text', outputs='text')\ndemo.launch()"

class PreviewManager:
    # ... (PreviewManager remains largely the same) ...


class GradioInterface:
    def __init__(self):
        self.template_manager = TemplateManager(TEMPLATE_DIR)
        self.template_manager.load_templates()
        self.current_code = ""
        self.rag_system = RAGSystem()
        self.preview_manager = PreviewManager()

    # ... (other GradioInterface methods remain largely the same,  but you may want to improve error handling) ...

    def _save_as_template(self, code: str, name: str, description: str) -> Tuple[List[str], str]:
        """Save current code as template and add to database"""
        try:
            components = self._extract_components(code)
            template = Template(code=code, description=description, components=components)
            if self.template_manager.save_template(name, template):
              self.rag_system.add_to_database(code) #add code to the database
              return self._get_template_choices(), f"✅ Template saved as {name}"
            else:
                raise Exception("Failed to save template")
        except Exception as e:
            error_msg = f"❌ Error saving template: {str(e)}"
            logger.error(error_msg)
            return self._get_template_choices(), error_msg


    def launch(self, **kwargs):
        with gr.Blocks() as interface:
            # ... (Interface remains largely the same) ...

        interface.launch(**kwargs)


def main():
    # 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__)
    logger.info("=== Application Startup ===")
    
    try:
        # Initialize and launch interface
        interface = GradioInterface()
        interface.launch(
            server_port=DEFAULT_PORT,
            share=False,
            debug=True
        )
    except Exception as e:
        logger.error(f"Application error: {e}")
        raise
    finally:
        logger.info("=== Application Shutdown ===")

if __name__ == "__main__":
    main()