haxerwddle commited on
Commit
e93c8ed
·
1 Parent(s): 85ad2da

prompt model change

Browse files
Files changed (1) hide show
  1. app.py +43 -25
app.py CHANGED
@@ -10,7 +10,8 @@ from transformers import (
10
  AutoFeatureExtractor,
11
  AutoModelForImageClassification,
12
  AutoTokenizer,
13
- AutoModelForCausalLM
 
14
  )
15
 
16
  # ------------------ LOAD CLASSIFIER ------------------
@@ -42,33 +43,50 @@ chat_model = AutoModelForCausalLM.from_pretrained(
42
  tiny_model,
43
  torch_dtype=torch.float32)
44
 
 
 
 
 
 
 
 
 
 
 
45
  def explain_recycling(label):
46
- prompt = f"""
47
- You are an expert in waste sorting.
48
- Return ONLY the following 2 bullet points:
49
-
50
- Recycling type: (one short category)
51
- Disposal: (1 clear and correct sentence)
52
-
53
- Example:
54
- Item: Glass bottle
55
- • Recycling type: Glass recycling
56
- • Disposal: Rinse and put in the glass-recycling bin.
57
-
58
- Now answer for:
59
- Item: {label}
60
- """
61
-
62
- inputs = tokenizer(prompt, return_tensors="pt")
63
- outputs = chat_model.generate(
64
- **inputs,
65
- max_new_tokens=80,
66
- temperature=0.3,
67
- do_sample=True,
68
- top_p=0.9
69
  )
70
 
71
- return tokenizer.decode(outputs[0], skip_special_tokens=True)
 
 
 
 
 
 
 
72
 
73
  # ------------------ PIPELINE ------------------
74
  def full_pipeline(image):
 
10
  AutoFeatureExtractor,
11
  AutoModelForImageClassification,
12
  AutoTokenizer,
13
+ AutoModelForCausalLM,
14
+ pipeline # <-- added
15
  )
16
 
17
  # ------------------ LOAD CLASSIFIER ------------------
 
43
  tiny_model,
44
  torch_dtype=torch.float32)
45
 
46
+ # ✅ Create pipeline (this was missing)
47
+ pipe = pipeline(
48
+ "text-generation",
49
+ model=chat_model,
50
+ tokenizer=tokenizer,
51
+ device_map="auto",
52
+ max_new_tokens=200
53
+ )
54
+
55
+
56
  def explain_recycling(label):
57
+ system_msg = {
58
+ "role": "system",
59
+ "content": (
60
+ "You are an expert in waste sorting. "
61
+ "You ALWAYS answer using exactly two bullet points:\n"
62
+ "Recycling type: <one short category>\n"
63
+ "• Disposal: <one clear correct sentence>\n"
64
+ "No extra text, no introductions, no explanations."
65
+ )
66
+ }
67
+
68
+ user_msg = {
69
+ "role": "user",
70
+ "content": f"Item: {label}\nReturn the two bullet points now."
71
+ }
72
+
73
+ messages = [system_msg, user_msg]
74
+
75
+ # Chat template
76
+ prompt = tokenizer.apply_chat_template(
77
+ messages,
78
+ tokenize=False,
79
+ add_generation_prompt=True
80
  )
81
 
82
+ # Use the pipeline (fixed)
83
+ outputs = pipe(
84
+ prompt,
85
+ do_sample=False # deterministic to avoid repeating instructions
86
+ )
87
+
88
+ return outputs[0]["generated_text"]
89
+
90
 
91
  # ------------------ PIPELINE ------------------
92
  def full_pipeline(image):