Update app_flash.py
Browse files- app_flash.py +105 -101
app_flash.py
CHANGED
|
@@ -1,128 +1,158 @@
|
|
| 1 |
-
import gc
|
| 2 |
import os
|
|
|
|
| 3 |
import torch
|
| 4 |
import torch.nn as nn
|
| 5 |
import torch.optim as optim
|
| 6 |
-
|
| 7 |
from datasets import load_dataset
|
| 8 |
-
from transformers import AutoTokenizer, AutoModel, AutoModelForCausalLM, pipeline
|
| 9 |
from flashpack import FlashPackMixin
|
| 10 |
-
from
|
|
|
|
|
|
|
| 11 |
|
| 12 |
# ============================================================
|
| 13 |
-
#
|
| 14 |
# ============================================================
|
| 15 |
device = torch.device("cpu")
|
| 16 |
torch.set_num_threads(4)
|
| 17 |
-
print(f"π§
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
# ============================================================
|
| 20 |
-
#
|
| 21 |
# ============================================================
|
| 22 |
class GemmaTrainer(nn.Module, FlashPackMixin):
|
| 23 |
-
def __init__(self, input_dim
|
| 24 |
super().__init__()
|
| 25 |
self.fc1 = nn.Linear(input_dim, hidden_dim)
|
| 26 |
self.relu = nn.ReLU()
|
| 27 |
self.fc2 = nn.Linear(hidden_dim, output_dim)
|
| 28 |
|
| 29 |
-
def forward(self, x
|
| 30 |
-
|
| 31 |
-
x = self.relu(x)
|
| 32 |
-
x = self.fc2(x)
|
| 33 |
-
return x
|
| 34 |
|
| 35 |
|
| 36 |
# ============================================================
|
| 37 |
-
#
|
| 38 |
# ============================================================
|
| 39 |
-
def build_encoder(model_name="gpt2", max_length
|
| 40 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 41 |
if tokenizer.pad_token is None:
|
| 42 |
tokenizer.pad_token = tokenizer.eos_token
|
| 43 |
-
|
| 44 |
embed_model = AutoModel.from_pretrained(model_name).to(device)
|
| 45 |
embed_model.eval()
|
| 46 |
|
| 47 |
@torch.no_grad()
|
| 48 |
-
def encode(
|
| 49 |
inputs = tokenizer(
|
| 50 |
-
|
| 51 |
return_tensors="pt",
|
| 52 |
truncation=True,
|
| 53 |
padding="max_length",
|
| 54 |
max_length=max_length,
|
| 55 |
).to(device)
|
| 56 |
-
|
| 57 |
-
return outputs.cpu()
|
| 58 |
|
| 59 |
return tokenizer, embed_model, encode
|
| 60 |
|
| 61 |
|
| 62 |
# ============================================================
|
| 63 |
-
#
|
| 64 |
# ============================================================
|
| 65 |
-
def
|
| 66 |
-
model
|
| 67 |
-
|
| 68 |
-
embed_model =
|
| 69 |
-
return model, tokenizer, embed_model
|
| 70 |
|
| 71 |
-
#
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
# model = GemmaTrainer.from_flashpack(hf_repo)
|
| 75 |
|
| 76 |
-
#
|
| 77 |
-
|
| 78 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
|
|
|
|
|
|
|
| 80 |
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
|
| 86 |
-
|
| 87 |
-
|
|
|
|
| 88 |
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
device=-1, # CPU
|
| 94 |
-
)
|
| 95 |
|
| 96 |
-
|
|
|
|
| 97 |
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
|
| 108 |
|
| 109 |
# ============================================================
|
| 110 |
-
#
|
| 111 |
# ============================================================
|
| 112 |
-
model, tokenizer, embed_model =
|
| 113 |
dataset = load_dataset("gokaygokay/prompt-enhancer-dataset", split="train")
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 119 |
|
| 120 |
|
| 121 |
# ============================================================
|
| 122 |
-
#
|
| 123 |
# ============================================================
|
| 124 |
@torch.no_grad()
|
| 125 |
-
def encode_for_inference(prompt: str)
|
| 126 |
inputs = tokenizer(
|
| 127 |
prompt,
|
| 128 |
return_tensors="pt",
|
|
@@ -140,10 +170,7 @@ def enhance_prompt_flashpack(user_prompt: str, temperature: float, max_tokens: i
|
|
| 140 |
mapped = model(short_emb.to(device)).cpu()
|
| 141 |
|
| 142 |
sims = (long_embeddings @ mapped.t()).squeeze(1)
|
| 143 |
-
|
| 144 |
-
mapped_norm = mapped.norm()
|
| 145 |
-
sims = sims / (long_norms * (mapped_norm + 1e-12))
|
| 146 |
-
|
| 147 |
best_idx = int(sims.argmax().item())
|
| 148 |
enhanced_prompt = dataset[best_idx]["long_prompt"]
|
| 149 |
|
|
@@ -153,36 +180,14 @@ def enhance_prompt_flashpack(user_prompt: str, temperature: float, max_tokens: i
|
|
| 153 |
|
| 154 |
|
| 155 |
# ============================================================
|
| 156 |
-
#
|
| 157 |
# ============================================================
|
| 158 |
-
|
| 159 |
-
chat_history = chat_history or []
|
| 160 |
-
messages = [
|
| 161 |
-
{"role": "system", "content": "Enhance and expand the following prompt with more details and context:"},
|
| 162 |
-
{"role": "user", "content": user_prompt}
|
| 163 |
-
]
|
| 164 |
-
prompt = tokenizer_gemma.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
|
| 165 |
-
output = pipe_gemma(
|
| 166 |
-
prompt,
|
| 167 |
-
max_new_tokens=int(max_tokens),
|
| 168 |
-
temperature=float(temperature),
|
| 169 |
-
do_sample=True,
|
| 170 |
-
)[0]["generated_text"]
|
| 171 |
-
enhanced_text = extract_later_part(user_prompt, output)
|
| 172 |
-
chat_history.append({"role": "user", "content": user_prompt})
|
| 173 |
-
chat_history.append({"role": "assistant", "content": enhanced_text})
|
| 174 |
-
return chat_history
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
# ============================================================
|
| 178 |
-
# 8οΈβ£ Gradio UI
|
| 179 |
-
# ============================================================
|
| 180 |
-
with gr.Blocks(title="Prompt Enhancer β FlashPack + Gemma (CPU)", theme=gr.themes.Soft()) as demo:
|
| 181 |
gr.Markdown("""
|
| 182 |
-
# β¨ Prompt Enhancer
|
| 183 |
-
- **
|
| 184 |
-
-
|
| 185 |
-
- CPU-only,
|
| 186 |
""")
|
| 187 |
|
| 188 |
with gr.Row():
|
|
@@ -191,17 +196,16 @@ with gr.Blocks(title="Prompt Enhancer β FlashPack + Gemma (CPU)", theme=gr.the
|
|
| 191 |
user_prompt = gr.Textbox(placeholder="Enter a short prompt...", label="Your Prompt", lines=3)
|
| 192 |
temperature = gr.Slider(0.1, 1.5, value=0.7, label="Temperature")
|
| 193 |
max_tokens = gr.Slider(32, 512, value=256, label="Max Tokens")
|
| 194 |
-
|
| 195 |
-
send_flashpack = gr.Button("π Enhance (FlashPack)")
|
| 196 |
clear_btn = gr.Button("π§Ή Clear Chat")
|
| 197 |
|
| 198 |
-
send_gemma.click(enhance_prompt_gemma, [user_prompt, temperature, max_tokens, chatbot], chatbot)
|
| 199 |
send_flashpack.click(enhance_prompt_flashpack, [user_prompt, temperature, max_tokens, chatbot], chatbot)
|
| 200 |
-
user_prompt.submit(
|
| 201 |
clear_btn.click(lambda: [], None, chatbot)
|
| 202 |
|
|
|
|
| 203 |
# ============================================================
|
| 204 |
-
#
|
| 205 |
# ============================================================
|
| 206 |
if __name__ == "__main__":
|
| 207 |
demo.launch(show_error=True)
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
+
import re
|
| 3 |
import torch
|
| 4 |
import torch.nn as nn
|
| 5 |
import torch.optim as optim
|
| 6 |
+
from typing import Tuple
|
| 7 |
from datasets import load_dataset
|
|
|
|
| 8 |
from flashpack import FlashPackMixin
|
| 9 |
+
from huggingface_hub import HfApi, create_repo, repo_exists
|
| 10 |
+
import gradio as gr
|
| 11 |
+
from transformers import AutoTokenizer, AutoModel
|
| 12 |
|
| 13 |
# ============================================================
|
| 14 |
+
# βοΈ Setup
|
| 15 |
# ============================================================
|
| 16 |
device = torch.device("cpu")
|
| 17 |
torch.set_num_threads(4)
|
| 18 |
+
print(f"π§ Using device: {device} (CPU-only mode)")
|
| 19 |
+
|
| 20 |
+
HF_REPO = "rahul7star/FlashPack"
|
| 21 |
+
MODEL_ID = HF_REPO
|
| 22 |
+
|
| 23 |
|
| 24 |
# ============================================================
|
| 25 |
+
# π§ Define FlashPack Trainer
|
| 26 |
# ============================================================
|
| 27 |
class GemmaTrainer(nn.Module, FlashPackMixin):
|
| 28 |
+
def __init__(self, input_dim=768, hidden_dim=512, output_dim=768):
|
| 29 |
super().__init__()
|
| 30 |
self.fc1 = nn.Linear(input_dim, hidden_dim)
|
| 31 |
self.relu = nn.ReLU()
|
| 32 |
self.fc2 = nn.Linear(hidden_dim, output_dim)
|
| 33 |
|
| 34 |
+
def forward(self, x):
|
| 35 |
+
return self.fc2(self.relu(self.fc1(x)))
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
|
| 38 |
# ============================================================
|
| 39 |
+
# π€ Encoder Builder (GPT2 base)
|
| 40 |
# ============================================================
|
| 41 |
+
def build_encoder(model_name="gpt2", max_length=32):
|
| 42 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 43 |
if tokenizer.pad_token is None:
|
| 44 |
tokenizer.pad_token = tokenizer.eos_token
|
|
|
|
| 45 |
embed_model = AutoModel.from_pretrained(model_name).to(device)
|
| 46 |
embed_model.eval()
|
| 47 |
|
| 48 |
@torch.no_grad()
|
| 49 |
+
def encode(text: str):
|
| 50 |
inputs = tokenizer(
|
| 51 |
+
text,
|
| 52 |
return_tensors="pt",
|
| 53 |
truncation=True,
|
| 54 |
padding="max_length",
|
| 55 |
max_length=max_length,
|
| 56 |
).to(device)
|
| 57 |
+
return embed_model(**inputs).last_hidden_state.mean(dim=1).cpu()
|
|
|
|
| 58 |
|
| 59 |
return tokenizer, embed_model, encode
|
| 60 |
|
| 61 |
|
| 62 |
# ============================================================
|
| 63 |
+
# π§© FlashPack: Train and Upload (uses Gemma only internally)
|
| 64 |
# ============================================================
|
| 65 |
+
def train_flashpack_model(hf_repo=HF_REPO):
|
| 66 |
+
print(f"π Training new FlashPack model for repo: {hf_repo}")
|
| 67 |
+
model = GemmaTrainer()
|
| 68 |
+
tokenizer, embed_model, encode = build_encoder("gpt2")
|
|
|
|
| 69 |
|
| 70 |
+
# Load dataset (Gemma-expanded dataset)
|
| 71 |
+
dataset = load_dataset("gokaygokay/prompt-enhancer-dataset", split="train")
|
|
|
|
|
|
|
| 72 |
|
| 73 |
+
# Compute embeddings for training (short β long)
|
| 74 |
+
X, Y = [], []
|
| 75 |
+
for p in dataset.select(range(300)):
|
| 76 |
+
short_emb = encode(p["short_prompt"])
|
| 77 |
+
long_emb = encode(p["long_prompt"])
|
| 78 |
+
X.append(short_emb)
|
| 79 |
+
Y.append(long_emb)
|
| 80 |
|
| 81 |
+
X = torch.vstack(X)
|
| 82 |
+
Y = torch.vstack(Y)
|
| 83 |
|
| 84 |
+
optimizer = optim.Adam(model.parameters(), lr=1e-3)
|
| 85 |
+
for epoch in range(10):
|
| 86 |
+
out = model(X)
|
| 87 |
+
loss = nn.MSELoss()(out, Y)
|
| 88 |
+
optimizer.zero_grad()
|
| 89 |
+
loss.backward()
|
| 90 |
+
optimizer.step()
|
| 91 |
+
print(f"Epoch {epoch+1}/10 | Loss: {loss.item():.6f}")
|
| 92 |
|
| 93 |
+
# Save FlashPack model and push
|
| 94 |
+
model.to_flashpack("flashpack_model")
|
| 95 |
+
print("πΎ Model saved locally. Uploading to Hugging Face...")
|
| 96 |
|
| 97 |
+
api = HfApi()
|
| 98 |
+
if not repo_exists(hf_repo):
|
| 99 |
+
create_repo(hf_repo, repo_type="model", exist_ok=True)
|
| 100 |
+
model.push_to_hub(hf_repo, commit_message="Initial FlashPack model training")
|
|
|
|
|
|
|
| 101 |
|
| 102 |
+
print(f"β
Model uploaded successfully to {hf_repo}")
|
| 103 |
+
return model, tokenizer, embed_model
|
| 104 |
|
| 105 |
+
|
| 106 |
+
# ============================================================
|
| 107 |
+
# π¦ Load FlashPack from Hub
|
| 108 |
+
# ============================================================
|
| 109 |
+
def load_flashpack_model(hf_repo=HF_REPO):
|
| 110 |
+
print(f"π₯ Loading FlashPack model from {hf_repo}...")
|
| 111 |
+
model = GemmaTrainer.from_flashpack(hf_repo)
|
| 112 |
+
tokenizer, embed_model, encode = build_encoder("gpt2")
|
| 113 |
+
print("β
Loaded FlashPack model successfully.")
|
| 114 |
+
return model, tokenizer, embed_model
|
| 115 |
+
|
| 116 |
+
|
| 117 |
+
# ============================================================
|
| 118 |
+
# β‘ Auto Load or Train
|
| 119 |
+
# ============================================================
|
| 120 |
+
def get_flashpack_model(hf_repo=HF_REPO):
|
| 121 |
+
try:
|
| 122 |
+
api = HfApi()
|
| 123 |
+
if repo_exists(hf_repo):
|
| 124 |
+
print("β
Found trained model on Hub.")
|
| 125 |
+
return load_flashpack_model(hf_repo)
|
| 126 |
+
else:
|
| 127 |
+
print("β Model not found, training new one using Gemma dataset...")
|
| 128 |
+
return train_flashpack_model(hf_repo)
|
| 129 |
+
except Exception as e:
|
| 130 |
+
print(f"β οΈ Repo check failed: {e}. Retraining model locally.")
|
| 131 |
+
return train_flashpack_model(hf_repo)
|
| 132 |
|
| 133 |
|
| 134 |
# ============================================================
|
| 135 |
+
# π Dataset + Model
|
| 136 |
# ============================================================
|
| 137 |
+
model, tokenizer, embed_model = get_flashpack_model()
|
| 138 |
dataset = load_dataset("gokaygokay/prompt-enhancer-dataset", split="train")
|
| 139 |
+
|
| 140 |
+
long_embeddings = torch.vstack([
|
| 141 |
+
embed_model(**tokenizer(
|
| 142 |
+
p["long_prompt"], return_tensors="pt",
|
| 143 |
+
truncation=True, padding="max_length", max_length=32
|
| 144 |
+
)).last_hidden_state.mean(dim=1).cpu()
|
| 145 |
+
for p in dataset.select(range(min(500, len(dataset))))
|
| 146 |
+
])
|
| 147 |
+
|
| 148 |
+
print("β
FlashPack model and embeddings loaded.")
|
| 149 |
|
| 150 |
|
| 151 |
# ============================================================
|
| 152 |
+
# π§ Inference Helpers
|
| 153 |
# ============================================================
|
| 154 |
@torch.no_grad()
|
| 155 |
+
def encode_for_inference(prompt: str):
|
| 156 |
inputs = tokenizer(
|
| 157 |
prompt,
|
| 158 |
return_tensors="pt",
|
|
|
|
| 170 |
mapped = model(short_emb.to(device)).cpu()
|
| 171 |
|
| 172 |
sims = (long_embeddings @ mapped.t()).squeeze(1)
|
| 173 |
+
sims /= (long_embeddings.norm(dim=1) * (mapped.norm() + 1e-12))
|
|
|
|
|
|
|
|
|
|
| 174 |
best_idx = int(sims.argmax().item())
|
| 175 |
enhanced_prompt = dataset[best_idx]["long_prompt"]
|
| 176 |
|
|
|
|
| 180 |
|
| 181 |
|
| 182 |
# ============================================================
|
| 183 |
+
# π¬ Gradio UI
|
| 184 |
# ============================================================
|
| 185 |
+
with gr.Blocks(title="Prompt Enhancer β FlashPack Only", theme=gr.themes.Soft()) as demo:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 186 |
gr.Markdown("""
|
| 187 |
+
# β¨ FlashPack Prompt Enhancer
|
| 188 |
+
- Uses pre-trained **FlashPack model** (`rahul7star/FlashPack`)
|
| 189 |
+
- Matches short prompts to enhanced long prompts using learned embeddings
|
| 190 |
+
- CPU-only, no Gemma dependency during inference.
|
| 191 |
""")
|
| 192 |
|
| 193 |
with gr.Row():
|
|
|
|
| 196 |
user_prompt = gr.Textbox(placeholder="Enter a short prompt...", label="Your Prompt", lines=3)
|
| 197 |
temperature = gr.Slider(0.1, 1.5, value=0.7, label="Temperature")
|
| 198 |
max_tokens = gr.Slider(32, 512, value=256, label="Max Tokens")
|
| 199 |
+
send_flashpack = gr.Button("π Enhance Prompt")
|
|
|
|
| 200 |
clear_btn = gr.Button("π§Ή Clear Chat")
|
| 201 |
|
|
|
|
| 202 |
send_flashpack.click(enhance_prompt_flashpack, [user_prompt, temperature, max_tokens, chatbot], chatbot)
|
| 203 |
+
user_prompt.submit(enhance_prompt_flashpack, [user_prompt, temperature, max_tokens, chatbot], chatbot)
|
| 204 |
clear_btn.click(lambda: [], None, chatbot)
|
| 205 |
|
| 206 |
+
|
| 207 |
# ============================================================
|
| 208 |
+
# π Launch App
|
| 209 |
# ============================================================
|
| 210 |
if __name__ == "__main__":
|
| 211 |
demo.launch(show_error=True)
|