Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import os, json, datetime | |
| from core.directives import CoreDirectives | |
| from core.memory import BuddyMemory | |
| from backend.file_manager import ensure_project, list_project, read_file, save_file, save_uploaded_file | |
| from exporters.zip_builder import build_zip | |
| from utils.code_tools import detect_language, analyze_code, suggest_fixes | |
| directives = CoreDirectives() | |
| memory = BuddyMemory() | |
| def format_history(history): | |
| out = "" | |
| for m in history[-12:]: | |
| if m["role"]=="user": | |
| out += f"π€ {m.get('name','User')}: {m['text']}\n" | |
| else: | |
| out += f"π {m.get('name','GenBuddy')}: {m['text']}\n" | |
| return out | |
| def chat_endpoint(message, user_name, bot_name, bot_gender, file=None, project='default'): | |
| user_name = user_name or 'You' | |
| bot_name = bot_name or 'GenBuddy' | |
| memory.set_pref(user_name, 'bot_name', bot_name) | |
| memory.set_pref(user_name, 'bot_gender', bot_gender) | |
| memory.add_message(user_name, 'user', message) | |
| if file is not None: | |
| saved = save_uploaded_file(project, file) | |
| if 'error' in saved: | |
| reply = "Failed to save uploaded file: "+saved['error'] | |
| else: | |
| content = read_file(project, saved['relpath']) | |
| lang = detect_language(saved['relpath'], content) | |
| analysis = analyze_code(content) | |
| fixes = suggest_fixes(lang, content) | |
| reply = f"Saved {saved['relpath']} to project {project}. Detected: {lang}. {analysis['summary']}" | |
| if fixes: | |
| reply += "\nSuggested fixes:\n" + "\n".join(fixes[:5]) | |
| memory.add_message(user_name, 'bot', reply, name=bot_name) | |
| return reply, format_history(memory.get_conversation(user_name)) | |
| lower = (message or '').lower() | |
| if 'analyze' in lower or 'fix' in lower or 'debug' in lower: | |
| files = list_project(project) | |
| summaries = [] | |
| for f in files: | |
| content = read_file(project, f) | |
| lang = detect_language(f, content) | |
| a = analyze_code(content) | |
| summaries.append(f"- {f} ({lang}): {a['summary']}") | |
| reply = "Analyzed files:\n" + "\n".join(summaries[:20]) | |
| memory.add_message(user_name,'bot',reply,name=bot_name) | |
| return reply, format_history(memory.get_conversation(user_name)) | |
| if 'zip' in lower or 'export' in lower: | |
| out = build_zip(os.path.join('projects',project), f'/mnt/data/{project}.zip') | |
| reply = f"Exported: {out}" | |
| memory.add_message(user_name,'bot',reply,name=bot_name) | |
| return reply, format_history(memory.get_conversation(user_name)) | |
| reply = f"{bot_name}: I can analyze code, manage projects, and export zips. Try uploading a file or saying 'analyze project'." | |
| memory.add_message(user_name,'bot',reply,name=bot_name) | |
| return reply, format_history(memory.get_conversation(user_name)) | |
| css = open('static/style.css','r').read() if os.path.exists('static/style.css') else '' | |
| with gr.Blocks(css=css, title='GenBuddy') as demo: | |
| gr.Markdown("""<h1 style='text-align:center'>πΈ GenBuddy β Intelligent AI Buddy</h1>""") | |
| with gr.Row(): | |
| with gr.Column(scale=3): | |
| user_name = gr.Textbox(label='Your name', value='You') | |
| bot_name = gr.Textbox(label='Bot name', value='GenBuddy') | |
| bot_gender = gr.Radio(choices=['female','male','non-binary','no-preference'], label='Bot gender', value='female') | |
| message = gr.Textbox(lines=3, placeholder='Tell me what to do...') | |
| file_input = gr.File(label='Upload a file (code or asset)') | |
| send = gr.Button('Send') | |
| out = gr.Textbox(label='Bot response', lines=12) | |
| with gr.Column(scale=2): | |
| gr.Markdown('### Project Manager') | |
| project = gr.Textbox(label='Project name', value='default') | |
| list_btn = gr.Button('List files') | |
| files_view = gr.Textbox(label='Files (JSON)', lines=12) | |
| zip_btn = gr.Button('Export ZIP') | |
| zip_out = gr.Textbox(label='Zip status', lines=2) | |
| gr.Markdown('### Conversation history') | |
| history = gr.Textbox(label='History', lines=18, interactive=False) | |
| send.click(chat_endpoint, inputs=[message, user_name, bot_name, bot_gender, file_input, project], outputs=[out, history]) | |
| list_btn.click(lambda p: json.dumps(list_project(p), indent=2), inputs=[project], outputs=[files_view]) | |
| 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]) | |
| demo.launch() |