TheFinalBuddy / app.py
hotboxxgenn's picture
Upload 9 files
c0938f5 verified
import gradio as gr
from pathlib import Path
from core.directives import BuddyPersonality
from core.code_intel import analyze_code, detect_language
from core.project import list_project, read_file, write_file, export_project, scaffold_login_system
from utils.context import load_context, save_context
import json, shutil, os
CTX = load_context()
buddy = BuddyPersonality()
BASE = Path('scaffold_projects')
BASE.mkdir(exist_ok=True)
sample = BASE / 'welcome_project'
if not sample.exists():
(sample / 'hello.txt').parent.mkdir(parents=True, exist_ok=True)
(sample / 'hello.txt').write_text('Welcome to DeepSite Buddy Pro!\nUse the UI to generate projects, upload files, and edit them.')
def run_analysis(file):
try:
content = file.read().decode('utf-8', errors='ignore')
except Exception:
try:
with open(file.name, 'r', encoding='utf-8', errors='ignore') as f:
content = f.read()
except Exception as e:
return f'Could not read file: {e}'
lang = detect_language(file.name if hasattr(file,'name') else 'file')
res = analyze_code(file.name if hasattr(file,'name') else 'file', content)
return json.dumps({"language": lang, "analysis": res}, indent=2, ensure_ascii=False)
def cmd_make_login():
p = BASE / 'login_system'
if p.exists():
shutil.rmtree(p)
scaffold_login_system(p)
return f'Login system scaffold created at {p}. Use "Show Project Tree" to view.'
def show_tree(proj_name='welcome_project'):
p = BASE / proj_name
if not p.exists():
return f'Project {proj_name} does not exist.'
return list_project(p)
def read_proj_file(proj_name, rel_path):
p = BASE / proj_name / rel_path
content = read_file(p)
if content is None:
return f'File not found: {rel_path}'
return content
def save_proj_file(proj_name, rel_path, content):
p = BASE / proj_name / rel_path
write_file(p, content)
return f'Wrote {rel_path}'
def export_proj_zip(proj_name):
p = BASE / proj_name
if not p.exists():
return None
return export_project(p, f'{proj_name}_export')
with gr.Blocks(css='static/style.css', title='DeepSite Buddy Pro') as demo:
gr.Markdown('# DeepSite Buddy Pro 🌸☁️\nA dreamy AI developer buddy.')
with gr.Row():
with gr.Column(scale=3):
name_in = gr.Textbox(label='Set Buddy Name', placeholder=buddy.get_name())
set_name_btn = gr.Button('Set Name')
chat_in = gr.Textbox(label='Say something to Buddy', placeholder='e.g. make login system or analyze my file')
chat_btn = gr.Button('Run')
file_in = gr.File(label='Upload code file for analysis', file_count='single')
analysis_out = gr.Code(label='Analysis Output', language='json')
with gr.Row():
make_login = gr.Button('Make Login System (scaffold)')
show_tree_btn = gr.Button('Show Project Tree')
project_select = gr.Dropdown(choices=[p.name for p in BASE.iterdir() if p.is_dir()], value='welcome_project', label='Project')
export_btn = gr.Button('Export Project ZIP')
with gr.Column(scale=2):
gr.Markdown('### Project Explorer')
tree_out = gr.Textbox(label='Project Tree', lines=20)
gr.Markdown('### File Editor')
file_path = gr.Textbox(label='Relative path (e.g., frontend/index.html)', placeholder='hello.txt')
file_editor = gr.Code('', label='Editor', language='auto', lines=20)
save_file_btn = gr.Button('Save File')
preview_html = gr.HTML('<div style="padding:12px;background:rgba(255,255,255,0.6);border-radius:8px;">Live preview will show HTML content when you open an HTML file.</div>')
def _set_name(n):
if not n:
return buddy.get_name()
buddy.set_name(n)
return f'Buddy name set to {n}'
set_name_btn.click(_set_name, inputs=[name_in], outputs=[name_in])
def _run_chat(text, uploaded_file):
if uploaded_file:
return run_analysis(uploaded_file)
lower = (text or '').lower()
if any(k in lower for k in ('make login','login system','make a login')):
return cmd_make_login()
if any(k in lower for k in ('show project','project tree','show tree')):
return show_tree(project_select.value)
out = f"Hello, I'm {buddy.get_name()}! I can analyze files, scaffold projects, and export zips. You said: {text}"
buddy.record_conversation(text, out)
return out
chat_btn.click(_run_chat, inputs=[chat_in, file_in], outputs=[analysis_out])
make_login.click(lambda: cmd_make_login(), inputs=None, outputs=[analysis_out, tree_out])
show_tree_btn.click(lambda: show_tree(project_select.value), inputs=None, outputs=tree_out)
def _open_file(proj, rel):
return read_proj_file(proj, rel)
file_path.change(_open_file, inputs=[project_select, file_path], outputs=file_editor)
save_file_btn.click(lambda proj, rel, content: save_proj_file(proj, rel, content), inputs=[project_select, file_path, file_editor], outputs=analysis_out)
export_btn.click(lambda proj: export_proj_zip(proj), inputs=[project_select], outputs=gr.File(label='Download ZIP'))
demo.launch()