File size: 12,147 Bytes
a35ba6f
 
 
 
 
 
 
 
 
 
 
ad6c034
e64f720
 
 
4b47449
eb37059
c4fc102
a35ba6f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3dce557
 
 
a35ba6f
3dce557
a35ba6f
 
 
 
 
3dce557
 
a35ba6f
 
 
 
3dce557
 
a35ba6f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e64f720
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a35ba6f
e64f720
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a35ba6f
 
 
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
import streamlit as st
from flask import Flask, jsonify, request
from flask_login import LoginManager, UserMixin, login_user, login_required, logout_user, current_user
from flask_sqlalchemy import SQLAlchemy
from werkzeug.security import generate_password_hash, check_password_hash
import pdb
import subprocess
import docker
from huggingface_hub import HfApi, create_repo
import importlib
import os
from huggingface_hub import HfApi, create_repo
from transformers import pipeline
codex_pipeline = pipeline("code-generation", model="EleutherAI/code-davinci-002")
import huggingface_cli

hf_api = HfApi()

# Initialize Flask app
app = Flask(__name__)
app.config['SECRET_KEY'] = 'your-secret-key'
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///users.db'
db = SQLAlchemy(app)
login_manager = LoginManager()
login_manager.init_app(app)

# User and Project models (as defined earlier)

class User(UserMixin, db.Model):
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(100), unique=True, nullable=False)
    password_hash = db.Column(db.String(100), nullable=False)
    projects = db.relationship('Project', backref='user', lazy=True)

class Project(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(100), nullable=False)
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)

@login_manager.user_loader
def load_user(user_id):
    return User.query.get(int(user_id))

# Authentication routes (as defined earlier)

@app.route('/register', methods=['POST'])
def register():
    data = request.get_json()
    username = data.get('username')
    password = data.get('password')
    if User.query.filter_by(username=username).first():
        return jsonify({'message': 'Username already exists'}), 400
    new_user = User(username=username, password_hash=generate_password_hash(password))
    db.session.add(new_user)
    db.session.commit()
    return jsonify({'message': 'User registered successfully'}), 201

@app.route('/login', methods=['POST'])
def login():
    data = request.get_json()
    username = data.get('username')
    password = data.get('password')
    user = User.query.filter_by(username=username).first()
    if user and check_password_hash(user.password_hash, password):
        login_user(user)
        return jsonify({'message': 'Logged in successfully'}), 200
    return jsonify({'message': 'Invalid username or password'}), 401

@app.route('/logout')
@login_required
def logout():
    logout_user()
    return jsonify({'message': 'Logged out successfully'}), 200

@app.route('/create_project', methods=['POST'])
@login_required
def create_project():
    data = request.get_json()
    project_name = data.get('project_name')
    new_project = Project(name=project_name, user_id=current_user.id)
    db.session.add(new_project)
    db.session.commit()
    return jsonify({'message': 'Project created successfully'}), 201

@app.route('/get_projects')
@login_required
def get_projects():
    projects = Project.query.filter_by(user_id=current_user.id).all()
    return jsonify({'projects': [project.name for project in projects]}), 200

# Plugin system
class PluginManager:
    def __init__(self, plugin_dir):
        self.plugin_dir = plugin_dir
        self.plugins = {}

    def load_plugins(self):
        for filename in os.listdir(self.plugin_dir):
            if filename.endswith('.py'):
                module_name = filename[:-3]
                spec = importlib.util.spec_from_file_location(module_name, os.path.join(self.plugin_dir, filename))
                module = importlib.util.module_from_spec(spec)
                spec.loader.exec_module(module)
                if hasattr(module, 'register_plugin'):
                    plugin = module.register_plugin()
                    self.plugins[plugin.name] = plugin

    def get_plugin(self, name):
        return self.plugins.get(name)

    def list_plugins(self):
        return list(self.plugins.keys())

# Example plugin
# save this as a .py file in your plugin directory
def register_plugin():
    return ExamplePlugin()

class ExamplePlugin:
    name = "example_plugin"

    def run(self, input_data):
        return f"Plugin processed: {input_data}"

plugin_manager = PluginManager('./plugins')
plugin_manager.load_plugins()

def main():
    st.sidebar.title("AI-Guided Development")
    app_mode = st.sidebar.selectbox("Choose the app mode", 
                                    ["Home", "Login/Register", "File Explorer", "Code Editor", "Terminal", 
                                     "Build & Deploy", "AI Assistant", "Plugins"])

    # AI Guide Toggle
    ai_guide_level = st.sidebar.radio("AI Guide Level", ["Full Assistance", "Partial Assistance", "No Assistance"])

    if app_mode == "Home":
        st.title("Welcome to AI-Guided Development")
        st.write("Select a mode from the sidebar to get started.")

    elif app_mode == "Login/Register":
        login_register_page()

    elif app_mode == "File Explorer":
        file_explorer_page()

    elif app_mode == "Code Editor":
        code_editor_page()

    elif app_mode == "Terminal":
        terminal_page()

    elif app_mode == "Build & Deploy":
        build_and_deploy_page()

    elif app_mode == "AI Assistant":
        ai_assistant_page()

    elif app_mode == "Plugins":
        plugins_page()

@login_required
def file_explorer_page():
    st.header("File Explorer")
    # File explorer code (as before)

