Update app_cpu.py
Browse files- app_cpu.py +19 -8
app_cpu.py
CHANGED
|
@@ -1,18 +1,28 @@
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
|
|
|
|
| 3 |
|
| 4 |
# =========================================================
|
| 5 |
-
#
|
| 6 |
# =========================================================
|
| 7 |
MODEL_ID = "gokaygokay/prompt-enhancer-gemma-3-270m-it"
|
| 8 |
-
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
pipe = pipeline("text-generation", model=model, tokenizer=tokenizer)
|
| 11 |
|
| 12 |
# =========================================================
|
| 13 |
-
# Define
|
| 14 |
# =========================================================
|
| 15 |
def enhance_prompt(prompt: str):
|
|
|
|
| 16 |
if not prompt.strip():
|
| 17 |
return "⚠️ Please enter a prompt to enhance."
|
| 18 |
|
|
@@ -27,13 +37,14 @@ def enhance_prompt(prompt: str):
|
|
| 27 |
return result.strip()
|
| 28 |
|
| 29 |
# =========================================================
|
| 30 |
-
# Gradio UI
|
| 31 |
# =========================================================
|
| 32 |
with gr.Blocks(theme=gr.themes.Soft(), title="Prompt Enhancer ✨") as demo:
|
| 33 |
gr.Markdown(
|
| 34 |
"""
|
| 35 |
# ✨ Prompt Enhancer — Gemma 3 270M IT
|
| 36 |
-
|
|
|
|
| 37 |
"""
|
| 38 |
)
|
| 39 |
|
|
@@ -57,7 +68,7 @@ with gr.Blocks(theme=gr.themes.Soft(), title="Prompt Enhancer ✨") as demo:
|
|
| 57 |
gr.Markdown(
|
| 58 |
"""
|
| 59 |
---
|
| 60 |
-
🧠 **
|
| 61 |
- “a futuristic city at sunset”
|
| 62 |
- “a woman reading under a tree”
|
| 63 |
- “a magical forest with glowing mushrooms”
|
|
@@ -65,7 +76,7 @@ with gr.Blocks(theme=gr.themes.Soft(), title="Prompt Enhancer ✨") as demo:
|
|
| 65 |
)
|
| 66 |
|
| 67 |
# =========================================================
|
| 68 |
-
# Launch
|
| 69 |
# =========================================================
|
| 70 |
if __name__ == "__main__":
|
| 71 |
demo.launch()
|
|
|
|
| 1 |
+
import os
|
| 2 |
import gradio as gr
|
| 3 |
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
|
| 4 |
+
from huggingface_hub import snapshot_download
|
| 5 |
|
| 6 |
# =========================================================
|
| 7 |
+
# 1️⃣ Download model to local cache once
|
| 8 |
# =========================================================
|
| 9 |
MODEL_ID = "gokaygokay/prompt-enhancer-gemma-3-270m-it"
|
| 10 |
+
print(f"📦 Downloading model {MODEL_ID} ...")
|
| 11 |
+
model_path = snapshot_download(repo_id=MODEL_ID)
|
| 12 |
+
print(f"✅ Model downloaded to: {model_path}")
|
| 13 |
+
|
| 14 |
+
# =========================================================
|
| 15 |
+
# 2️⃣ Load tokenizer and model from local path
|
| 16 |
+
# =========================================================
|
| 17 |
+
tokenizer = AutoTokenizer.from_pretrained(model_path)
|
| 18 |
+
model = AutoModelForCausalLM.from_pretrained(model_path)
|
| 19 |
pipe = pipeline("text-generation", model=model, tokenizer=tokenizer)
|
| 20 |
|
| 21 |
# =========================================================
|
| 22 |
+
# 3️⃣ Define function to enhance prompt
|
| 23 |
# =========================================================
|
| 24 |
def enhance_prompt(prompt: str):
|
| 25 |
+
"""Enhance and expand a user prompt with more details and context."""
|
| 26 |
if not prompt.strip():
|
| 27 |
return "⚠️ Please enter a prompt to enhance."
|
| 28 |
|
|
|
|
| 37 |
return result.strip()
|
| 38 |
|
| 39 |
# =========================================================
|
| 40 |
+
# 4️⃣ Gradio UI
|
| 41 |
# =========================================================
|
| 42 |
with gr.Blocks(theme=gr.themes.Soft(), title="Prompt Enhancer ✨") as demo:
|
| 43 |
gr.Markdown(
|
| 44 |
"""
|
| 45 |
# ✨ Prompt Enhancer — Gemma 3 270M IT
|
| 46 |
+
Give your idea a creative boost!
|
| 47 |
+
Enter a simple prompt, and this app will enhance it with vivid detail and context.
|
| 48 |
"""
|
| 49 |
)
|
| 50 |
|
|
|
|
| 68 |
gr.Markdown(
|
| 69 |
"""
|
| 70 |
---
|
| 71 |
+
🧠 **Try these examples:**
|
| 72 |
- “a futuristic city at sunset”
|
| 73 |
- “a woman reading under a tree”
|
| 74 |
- “a magical forest with glowing mushrooms”
|
|
|
|
| 76 |
)
|
| 77 |
|
| 78 |
# =========================================================
|
| 79 |
+
# 5️⃣ Launch app
|
| 80 |
# =========================================================
|
| 81 |
if __name__ == "__main__":
|
| 82 |
demo.launch()
|