Spaces:
Sleeping
Sleeping
Update app.py
#6
by hotboxxgenn - opened
app.py
CHANGED
|
@@ -1,87 +1,141 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import os
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
from
|
| 6 |
-
|
| 7 |
-
|
|
|
|
| 8 |
|
| 9 |
-
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
-
def
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
if m["role"]=="user":
|
| 16 |
-
out += f"๐ค {m.get('name','User')}: {m['text']}\n"
|
| 17 |
-
else:
|
| 18 |
-
out += f"๐ {m.get('name','GenBuddy')}: {m['text']}\n"
|
| 19 |
-
return out
|
| 20 |
|
| 21 |
-
def
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
saved = save_uploaded_file(project, file)
|
| 29 |
-
if 'error' in saved:
|
| 30 |
-
reply = "Failed to save uploaded file: "+saved['error']
|
| 31 |
-
else:
|
| 32 |
-
content = read_file(project, saved['relpath'])
|
| 33 |
-
lang = detect_language(saved['relpath'], content)
|
| 34 |
-
analysis = analyze_code(content)
|
| 35 |
-
fixes = suggest_fixes(lang, content)
|
| 36 |
-
reply = f"Saved {saved['relpath']} to project {project}. Detected: {lang}. {analysis['summary']}"
|
| 37 |
-
if fixes:
|
| 38 |
-
reply += "\nSuggested fixes:\n" + "\n".join(fixes[:5])
|
| 39 |
-
memory.add_message(user_name, 'bot', reply, name=bot_name)
|
| 40 |
-
return reply, format_history(memory.get_conversation(user_name))
|
| 41 |
-
lower = (message or '').lower()
|
| 42 |
-
if 'analyze' in lower or 'fix' in lower or 'debug' in lower:
|
| 43 |
-
files = list_project(project)
|
| 44 |
-
summaries = []
|
| 45 |
-
for f in files:
|
| 46 |
-
content = read_file(project, f)
|
| 47 |
-
lang = detect_language(f, content)
|
| 48 |
-
a = analyze_code(content)
|
| 49 |
-
summaries.append(f"- {f} ({lang}): {a['summary']}")
|
| 50 |
-
reply = "Analyzed files:\n" + "\n".join(summaries[:20])
|
| 51 |
-
memory.add_message(user_name,'bot',reply,name=bot_name)
|
| 52 |
-
return reply, format_history(memory.get_conversation(user_name))
|
| 53 |
-
if 'zip' in lower or 'export' in lower:
|
| 54 |
-
out = build_zip(os.path.join('projects',project), f'/mnt/data/{project}.zip')
|
| 55 |
-
reply = f"Exported: {out}"
|
| 56 |
-
memory.add_message(user_name,'bot',reply,name=bot_name)
|
| 57 |
-
return reply, format_history(memory.get_conversation(user_name))
|
| 58 |
-
reply = f"{bot_name}: I can analyze code, manage projects, and export zips. Try uploading a file or saying 'analyze project'."
|
| 59 |
-
memory.add_message(user_name,'bot',reply,name=bot_name)
|
| 60 |
-
return reply, format_history(memory.get_conversation(user_name))
|
| 61 |
|
| 62 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
|
| 64 |
-
|
| 65 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
with gr.Row():
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
send.click(chat_endpoint, inputs=[message, user_name, bot_name, bot_gender, file_input, project], outputs=[out, history])
|
| 85 |
-
list_btn.click(lambda p: json.dumps(list_project(p), indent=2), inputs=[project], outputs=[files_view])
|
| 86 |
-
zip_btn.click(lambda p: build_zip(os.path.join('projects', p), f'/mnt/data/{p}.zip') or f'/mnt/data/{p}.zip', inputs=[project], outputs=[zip_out])
|
| 87 |
-
launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import os
|
| 3 |
+
import shutil
|
| 4 |
+
import zipfile
|
| 5 |
+
from datetime import datetime
|
| 6 |
+
import tempfile
|
| 7 |
+
import json
|
| 8 |
+
import re
|
| 9 |
|
| 10 |
+
# ------------------------
|
| 11 |
+
# Memory & Personality
|
| 12 |
+
# ------------------------
|
| 13 |
+
MEMORY_FILE = "user_memory.json"
|
| 14 |
+
if os.path.exists(MEMORY_FILE):
|
| 15 |
+
with open(MEMORY_FILE, "r") as f:
|
| 16 |
+
user_memory = json.load(f)
|
| 17 |
+
else:
|
| 18 |
+
user_memory = {}
|
| 19 |
|
| 20 |
+
def save_memory():
|
| 21 |
+
with open(MEMORY_FILE, "w") as f:
|
| 22 |
+
json.dump(user_memory, f)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
+
def set_user_preferences(name, gender):
|
| 25 |
+
user_memory["name"] = name or "GenBuddy"
|
| 26 |
+
user_memory["gender"] = gender or "female"
|
| 27 |
+
if "preferences" not in user_memory:
|
| 28 |
+
user_memory["preferences"] = {"tone":"friendly","verbosity":"medium"}
|
| 29 |
+
save_memory()
|
| 30 |
+
return f"Hello {user_memory['name']} ({user_memory['gender']})! Ready to build apps."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
+
def update_preferences(tone=None, verbosity=None):
|
| 33 |
+
prefs = user_memory.get("preferences", {})
|
| 34 |
+
if tone: prefs["tone"] = tone
|
| 35 |
+
if verbosity: prefs["verbosity"] = verbosity
|
| 36 |
+
user_memory["preferences"] = prefs
|
| 37 |
+
save_memory()
|
| 38 |
+
return f"Preferences updated: {prefs}"
|
| 39 |
|
| 40 |
+
# ------------------------
|
| 41 |
+
# Code Intelligence
|
| 42 |
+
# ------------------------
|
| 43 |
+
def analyze_code(file):
|
| 44 |
+
try:
|
| 45 |
+
content = file.read().decode("utf-8")
|
| 46 |
+
except:
|
| 47 |
+
return "โ ๏ธ Cannot read file, unsupported encoding."
|
| 48 |
+
|
| 49 |
+
# Basic static analysis (expandable)
|
| 50 |
+
errors = []
|
| 51 |
+
warnings = []
|
| 52 |
+
if "import" in content and "from" not in content:
|
| 53 |
+
warnings.append("Check imports: might be incomplete.")
|
| 54 |
+
if re.search(r"print\s*\(", content) is None:
|
| 55 |
+
warnings.append("No print/logging statements detected.")
|
| 56 |
+
|
| 57 |
+
return f"โ
Analysis complete for {file.name}.\nErrors: {errors if errors else 'None'}\nWarnings: {warnings if warnings else 'None'}"
|
| 58 |
+
|
| 59 |
+
# ------------------------
|
| 60 |
+
# Project Builder
|
| 61 |
+
# ------------------------
|
| 62 |
+
def build_app(command, uploaded_files=[]):
|
| 63 |
+
"""Full automated app builder workflow."""
|
| 64 |
+
timestamp = datetime.now().strftime("%Y%m%d%H%M%S")
|
| 65 |
+
project_dir = tempfile.mkdtemp(prefix=f"GenBuddy_{timestamp}_")
|
| 66 |
+
|
| 67 |
+
# Auto-create folder structure
|
| 68 |
+
frontend_dir = os.path.join(project_dir, "frontend")
|
| 69 |
+
backend_dir = os.path.join(project_dir, "backend")
|
| 70 |
+
os.makedirs(frontend_dir, exist_ok=True)
|
| 71 |
+
os.makedirs(backend_dir, exist_ok=True)
|
| 72 |
+
|
| 73 |
+
# Process uploaded files
|
| 74 |
+
for file in uploaded_files:
|
| 75 |
+
fname = os.path.join(project_dir, file.name)
|
| 76 |
+
with open(fname, "wb") as f:
|
| 77 |
+
f.write(file.read())
|
| 78 |
+
|
| 79 |
+
# Auto-generate basic frontend files
|
| 80 |
+
with open(os.path.join(frontend_dir, "index.html"), "w") as f:
|
| 81 |
+
f.write(f"<!DOCTYPE html>\n<html>\n<head>\n<title>GenBuddy App</title>\n")
|
| 82 |
+
f.write("</head>\n<body>\n")
|
| 83 |
+
f.write(f"<h1>App generated for command: {command}</h1>\n")
|
| 84 |
+
f.write("</body>\n</html>")
|
| 85 |
+
|
| 86 |
+
with open(os.path.join(frontend_dir, "styles.css"), "w") as f:
|
| 87 |
+
f.write("body { font-family: Arial, sans-serif; background: linear-gradient(to right, #8ec5ff, #d3b8ff, #ffb3d9); color: #333; padding: 20px; }")
|
| 88 |
+
|
| 89 |
+
with open(os.path.join(frontend_dir, "app.js"), "w") as f:
|
| 90 |
+
f.write("// Auto-generated JS stub\nconsole.log('App loaded');")
|
| 91 |
+
|
| 92 |
+
# Auto-generate backend stub
|
| 93 |
+
with open(os.path.join(backend_dir, "server.py"), "w") as f:
|
| 94 |
+
f.write("# Backend stub\nprint('Server running')\n")
|
| 95 |
+
|
| 96 |
+
# Zip the project
|
| 97 |
+
zip_path = os.path.join(tempfile.gettempdir(), f"{timestamp}_project.zip")
|
| 98 |
+
with zipfile.ZipFile(zip_path, "w") as zipf:
|
| 99 |
+
for root, dirs, files in os.walk(project_dir):
|
| 100 |
+
for file in files:
|
| 101 |
+
zipf.write(os.path.join(root, file),
|
| 102 |
+
os.path.relpath(os.path.join(root, file), project_dir))
|
| 103 |
+
|
| 104 |
+
# Clean up project folder after zipping
|
| 105 |
+
shutil.rmtree(project_dir)
|
| 106 |
+
|
| 107 |
+
return zip_path
|
| 108 |
+
|
| 109 |
+
# ------------------------
|
| 110 |
+
# Command Handler
|
| 111 |
+
# ------------------------
|
| 112 |
+
def handle_command(command, uploaded_files=[]):
|
| 113 |
+
if not command.strip():
|
| 114 |
+
return "โ ๏ธ Please enter a valid command."
|
| 115 |
+
zip_file = build_app(command, uploaded_files)
|
| 116 |
+
return f"โ
App built successfully! Download here:", zip_file
|
| 117 |
+
|
| 118 |
+
# ------------------------
|
| 119 |
+
# Gradio Interface
|
| 120 |
+
# ------------------------
|
| 121 |
+
with gr.Blocks(css="style.css"):
|
| 122 |
+
gr.Markdown("## ๐ธ GenBuddy โ Intelligent App-Building AI")
|
| 123 |
+
|
| 124 |
with gr.Row():
|
| 125 |
+
name_input = gr.Textbox(label="Buddy Name", placeholder="Choose a name...")
|
| 126 |
+
gender_input = gr.Dropdown(label="Buddy Gender", choices=["female","male"], value="female")
|
| 127 |
+
save_btn = gr.Button("Set Name & Gender")
|
| 128 |
+
status_box = gr.Textbox(label="Status", interactive=False)
|
| 129 |
+
|
| 130 |
+
save_btn.click(set_user_preferences, inputs=[name_input, gender_input], outputs=status_box)
|
| 131 |
+
|
| 132 |
+
with gr.Row():
|
| 133 |
+
cmd_input = gr.Textbox(label="Command / App Request", placeholder="e.g., 'Create login system with email + OTP'")
|
| 134 |
+
file_input = gr.File(label="Upload files (optional)", file_types=None, file_types_allow_multiple=True)
|
| 135 |
+
run_btn = gr.Button("Execute Command")
|
| 136 |
+
output_text = gr.Textbox(label="Output", interactive=False)
|
| 137 |
+
download_file = gr.File(label="Download ZIP", interactive=True)
|
| 138 |
+
|
| 139 |
+
run_btn.click(handle_command, inputs=[cmd_input, file_input], outputs=[output_text, download_file])
|
| 140 |
+
|
| 141 |
+
launch()
|
|
|
|
|
|
|
|
|
|
|
|