rahul7star commited on
Commit
ac3979b
·
verified ·
1 Parent(s): fd97af7

Update app_low.py

Browse files
Files changed (1) hide show
  1. app_low.py +23 -14
app_low.py CHANGED
@@ -1,47 +1,55 @@
1
  import gradio as gr
2
- from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
3
  import torch
 
4
 
5
  # ============================================================
6
  # 1️⃣ Load model and tokenizer
7
  # ============================================================
8
  MODEL_ID = "gokaygokay/prompt-enhancer-gemma-3-270m-it"
9
 
10
- # Use CPU-friendly settings
11
  device = 0 if torch.cuda.is_available() else -1
12
 
13
  tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
14
  model = AutoModelForCausalLM.from_pretrained(MODEL_ID)
15
 
16
- # Text-generation pipeline
17
  pipe = pipeline(
18
  "text-generation",
19
  model=model,
20
  tokenizer=tokenizer,
21
- device=device, # 0 for GPU, -1 for CPU
22
  )
23
 
24
  # ============================================================
25
- # 2️⃣ Define the generation function
26
  # ============================================================
27
  def enhance_prompt(user_prompt, temperature, max_tokens, chat_history):
28
  chat_history = chat_history or []
29
 
30
- full_prompt = f"Enhance and expand the following prompt with more details and context: {user_prompt}"
 
 
 
 
31
 
 
 
 
 
32
  output = pipe(
33
- full_prompt,
34
  max_new_tokens=int(max_tokens),
35
  temperature=float(temperature),
36
  do_sample=True,
37
- )[0]['generated_text'].strip()
38
 
39
- # Convert to Gradio messages format
40
  chat_history.append({"role": "user", "content": user_prompt})
41
  chat_history.append({"role": "assistant", "content": output})
42
 
43
  return chat_history
44
 
 
45
  # ============================================================
46
  # 3️⃣ Gradio UI
47
  # ============================================================
@@ -49,7 +57,8 @@ with gr.Blocks(title="Prompt Enhancer – Gemma 3 270M", theme=gr.themes.Soft())
49
  gr.Markdown(
50
  """
51
  # ✨ Prompt Enhancer (Gemma 3 270M)
52
- Enter a short prompt, and the model will expand it with extra details, context, and creativity.
 
53
  """
54
  )
55
 
@@ -66,7 +75,7 @@ with gr.Blocks(title="Prompt Enhancer – Gemma 3 270M", theme=gr.themes.Soft())
66
  send_btn = gr.Button("🚀 Enhance Prompt", variant="primary")
67
  clear_btn = gr.Button("🧹 Clear Chat")
68
 
69
- # Bind functions
70
  send_btn.click(enhance_prompt, [user_prompt, temperature, max_tokens, chatbot], chatbot)
71
  user_prompt.submit(enhance_prompt, [user_prompt, temperature, max_tokens, chatbot], chatbot)
72
  clear_btn.click(lambda: [], None, chatbot)
@@ -74,9 +83,9 @@ with gr.Blocks(title="Prompt Enhancer – Gemma 3 270M", theme=gr.themes.Soft())
74
  gr.Markdown(
75
  """
76
  ---
77
- 💡 Tips:
78
- - Works best with short, descriptive prompts (e.g., "A cat sitting on a chair").
79
- - Adjust temperature for creativity: higher = more diverse output.
80
  """
81
  )
82
 
 
1
  import gradio as gr
 
2
  import torch
3
+ from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
4
 
5
  # ============================================================
6
  # 1️⃣ Load model and tokenizer
7
  # ============================================================
8
  MODEL_ID = "gokaygokay/prompt-enhancer-gemma-3-270m-it"
9
 
10
+ # Use GPU if available
11
  device = 0 if torch.cuda.is_available() else -1
12
 
13
  tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
14
  model = AutoModelForCausalLM.from_pretrained(MODEL_ID)
15
 
 
16
  pipe = pipeline(
17
  "text-generation",
18
  model=model,
19
  tokenizer=tokenizer,
20
+ device=device, # 0 for GPU, -1 for CPU
21
  )
22
 
23
  # ============================================================
24
+ # 2️⃣ Define the generation function (chat-template style)
25
  # ============================================================
26
  def enhance_prompt(user_prompt, temperature, max_tokens, chat_history):
27
  chat_history = chat_history or []
28
 
29
+ # Build messages using proper roles
30
+ messages = [
31
+ {"role": "system", "content": "Enhance and expand the following prompt with more details and context:"},
32
+ {"role": "user", "content": user_prompt}
33
+ ]
34
 
35
+ # Use tokenizer chat template to build the input
36
+ prompt = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
37
+
38
+ # Generate output
39
  output = pipe(
40
+ prompt,
41
  max_new_tokens=int(max_tokens),
42
  temperature=float(temperature),
43
  do_sample=True,
44
+ )[0]["generated_text"].strip()
45
 
46
+ # Append conversation to history
47
  chat_history.append({"role": "user", "content": user_prompt})
48
  chat_history.append({"role": "assistant", "content": output})
49
 
50
  return chat_history
51
 
52
+
53
  # ============================================================
54
  # 3️⃣ Gradio UI
55
  # ============================================================
 
57
  gr.Markdown(
58
  """
59
  # ✨ Prompt Enhancer (Gemma 3 270M)
60
+ Enter a short prompt, and the model will **expand it with details and creative context**
61
+ using the Gemma chat-template interface.
62
  """
63
  )
64
 
 
75
  send_btn = gr.Button("🚀 Enhance Prompt", variant="primary")
76
  clear_btn = gr.Button("🧹 Clear Chat")
77
 
78
+ # Bind UI actions
79
  send_btn.click(enhance_prompt, [user_prompt, temperature, max_tokens, chatbot], chatbot)
80
  user_prompt.submit(enhance_prompt, [user_prompt, temperature, max_tokens, chatbot], chatbot)
81
  clear_btn.click(lambda: [], None, chatbot)
 
83
  gr.Markdown(
84
  """
85
  ---
86
+ 💡 **Tips:**
87
+ - Works best with short, descriptive prompts (e.g., "a cat sitting on a chair")
88
+ - Increase *Temperature* for more creative output.
89
  """
90
  )
91