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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import os
|
| 3 |
+
import subprocess
|
| 4 |
+
import json
|
| 5 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig
|
| 6 |
+
import torch
|
| 7 |
+
import time
|
| 8 |
+
|
| 9 |
+
# --- Configuration ---
|
| 10 |
+
MODEL_ID = "mistralai/Mistral-7B-Instruct-v0.2"
|
| 11 |
+
FINETUNED_MODEL_PATH = "./buddha-v2-finetuned"
|
| 12 |
+
SCHEMA_FILE = "master_builder_schema.json"
|
| 13 |
+
|
| 14 |
+
# --- Model & Tokenizer Initialization (Placeholder for Quick Load) ---
|
| 15 |
+
# We use a placeholder function to avoid loading the massive model immediately on startup
|
| 16 |
+
# which often causes timeouts in Gradio. The real loading happens on the first interaction.
|
| 17 |
+
model = None
|
| 18 |
+
tokenizer = None
|
| 19 |
+
master_builder_schema = {}
|
| 20 |
+
model_loaded = False
|
| 21 |
+
is_finetuned = os.path.exists(FINETUNED_MODEL_PATH)
|
| 22 |
+
|
| 23 |
+
def load_model():
|
| 24 |
+
global model, tokenizer, master_builder_schema, model_loaded
|
| 25 |
+
|
| 26 |
+
if model_loaded:
|
| 27 |
+
return
|
| 28 |
+
|
| 29 |
+
# Load the Master Builder Schema
|
| 30 |
+
try:
|
| 31 |
+
with open(SCHEMA_FILE, "r") as f:
|
| 32 |
+
master_builder_schema.update(json.load(f))
|
| 33 |
+
except FileNotFoundError:
|
| 34 |
+
print(f"Warning: {SCHEMA_FILE} not found.")
|
| 35 |
+
|
| 36 |
+
# Determine which model to load
|
| 37 |
+
load_path = FINETUNED_MODEL_PATH if is_finetuned else MODEL_ID
|
| 38 |
+
|
| 39 |
+
print(f"Loading model from: {load_path}")
|
| 40 |
+
|
| 41 |
+
# Configuration for 4-bit quantization
|
| 42 |
+
bnb_config = BitsAndBytesConfig(
|
| 43 |
+
load_in_4bit=True,
|
| 44 |
+
bnb_4bit_use_double_quant=True,
|
| 45 |
+
bnb_4bit_quant_type="nf4",
|
| 46 |
+
bnb_4bit_compute_dtype=torch.bfloat16
|
| 47 |
+
)
|
| 48 |
+
|
| 49 |
+
try:
|
| 50 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
|
| 51 |
+
# Load the model with 4-bit config, even if loading from local path
|
| 52 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 53 |
+
load_path,
|
| 54 |
+
quantization_config=bnb_config,
|
| 55 |
+
device_map="auto"
|
| 56 |
+
)
|
| 57 |
+
model_loaded = True
|
| 58 |
+
print("Model loading complete.")
|
| 59 |
+
except Exception as e:
|
| 60 |
+
print(f"CRITICAL ERROR loading model: {e}")
|
| 61 |
+
# Return False to signal failure
|
| 62 |
+
return False
|
| 63 |
+
|
| 64 |
+
return True
|
| 65 |
+
|
| 66 |
+
# --- Fine-Tuning Status Function ---
|
| 67 |
+
def check_status():
|
| 68 |
+
global is_finetuned
|
| 69 |
+
|
| 70 |
+
if os.path.exists(FINETUNED_MODEL_PATH):
|
| 71 |
+
is_finetuned = True
|
| 72 |
+
return gr.Markdown(
|
| 73 |
+
f"""
|
| 74 |
+
## ✅ Fine-Tuning Complete!
|
| 75 |
+
The fine-tuned model is available at: `{FINETUNED_MODEL_PATH}`
|
| 76 |
+
The main chat interface is now powered by the Master Builder V2 Agent!
|
| 77 |
+
"""
|
| 78 |
+
)
|
| 79 |
+
|
| 80 |
+
# Check for the log file or simply indicate training is required
|
| 81 |
+
return gr.Markdown(
|
| 82 |
+
"""
|
| 83 |
+
## ⏳ Training Required (Torch Error Fix Pending)
|
| 84 |
+
The fine-tuning process has not finished yet, or the job hasn't been started.
|
| 85 |
+
|
| 86 |
+
**CRITICAL STEP:** You must run the installation and training commands in the **JupyterLab Terminal**:
|
| 87 |
+
|
| 88 |
+
1. **Install Libraries (Fixes the 'torch' error):**
|
| 89 |
+
`pip install -r requirements.txt`
|
| 90 |
+
2. **Launch Fine-Tuning:**
|
| 91 |
+
`/usr/bin/python3 finetune_v2.py`
|
| 92 |
+
"""
|
| 93 |
+
)
|
| 94 |
+
|
| 95 |
+
|
| 96 |
+
# --- Core Response Function ---
|
| 97 |
+
def respond_and_generate_app(message, history, app_request_box):
|
| 98 |
+
if not model_loaded:
|
| 99 |
+
if not load_model():
|
| 100 |
+
# If model failed to load (e.g., due to missing torch), give a manual instruction
|
| 101 |
+
yield history, f"🔴 **ERROR:** Cannot load model (likely missing **torch**). Please run `pip install -r requirements.txt` in the **JupyterLab Terminal** first."
|
| 102 |
+
return
|
| 103 |
+
|
| 104 |
+
# --- Tool Call Simulation (Before Fine-Tuning) ---
|
| 105 |
+
if "generate app" in message.lower() or "build" in message.lower():
|
| 106 |
+
# This is a placeholder response
|
| 107 |
+
placeholder_app = f"""
|
| 108 |
+
<!-- HTML Code for {message} -->
|
| 109 |
+
<script src="https://cdn.tailwindcss.com"></script>
|
| 110 |
+
<div class="p-8 max-w-lg mx-auto bg-gray-100 rounded-xl shadow-lg m-4">
|
| 111 |
+
<h2 class="text-2xl font-bold text-indigo-600 mb-4">Master Builder Status</h2>
|
| 112 |
+
<p class="text-gray-700">The Master Builder is currently **running the fine-tuning job** in the background. Once the job is finished, I will be able to generate the full, runnable application for your request:
|
| 113 |
+
<i class="font-semibold">'{message}'</i></p>
|
| 114 |
+
<div class="mt-4 p-3 bg-indigo-100 rounded">
|
| 115 |
+
<p class="text-sm font-mono text-indigo-800">Final Step: Run `pip install -r requirements.txt` and then `/usr/bin/python3 finetune_v2.py` in the **JupyterLab Terminal**.</p>
|
| 116 |
+
</div>
|
| 117 |
+
</div>
|
| 118 |
+
"""
|
| 119 |
+
app_request_box = gr.HTML(placeholder_app)
|
| 120 |
+
new_message = "Master Builder received request. Training job status updated."
|
| 121 |
+
|
| 122 |
+
else:
|
| 123 |
+
# Standard chat response
|
| 124 |
+
new_message = f"Hello! I am Buddha-v2. I am currently focused on becoming the Master Builder Agent. You asked: '{message}'. To generate a web application, ask me to 'Generate an app for...' or 'Build me a website for...'. Check the **Status Tab** for my fine-tuning progress!"
|
| 125 |
+
app_request_box = gr.HTML("<h2>Awaiting App Generation Request...</h2>")
|
| 126 |
+
|
| 127 |
+
# Update history with the response
|
| 128 |
+
history = history + [[message, new_message]]
|
| 129 |
+
|
| 130 |
+
# This uses a yield loop to simulate streaming/updating the app panel
|
| 131 |
+
yield history, app_request_box
|
| 132 |
+
|
| 133 |
+
# --- Gradio Interface Layout ---
|
| 134 |
+
|
| 135 |
+
# Custom CSS for a better look
|
| 136 |
+
css = """
|
| 137 |
+
.gradio-container {
|
| 138 |
+
background: linear-gradient(135deg, #1e3a8a, #374151); /* Dark blue to dark grey */
|
| 139 |
+
color: white;
|
| 140 |
+
}
|
| 141 |
+
h1, h2, .label {
|
| 142 |
+
color: #a5b4fc !important; /* Light blue/purple */
|
| 143 |
+
}
|
| 144 |
+
.chat-message {
|
| 145 |
+
border-radius: 12px;
|
| 146 |
+
}
|
| 147 |
+
.user-message {
|
| 148 |
+
background-color: #3b82f6 !important; /* Blue for user */
|
| 149 |
+
color: white !important;
|
| 150 |
+
}
|
| 151 |
+
.bot-message {
|
| 152 |
+
background-color: #1f2937 !important; /* Darker grey for bot */
|
| 153 |
+
color: #e5e7eb !important;
|
| 154 |
+
}
|
| 155 |
+
"""
|
| 156 |
+
|
| 157 |
+
with gr.Blocks(title="Buddha-v2 Master Builder", css=css, theme=gr.themes.Soft()) as demo:
|
| 158 |
+
gr.HTML("<h1 class='text-center text-4xl py-4 font-bold' style='color: #818cf8;'>Buddha-v2: The Master Manifestor</h1>")
|
| 159 |
+
|
| 160 |
+
# Box to display the generated HTML App
|
| 161 |
+
app_output = gr.HTML(
|
| 162 |
+
value="<h2>Awaiting App Generation Request...</h2>",
|
| 163 |
+
label="Generated Application Preview",
|
| 164 |
+
elem_id="app-output-box"
|
| 165 |
+
)
|
| 166 |
+
|
| 167 |
+
with gr.Tabs():
|
| 168 |
+
|
| 169 |
+
with gr.TabItem("💬 Master Builder Chat"):
|
| 170 |
+
chatbot = gr.Chatbot(
|
| 171 |
+
height=400,
|
| 172 |
+
label="Buddha-v2 Conversation"
|
| 173 |
+
)
|
| 174 |
+
|
| 175 |
+
msg = gr.Textbox(
|
| 176 |
+
placeholder="Ask Buddha-v2 a question, or request a web app (e.g., 'Generate an app for tracking expenses').",
|
| 177 |
+
container=False,
|
| 178 |
+
scale=7
|
| 179 |
+
)
|
| 180 |
+
|
| 181 |
+
clear = gr.Button("Clear")
|
| 182 |
+
|
| 183 |
+
# Setup submit actions
|
| 184 |
+
msg.submit(respond_and_generate_app, [msg, chatbot, app_output], [chatbot, app_output])
|
| 185 |
+
clear.click(lambda: (None, gr.HTML("<h2>Awaiting App Generation Request...</h2>")), None, [chatbot, app_output])
|
| 186 |
+
|
| 187 |
+
gr.Examples(
|
| 188 |
+
examples=[
|
| 189 |
+
["What is the Master Builder Agent?"],
|
| 190 |
+
["Generate an app for calculating BMI."],
|
| 191 |
+
["Build me a simple portfolio website."],
|
| 192 |
+
],
|
| 193 |
+
inputs=msg
|
| 194 |
+
)
|
| 195 |
+
|
| 196 |
+
with gr.TabItem("⚙️ Fine-Tuning Status"):
|
| 197 |
+
status_output = gr.Markdown(check_status().value)
|
| 198 |
+
refresh_button = gr.Button("Refresh Status")
|
| 199 |
+
refresh_button.click(check_status, None, status_output)
|
| 200 |
+
|
| 201 |
+
gr.Markdown(
|
| 202 |
+
"""
|
| 203 |
+
### About Master Builder
|
| 204 |
+
The Buddha-v2 agent is being fine-tuned on the Mistral 7B model to become a **Master Manifestor**.
|
| 205 |
+
Its core skill is converting structured data and user requests into beautiful, **single-file, full-stack web applications** (HTML/CSS/JS or React/Angular) using a special instruction set.
|
| 206 |
+
"""
|
| 207 |
+
)
|
| 208 |
+
|
| 209 |
+
|
| 210 |
+
demo.launch(share=False)
|