@login_required
def code_editor_page():
    st.header("Code Editor")
    # Code editor with Monaco integration
    st.components.v1.html(
        """
        <div id="monaco-editor" style="width:800px;height:600px;border:1px solid grey"></div>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/monaco-editor/0.20.0/min/vs/loader.min.js"></script>
        <script>
            require.config({ paths: { 'vs': 'https://cdnjs.cloudflare.com/ajax/libs/monaco-editor/0.20.0/min/vs' }});
            require(['vs/editor/editor.main'], function() {
                var editor = monaco.editor.create(document.getElementById('monaco-editor'), {
                    value: 'print("Hello, World!")',
                    language: 'python'
                });
            });
        </script>
        """,
        height=650,
    )
    
    if st.button("Run Code"):
        code = st.session_state.get('code', '')  # Get code from Monaco editor
        output = run_code(code)
        st.code(output)
    
    if st.button("Debug Code"):
        code = st.session_state.get('code', '')
        st.write("Debugging mode activated. Check your console for the debugger.")
        debug_code(code)

@login_required
def terminal_page():
    st.header("Terminal")
    # Terminal code (as before)

@login_required
def build_and_deploy_page():
    st.header("Build & Deploy")
    project_name = st.text_input("Enter project name:")
    
    if st.button("Build Docker Image"):
        image, logs = build_docker_image(project_name)
        st.write(f"Docker image built: {image.tags}")
    
    if st.button("Run Docker Container"):
        port = st.number_input("Enter port number:", value=8501)
        container = run_docker_container(project_name, port)
        st.write(f"Docker container running: {container.id}")
    
    if st.button("Deploy to Hugging Face Spaces"):
        token = st.text_input("Enter your Hugging Face token:", type="password")
        if token:
            repo_url = deploy_to_hf_spaces(project_name, token)
            st.write(f"Deployed to Hugging Face Spaces: {repo_url}")

@login_required
def ai_assistant_page():
    st.header("AI Assistant")
    # AI assistant code (as before)

@login_required
def plugins_page():
    st.header("Plugins")
    st.write("Available plugins:")
    for plugin_name in plugin_manager.list_plugins():
        st.write(f"- {plugin_name}")
    
    selected_plugin = st.selectbox("Select a plugin to run:", plugin_manager.list_plugins())
    input_data = st.text_input("Enter input for the plugin:")
    
    if st.button("Run Plugin"):
        plugin = plugin_manager.get_plugin(selected_plugin)
        if plugin:
            result = plugin.run(input_data)
            st.write(f"Plugin output: {result}")

def login_register_page():
    st.header("Login/Register")
    action = st.radio("Choose action:", ["Login", "Register"])
    
    username = st.text_input("Username:")
    password = st.text_input("Password:", type="password")
    
    if action == "Login":
        if st.button("Login"):
            user = User.query.filter_by(username=username).first()
            if user and check_password_hash(user.password_hash, password):
                login_user(user)
                st.success("Logged in successfully!")
            else:
                st.error("Invalid username or password")
    else:
        if st.button("Register"):
            if User.query.filter_by(username=username).first():
                st.error("Username already exists")
            else:
                new_user = User(username=username, password_hash=generate_password_hash(password))
                db.session.add(new_user)
                db.session.commit()
                st.success("User registered successfully!")

def debug_code(code):
    try:
        pdb.run(code)
    except Exception as e:
        return str(e)

def run_code(code):
    try:
        result = subprocess.run(['python', '-c', code], capture_output=True, text=True, timeout=10)
        return result.stdout
    except subprocess.TimeoutExpired:
        return "Code execution timed out"
    except Exception as e:
        return str(e)

def build_docker_image(project_name):
    client = docker.from_env()
    image, build_logs = client.images.build(path=".", tag=project_name)
    return image, build_logs

def run_docker_container(image_name, port):
    client = docker.from_env()
    container = client.containers.run(image_name, detach=True, ports={f'{port}/tcp': port})
    return container

def generate_app(user_idea, project_name):
    # Extract key information from the user idea
    summary = nlp_pipeline(user_idea, max_length=50, min_length=10)[0]["summary_text"]

    # Create project directory if it doesn't exist
    project_path = create_project(project_name)

    # Generate code using Codex
    prompt = f"Create a simple Streamlit app for the project named '{project_name}'. The app should display the following summary: '{summary}'."
    generated_code = codex_pipeline(prompt)[0]['generated_text']

    # Save the generated code to a file in the project directory
    with open(os.path.join(project_path, "app.py"), "w") as f:
        f.write(generated_code)

    # Deploy the app to Hugging Face Spaces
    deploy_app_to_hf_spaces(project_name, token, generated_code)

    return generated_code, project_path

def deploy_app_to_hf_spaces(project_name, token, generated_code):
    repo_name = f"hf-{project_name}"
    repo_id = hf_api.changelog.get_repo_id(repo_name)

    if not repo_id:
        create_repo = huggingface_cli.create_repo(repo_name, "public", "Streamlit App", token)
        repo_id = create_repo["repo_id"]

    # Save the generated code to a temporary file

    temp_file = "temp_code.py"
    with open(temp_file, "w") as f:
        f.write(generated_code)

    # Upload the file to Hugging Face Spaces
    api.upload_files(repo_id, [temp_file], token)

    # Delete the temporary file
    os.remove(temp_file)

    def launch_chatapp(project_path):
    # ... (previous code)

    if st.button("Build & Deploy"):
        token = st.text_input("Enter your Hugging Face token:", type="password")
        generated_code, project_path = generate_app(st.session_state["user_idea"], project_name)
        st.write(f"Generated code: {generated_code}")
        if token:
            deploy_app_to_hf_spaces(project_name, token, generated_code)
            st.success("App deployed to Hugging Face Spaces!")
        else:
            st.error("Please enter a Hugging Face token to deploy the app.")
    
if __name__ == "__main__":
    db.create_all()  # Create the database tables if they don't exist
    main()