Spaces:
Sleeping
Sleeping
Update app.py (#8)
Browse files- Update app.py (2fcb8ba9234a1098de23f41b3c0dbc83a4a44629)
app.py
CHANGED
|
@@ -1,156 +1,226 @@
|
|
| 1 |
import os
|
| 2 |
import json
|
|
|
|
| 3 |
import gradio as gr
|
| 4 |
-
from datetime import datetime
|
| 5 |
-
from transformers import AutoTokenizer, AutoModelForCausalLM, Trainer, TrainingArguments
|
| 6 |
from datasets import load_dataset
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
-
#
|
| 9 |
-
|
| 10 |
-
|
|
|
|
|
|
|
| 11 |
|
| 12 |
-
|
| 13 |
-
|
|
|
|
| 14 |
|
| 15 |
def get_memory_file(model_name):
|
| 16 |
-
|
| 17 |
-
return os.path.join(MEMORY_DIR, f"{
|
| 18 |
|
| 19 |
def load_memory(model_name):
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
|
|
|
| 23 |
return json.load(f)
|
| 24 |
return []
|
| 25 |
|
| 26 |
-
def save_memory(model_name,
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
if
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
def tokenize(batch):
|
| 76 |
-
return tokenizer(batch["text"], truncation=True, padding="max_length", max_length=128)
|
| 77 |
-
|
| 78 |
-
dataset = dataset.map(tokenize, batched=True)
|
| 79 |
-
training_args = TrainingArguments(
|
| 80 |
-
output_dir=output_dir,
|
| 81 |
-
overwrite_output_dir=True,
|
| 82 |
-
per_device_train_batch_size=2,
|
| 83 |
-
num_train_epochs=int(epochs),
|
| 84 |
-
save_strategy="epoch",
|
| 85 |
-
logging_dir=f"{output_dir}/logs"
|
| 86 |
)
|
| 87 |
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 118 |
with gr.Blocks() as demo:
|
| 119 |
-
gr.Markdown("#
|
| 120 |
-
|
| 121 |
-
with gr.
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
|
|
|
|
|
|
| 155 |
|
| 156 |
demo.launch()
|
|
|
|
| 1 |
import os
|
| 2 |
import json
|
| 3 |
+
import datetime
|
| 4 |
import gradio as gr
|
|
|
|
|
|
|
| 5 |
from datasets import load_dataset
|
| 6 |
+
from transformers import (
|
| 7 |
+
AutoModelForCausalLM,
|
| 8 |
+
AutoTokenizer,
|
| 9 |
+
Trainer,
|
| 10 |
+
TrainingArguments,
|
| 11 |
+
DataCollatorForLanguageModeling,
|
| 12 |
+
)
|
| 13 |
+
import torch
|
| 14 |
+
|
| 15 |
+
# ==============================
|
| 16 |
+
# Paths & Storage Setup
|
| 17 |
+
# ==============================
|
| 18 |
+
BASE_DIR = "storage"
|
| 19 |
+
MODELS_DIR = os.path.join(BASE_DIR, "models")
|
| 20 |
+
MEMORY_DIR = os.path.join(BASE_DIR, "memory")
|
| 21 |
+
os.makedirs(MODELS_DIR, exist_ok=True)
|
| 22 |
+
os.makedirs(MEMORY_DIR, exist_ok=True)
|
| 23 |
|
| 24 |
+
# ==============================
|
| 25 |
+
# Global State
|
| 26 |
+
# ==============================
|
| 27 |
+
loaded_models = {} # cache for loaded models
|
| 28 |
+
chat_sessions = {} # memory per model & session
|
| 29 |
|
| 30 |
+
# ==============================
|
| 31 |
+
# Helper Functions
|
| 32 |
+
# ==============================
|
| 33 |
|
| 34 |
def get_memory_file(model_name):
|
| 35 |
+
"""Return path to memory file for a given model"""
|
| 36 |
+
return os.path.join(MEMORY_DIR, f"{model_name}_memory.json")
|
| 37 |
|
| 38 |
def load_memory(model_name):
|
| 39 |
+
"""Load chat memory from file"""
|
| 40 |
+
path = get_memory_file(model_name)
|
| 41 |
+
if os.path.exists(path):
|
| 42 |
+
with open(path, "r") as f:
|
| 43 |
return json.load(f)
|
| 44 |
return []
|
| 45 |
|
| 46 |
+
def save_memory(model_name, history):
|
| 47 |
+
"""Save chat memory to file"""
|
| 48 |
+
path = get_memory_file(model_name)
|
| 49 |
+
with open(path, "w") as f:
|
| 50 |
+
json.dump(history, f, indent=2)
|
| 51 |
+
|
| 52 |
+
def load_model(model_name):
|
| 53 |
+
"""Load model + tokenizer (cached if already loaded)"""
|
| 54 |
+
if model_name in loaded_models:
|
| 55 |
+
return loaded_models[model_name]
|
| 56 |
+
|
| 57 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 58 |
+
if tokenizer.pad_token is None:
|
| 59 |
+
tokenizer.pad_token = tokenizer.eos_token
|
| 60 |
+
|
| 61 |
+
model = AutoModelForCausalLM.from_pretrained(model_name)
|
| 62 |
+
model.to("cuda" if torch.cuda.is_available() else "cpu")
|
| 63 |
+
|
| 64 |
+
loaded_models[model_name] = (tokenizer, model)
|
| 65 |
+
return tokenizer, model
|
| 66 |
+
|
| 67 |
+
# ==============================
|
| 68 |
+
# Chat / Test Tab
|
| 69 |
+
# ==============================
|
| 70 |
+
|
| 71 |
+
def chat_with_model(model_name, user_message, session_id="default"):
|
| 72 |
+
if not model_name:
|
| 73 |
+
return "β οΈ Please select a model.", []
|
| 74 |
+
|
| 75 |
+
tokenizer, model = load_model(model_name)
|
| 76 |
+
|
| 77 |
+
# Load session memory
|
| 78 |
+
session_key = f"{model_name}_{session_id}"
|
| 79 |
+
if session_key not in chat_sessions:
|
| 80 |
+
chat_sessions[session_key] = load_memory(model_name)
|
| 81 |
+
|
| 82 |
+
history = chat_sessions[session_key]
|
| 83 |
+
history.append({"role": "user", "content": user_message})
|
| 84 |
+
|
| 85 |
+
# Prepare input for model
|
| 86 |
+
context = "\n".join([f"{h['role']}: {h['content']}" for h in history])
|
| 87 |
+
inputs = tokenizer(context, return_tensors="pt").to(model.device)
|
| 88 |
+
|
| 89 |
+
with torch.no_grad():
|
| 90 |
+
outputs = model.generate(
|
| 91 |
+
**inputs,
|
| 92 |
+
max_length=512,
|
| 93 |
+
pad_token_id=tokenizer.eos_token_id,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
)
|
| 95 |
|
| 96 |
+
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 97 |
+
response = response[len(context):].strip()
|
| 98 |
+
|
| 99 |
+
history.append({"role": "assistant", "content": response})
|
| 100 |
+
|
| 101 |
+
# Save memory
|
| 102 |
+
save_memory(model_name, history)
|
| 103 |
+
|
| 104 |
+
chat_sessions[session_key] = history
|
| 105 |
+
display_history = [(h["content"] if h["role"]=="user" else None,
|
| 106 |
+
h["content"] if h["role"]=="assistant" else None) for h in history]
|
| 107 |
+
|
| 108 |
+
return response, display_history
|
| 109 |
+
|
| 110 |
+
# ==============================
|
| 111 |
+
# Training Tab
|
| 112 |
+
# ==============================
|
| 113 |
+
|
| 114 |
+
def train_model(model_name, dataset_name, epochs, output_dir):
|
| 115 |
+
if not model_name or not dataset_name:
|
| 116 |
+
return "β οΈ Please provide model & dataset."
|
| 117 |
+
|
| 118 |
+
tokenizer, model = load_model(model_name)
|
| 119 |
+
dataset = load_dataset(dataset_name)
|
| 120 |
+
|
| 121 |
+
def tokenize_function(examples):
|
| 122 |
+
return tokenizer(examples["text"], truncation=True, padding="max_length")
|
| 123 |
+
|
| 124 |
+
tokenized = dataset.map(tokenize_function, batched=True, remove_columns=["text"])
|
| 125 |
+
|
| 126 |
+
data_collator = DataCollatorForLanguageModeling(tokenizer=tokenizer, mlm=False)
|
| 127 |
+
|
| 128 |
+
args = TrainingArguments(
|
| 129 |
+
output_dir=output_dir,
|
| 130 |
+
overwrite_output_dir=True,
|
| 131 |
+
evaluation_strategy="epoch",
|
| 132 |
+
save_strategy="epoch",
|
| 133 |
+
num_train_epochs=int(epochs),
|
| 134 |
+
per_device_train_batch_size=2,
|
| 135 |
+
save_total_limit=2,
|
| 136 |
+
logging_dir="./logs",
|
| 137 |
+
logging_steps=10,
|
| 138 |
+
)
|
| 139 |
+
|
| 140 |
+
trainer = Trainer(
|
| 141 |
+
model=model,
|
| 142 |
+
args=args,
|
| 143 |
+
train_dataset=tokenized["train"],
|
| 144 |
+
eval_dataset=tokenized.get("test"),
|
| 145 |
+
data_collator=data_collator,
|
| 146 |
+
tokenizer=tokenizer,
|
| 147 |
+
)
|
| 148 |
+
|
| 149 |
+
trainer.train()
|
| 150 |
+
model.save_pretrained(output_dir)
|
| 151 |
+
tokenizer.save_pretrained(output_dir)
|
| 152 |
+
|
| 153 |
+
return f"β
Training finished. Model saved to {output_dir}"
|
| 154 |
+
|
| 155 |
+
# ==============================
|
| 156 |
+
# Guide / Manual Tab
|
| 157 |
+
# ==============================
|
| 158 |
+
|
| 159 |
+
BEGINNER_GUIDE = """
|
| 160 |
+
# Beginner Guide: My AI Model Builder
|
| 161 |
+
|
| 162 |
+
1. **Choose a model** β Select a base Hugging Face model to load.
|
| 163 |
+
2. **Train / Fine-Tune** β Pick a dataset and train it for X epochs.
|
| 164 |
+
3. **Test / Chat** β Go to the Chat tab, type prompts, and interact with your model.
|
| 165 |
+
4. **Memory** β Your chats are saved per model automatically.
|
| 166 |
+
5. **Downloads** β You can export models and memory from the Downloads tab.
|
| 167 |
+
|
| 168 |
+
Thatβs it! π
|
| 169 |
+
"""
|
| 170 |
+
|
| 171 |
+
TECHNICAL_GUIDE = """
|
| 172 |
+
# Technical Manual: My AI Model Builder
|
| 173 |
+
|
| 174 |
+
- **Storage**: All models and memory are saved in `/storage/`.
|
| 175 |
+
- **Memory**: Each model has its own JSON memory file.
|
| 176 |
+
- **Trainer**: Uses Hugging Face `Trainer` with language modeling.
|
| 177 |
+
- **Customization**: You can swap base models, datasets, epochs, etc.
|
| 178 |
+
- **Sessions**: Each model can have multiple session IDs for separate conversations.
|
| 179 |
+
- **Reliability**: App checks if memory/model files exist before creating new ones.
|
| 180 |
+
"""
|
| 181 |
+
|
| 182 |
+
# ==============================
|
| 183 |
+
# Build Gradio UI
|
| 184 |
+
# ==============================
|
| 185 |
+
|
| 186 |
with gr.Blocks() as demo:
|
| 187 |
+
gr.Markdown("# π οΈ My AI Model Builder")
|
| 188 |
+
|
| 189 |
+
with gr.Tabs():
|
| 190 |
+
# Chat / Test Tab
|
| 191 |
+
with gr.Tab("π¬ Chat / Test"):
|
| 192 |
+
model_name = gr.Textbox(label="Model Name", placeholder="e.g. gpt2")
|
| 193 |
+
session_id = gr.Textbox(label="Session ID (optional)", value="default")
|
| 194 |
+
chatbot = gr.Chatbot()
|
| 195 |
+
msg = gr.Textbox(label="Your message")
|
| 196 |
+
send = gr.Button("Send")
|
| 197 |
+
|
| 198 |
+
send.click(
|
| 199 |
+
fn=chat_with_model,
|
| 200 |
+
inputs=[model_name, msg, session_id],
|
| 201 |
+
outputs=[msg, chatbot],
|
| 202 |
+
)
|
| 203 |
+
|
| 204 |
+
# Training Tab
|
| 205 |
+
with gr.Tab("π Training / Fine-Tuning"):
|
| 206 |
+
base_model = gr.Textbox(label="Base Model", placeholder="e.g. gpt2")
|
| 207 |
+
dataset_name = gr.Textbox(label="Dataset (HF Hub)", placeholder="e.g. wikitext")
|
| 208 |
+
epochs = gr.Number(label="Epochs", value=1)
|
| 209 |
+
output_dir = gr.Textbox(label="Output Dir", value="./storage/models/new_model")
|
| 210 |
+
train_btn = gr.Button("Start Training")
|
| 211 |
+
train_output = gr.Textbox(label="Training Log")
|
| 212 |
+
|
| 213 |
+
train_btn.click(
|
| 214 |
+
fn=train_model,
|
| 215 |
+
inputs=[base_model, dataset_name, epochs, output_dir],
|
| 216 |
+
outputs=train_output,
|
| 217 |
+
)
|
| 218 |
+
|
| 219 |
+
# Guide Tab
|
| 220 |
+
with gr.Tab("π Guide / Manual"):
|
| 221 |
+
gr.Markdown("## Beginner Walkthrough")
|
| 222 |
+
gr.Markdown(BEGINNER_GUIDE)
|
| 223 |
+
gr.Markdown("## Technical Reference")
|
| 224 |
+
gr.Markdown(TECHNICAL_GUIDE)
|
| 225 |
|
| 226 |
demo.launch()
|