Create app_flash.py
Browse files- app_flash.py +107 -0
app_flash.py
ADDED
|
@@ -0,0 +1,107 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
|
| 4 |
+
from flashpack.integrations.transformers import FlashPackTransformersModelMixin
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
# ============================================================
|
| 8 |
+
# 1️⃣ Extend model with FlashPackMixin
|
| 9 |
+
# ============================================================
|
| 10 |
+
class FlashPackedCausalLM(AutoModelForCausalLM, FlashPackTransformersModelMixin):
|
| 11 |
+
pass
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
# ============================================================
|
| 15 |
+
# 2️⃣ Load or prepare model
|
| 16 |
+
# ============================================================
|
| 17 |
+
MODEL_ID = "gokaygokay/prompt-enhancer-gemma-3-270m-it"
|
| 18 |
+
|
| 19 |
+
try:
|
| 20 |
+
print("📂 Trying to load FlashPack model...")
|
| 21 |
+
model = FlashPackedCausalLM.from_pretrained_flashpack("model_flashpack")
|
| 22 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
|
| 23 |
+
except Exception as e:
|
| 24 |
+
print("⚙️ FlashPack not found, loading from Hugging Face...")
|
| 25 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
|
| 26 |
+
model = AutoModelForCausalLM.from_pretrained(MODEL_ID)
|
| 27 |
+
model.save_pretrained_flashpack("model_flashpack")
|
| 28 |
+
print("✅ Model saved as FlashPack for next startup!")
|
| 29 |
+
|
| 30 |
+
# Create text-generation pipeline
|
| 31 |
+
pipe = pipeline("text-generation", model=model, tokenizer=tokenizer, torch_dtype="auto", device_map="auto")
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
# ============================================================
|
| 35 |
+
# 3️⃣ Define inference logic
|
| 36 |
+
# ============================================================
|
| 37 |
+
def enhance_prompt(user_prompt, temperature, max_tokens, chat_history):
|
| 38 |
+
chat_history = chat_history or []
|
| 39 |
+
|
| 40 |
+
messages = [
|
| 41 |
+
{"role": "system", "content": "Enhance and expand the following prompt with more details and context:"},
|
| 42 |
+
{"role": "user", "content": user_prompt},
|
| 43 |
+
]
|
| 44 |
+
|
| 45 |
+
prompt = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
|
| 46 |
+
|
| 47 |
+
outputs = pipe(
|
| 48 |
+
prompt,
|
| 49 |
+
max_new_tokens=int(max_tokens),
|
| 50 |
+
temperature=float(temperature),
|
| 51 |
+
do_sample=True,
|
| 52 |
+
)
|
| 53 |
+
|
| 54 |
+
enhanced = outputs[0]["generated_text"].strip()
|
| 55 |
+
|
| 56 |
+
# Add to chat
|
| 57 |
+
chat_history.append({"role": "user", "content": user_prompt})
|
| 58 |
+
chat_history.append({"role": "assistant", "content": enhanced})
|
| 59 |
+
|
| 60 |
+
return chat_history
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
# ============================================================
|
| 64 |
+
# 4️⃣ Gradio Interface
|
| 65 |
+
# ============================================================
|
| 66 |
+
with gr.Blocks(title="Prompt Enhancer – Gemma 3 270M", theme=gr.themes.Soft()) as demo:
|
| 67 |
+
gr.Markdown(
|
| 68 |
+
"""
|
| 69 |
+
# ✨ Prompt Enhancer (Gemma 3 270M)
|
| 70 |
+
Enter a short prompt, and the model will **expand it with details and creative context**
|
| 71 |
+
using the Gemma chat-template interface.
|
| 72 |
+
"""
|
| 73 |
+
)
|
| 74 |
+
|
| 75 |
+
with gr.Row():
|
| 76 |
+
chatbot = gr.Chatbot(height=400, label="Enhanced Prompts", type="messages")
|
| 77 |
+
with gr.Column(scale=1):
|
| 78 |
+
user_prompt = gr.Textbox(
|
| 79 |
+
placeholder="Enter a short prompt...",
|
| 80 |
+
label="Your Prompt",
|
| 81 |
+
lines=3,
|
| 82 |
+
)
|
| 83 |
+
temperature = gr.Slider(0.0, 1.0, value=0.7, step=0.05, label="Temperature")
|
| 84 |
+
max_tokens = gr.Slider(32, 256, value=128, step=16, label="Max Tokens")
|
| 85 |
+
send_btn = gr.Button("🚀 Enhance Prompt", variant="primary")
|
| 86 |
+
clear_btn = gr.Button("🧹 Clear Chat")
|
| 87 |
+
|
| 88 |
+
# Bind UI actions
|
| 89 |
+
send_btn.click(enhance_prompt, [user_prompt, temperature, max_tokens, chatbot], chatbot)
|
| 90 |
+
user_prompt.submit(enhance_prompt, [user_prompt, temperature, max_tokens, chatbot], chatbot)
|
| 91 |
+
clear_btn.click(lambda: [], None, chatbot)
|
| 92 |
+
|
| 93 |
+
gr.Markdown(
|
| 94 |
+
"""
|
| 95 |
+
---
|
| 96 |
+
💡 **Tips:**
|
| 97 |
+
- Works best with short, descriptive prompts (e.g., "a cat sitting on a chair")
|
| 98 |
+
- Increase *Temperature* for more creative output.
|
| 99 |
+
"""
|
| 100 |
+
)
|
| 101 |
+
|
| 102 |
+
|
| 103 |
+
# ============================================================
|
| 104 |
+
# 5️⃣ Launch App
|
| 105 |
+
# ============================================================
|
| 106 |
+
if __name__ == "__main__":
|
| 107 |
+
demo.launch(show_error=True)
|