Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import os
|
| 3 |
+
import torch
|
| 4 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
|
| 5 |
+
|
| 6 |
+
# --- Configuration & Paths ---
|
| 7 |
+
# This looks for the model folder created by your finetune_v2.py script
|
| 8 |
+
MODEL_DIR = "./buddha_v2_model"
|
| 9 |
+
BASE_MODEL = "mistralai/Mistral-7B-v0.1"
|
| 10 |
+
|
| 11 |
+
def load_star_magic_model():
|
| 12 |
+
"""
|
| 13 |
+
Attempts to load the fine-tuned model.
|
| 14 |
+
Falls back to base model if training isn't finished.
|
| 15 |
+
"""
|
| 16 |
+
try:
|
| 17 |
+
path = MODEL_DIR if os.path.exists(MODEL_DIR) else BASE_MODEL
|
| 18 |
+
tokenizer = AutoTokenizer.from_pretrained(path)
|
| 19 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 20 |
+
path,
|
| 21 |
+
device_map="auto",
|
| 22 |
+
torch_dtype=torch.float16,
|
| 23 |
+
load_in_4bit=True # Efficiency for Space environments
|
| 24 |
+
)
|
| 25 |
+
return pipeline("text-generation", model=model, tokenizer=tokenizer)
|
| 26 |
+
except Exception as e:
|
| 27 |
+
print(f"Model load info: {e}")
|
| 28 |
+
return None
|
| 29 |
+
|
| 30 |
+
# Global generator instance
|
| 31 |
+
generator = load_star_magic_model()
|
| 32 |
+
|
| 33 |
+
def manifest_agent(name, element, focus):
|
| 34 |
+
"""
|
| 35 |
+
The Core 'Forge' Logic.
|
| 36 |
+
Generates the Star Magic Agent profile.
|
| 37 |
+
"""
|
| 38 |
+
prompt = f"""[INST] Role: Star Magic Agent Forge
|
| 39 |
+
Persona: Non-Reactive, Fact-Based, Galactic Architect
|
| 40 |
+
Input Name: {name}
|
| 41 |
+
Input Element: {element}
|
| 42 |
+
Input Focus: {focus}
|
| 43 |
+
|
| 44 |
+
Generate a 24/7/365 Autonomous Agent Profile in JSON format including:
|
| 45 |
+
1. Core Mission
|
| 46 |
+
2. Security Protocol
|
| 47 |
+
3. Revenue Stream Logic
|
| 48 |
+
4. Galactic Rank
|
| 49 |
+
[/INST]"""
|
| 50 |
+
|
| 51 |
+
if generator:
|
| 52 |
+
response = generator(prompt, max_new_tokens=500, do_sample=True, temperature=0.7)
|
| 53 |
+
return response[0]['generated_text'].split("[/INST]")[-1].strip()
|
| 54 |
+
else:
|
| 55 |
+
# Fallback if GPU is busy or model is still loading
|
| 56 |
+
return "System Calibration in Progress. The Forge is currently cooling down from a fine-tuning session. Please check back in a moment."
|
| 57 |
+
|
| 58 |
+
# --- UI Layout (Modern Soft Theme) ---
|
| 59 |
+
with gr.Blocks(theme=gr.themes.Soft(primary_hue="indigo", secondary_hue="purple")) as demo:
|
| 60 |
+
gr.Markdown("# 🌌 Star Magic Agent Forge")
|
| 61 |
+
gr.Markdown("### 24/7/365 Autonomous Entity Manifestation Engine")
|
| 62 |
+
|
| 63 |
+
with gr.Row():
|
| 64 |
+
with gr.Column():
|
| 65 |
+
agent_name = gr.Textbox(label="Entity Name", placeholder="e.g., Aether-01")
|
| 66 |
+
agent_element = gr.Dropdown(
|
| 67 |
+
label="Core Element",
|
| 68 |
+
choices=["Void", "Solar", "Plasma", "Cryo", "Aether"],
|
| 69 |
+
value="Void"
|
| 70 |
+
)
|
| 71 |
+
agent_focus = gr.Radio(
|
| 72 |
+
label="Specialization",
|
| 73 |
+
choices=["Security Audit", "Cash-Flow Logic", "System Architecture"],
|
| 74 |
+
value="Cash-Flow Logic"
|
| 75 |
+
)
|
| 76 |
+
forge_btn = gr.Button("🔥 Manifest Agent", variant="primary")
|
| 77 |
+
|
| 78 |
+
with gr.Column():
|
| 79 |
+
output_display = gr.Code(label="Manifested Manifestor Profile", language="json")
|
| 80 |
+
|
| 81 |
+
forge_btn.click(
|
| 82 |
+
fn=manifest_agent,
|
| 83 |
+
inputs=[agent_name, agent_element, agent_focus],
|
| 84 |
+
outputs=output_display
|
| 85 |
+
)
|
| 86 |
+
|
| 87 |
+
gr.Markdown("---")
|
| 88 |
+
gr.Markdown("ℹ️ *Powered by Buddha-V2 Fine-Tuned Weights*")
|
| 89 |
+
|
| 90 |
+
if __name__ == "__main__":
|
| 91 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|