File size: 5,007 Bytes
fae4179
 
b8b7a36
 
e3ae7cd
b8b7a36
e1c400c
b8b7a36
4781efb
b8b7a36
 
 
 
 
 
 
39a6462
b8b7a36
 
 
 
4781efb
 
e1c400c
 
 
 
 
 
 
 
4781efb
e1c400c
4781efb
2b45640
4781efb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4cbab65
4781efb
 
 
 
 
 
 
 
 
 
 
4cbab65
4781efb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4cbab65
4781efb
 
4cbab65
b2eac4f
4781efb
 
 
 
 
e1c400c
 
 
 
4781efb
 
e1c400c
 
4781efb
 
e3ae7cd
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
import os
import subprocess
import random
import json
from flask import Flask, render_template
from datetime import datetime
from gradio import Blocks
from safe_search import safe_search
from i_search import google, i_search as i_s
from agent import (
    ACTION_PROMPT,
    ADD_PROMPT,
    COMPRESS_HISTORY_PROMPT,
    LOG_PROMPT,
    LOG_RESPONSE,
    MODIFY_PROMPT,
    PREFIX,
    SEARCH_QUERY,
    READ_PROMPT,
    TASK_PROMPT,
    UNDERSTAND_TEST_RESULTS_PROMPT,
)
from utils import parse_action, parse_file_content, read_python_module_structure
from huggingface_hub import cached_download, hf_hub_url
from transformers import InferenceClient

app = Flask(__name__)

@app.route("/")
def index():
    return render_template("index.html")

class App(Blocks):
    def __init__(self):
        super().__init__()
        self.app_state = {"components": []}
        self.terminal_history = ""
        self.components_registry = {
            "Button": {
                "properties": {
                    "label": "Click Me",
                    "onclick": ""
                },
                "description": "A clickable button",
                "code_snippet": "gr.Button(value='{{label}}', variant='primary')"
            },
            "Text Input": {
                "properties": {
                    "value": "",
                    "placeholder": "Enter text"
                },
                "description": "A field for entering text",
                "code_snippet": "gr.Textbox(label='{{placeholder}}')"
            },
            "Image": {
                "properties": {
                    "src": "#",
                    "alt": "Image"
                },
                "description": "Displays an image",
                "code_snippet": "gr.Image(label='{{alt}}')"
            },
            "Dropdown": {
                "properties": {
                    "choices": ["Option 1", "Option 2"],
                    "value": ""
                },
                "description": "A dropdown menu for selecting options",
                "code_snippet": "gr.Dropdown(choices={{choices}}, label='Dropdown')"
            }
        }
        self.nlp_model_names = [
            "google/flan-t5-small",
            "Qwen/CodeQwen1.5-7B-Chat-GGUF",
            "bartowski/Codestral-22B-v0.1-GGUF",
            "bartowski/AutoCoder-GGUF"
        ]
        self.nlp_models = []
        self.initialize_nlp_models()

    def initialize_nlp_models(self):
        for nlp_model_name in self.nlp_model_names:
            try:
                cached_download(hf_hub_url(nlp_model_name, revision="main"))
                self.nlp_models.append(InferenceClient(nlp_model_name))
            except:
                self.nlp_models.append(None)

    def get_nlp_response(self, input_text, model_index):
        if self.nlp_models[model_index]:
            response = self.nlp_models[model_index].text_generation(input_text)
            return response.generated_text
        else:
            return "NLP model not available."

    class Component:
        def __init__(self, type, properties=None, id=None):
            self.id = id or random.randint(1000, 9999)
            self.type = type
            self.properties = properties or self.components_registry[type]["properties"].copy()

        def to_dict(self):
            return {
                "id": self.id,
                "type": self.type,
                "properties": self.properties,
            }

        def render(self):
            if self.type == "Dropdown":
                self.properties["choices"] = str(self.properties["choices"]).replace("[", "").replace("]", "").replace("'", "")
            return self.components_registry[self.type]["code_snippet"].format(**self.properties)

    def update_app_canvas(self):
        components_html = "".join([f"<div>Component ID: {component['id']}, Type: {component['type']}, Properties: {component['properties']}</div>" for component in self.app_state["components"]])
        return components_html

    def add_component(self, component_type):
        if component_type in self.components_registry:
            new_component = self.Component(component_type)
            self.app_state["components"].append(new_component.to_dict())
            return (
                self.update_app_canvas(),
                f"System: Added component: {component_type}\n",
            )
        else:
            return None, f"Error: Invalid component type: {component_type}\n"

    def run_terminal_command(self, command, history):
        output = ""
        try:
            process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
            output, error = process.communicate()
            if error:
                output = error
        except Exception as e:
            output = str(e)
        self.terminal_history += f"{command}\n{output.decode('utf-8')}\n"
        return self.terminal_history

if __name__ == "__main__":
    app.run(debug=True)