haxerwddle commited on
Commit
f8a801c
·
1 Parent(s): 8156c42

prompt model change

Browse files
Files changed (1) hide show
  1. app.py +16 -64
app.py CHANGED
@@ -9,9 +9,8 @@ import torch
9
  from transformers import (
10
  AutoFeatureExtractor,
11
  AutoModelForImageClassification,
12
- AutoTokenizer,
13
- AutoModelForCausalLM,
14
- pipeline # <-- added
15
  )
16
 
17
  # ------------------ LOAD CLASSIFIER ------------------
@@ -35,71 +34,24 @@ def classify_image(image):
35
  return {id2label[i]: float(probs[i]) for i in top3}
36
 
37
 
38
- # ------------------ LOAD TinyLlama ------------------
39
- tiny_model = "TinyLlama/TinyLlama-1.1B-Chat-v1.0"
 
40
 
41
- tokenizer = AutoTokenizer.from_pretrained(tiny_model)
42
- chat_model = AutoModelForCausalLM.from_pretrained(
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
- def clean_chat_output(full_text):
56
- # Split at the last assistant tag (TinyLlama uses <|assistant|>)
57
- if "<|assistant|>" in full_text:
58
- full_text = full_text.split("<|assistant|>")[-1]
59
-
60
- # Remove any leftover "Item:" echoes
61
- lines = full_text.strip().split("\n")
62
- if lines[0].lower().startswith("item:"):
63
- lines = lines[1:]
64
-
65
- return "\n".join(lines).strip()
66
-
67
-
68
- def explain_recycling(label):
69
- system_msg = {
70
- "role": "system",
71
- "content": (
72
- "You are an expert in waste sorting. "
73
- "You ALWAYS answer using exactly two bullet points:\n"
74
- "• Recycling type: <one short category>\n"
75
- "• Disposal: <one clear correct sentence>\n"
76
- "No extra text, no introductions, no explanations."
77
- )
78
- }
79
-
80
- user_msg = {
81
- "role": "user",
82
- "content": f"Item: {label}\nReturn the two bullet points now."
83
- }
84
-
85
- messages = [system_msg, user_msg]
86
-
87
- # Chat template
88
- prompt = tokenizer.apply_chat_template(
89
- messages,
90
- tokenize=False,
91
- add_generation_prompt=True
92
- )
93
 
94
- # Use the pipeline (fixed)
95
- outputs = pipe(
96
- prompt,
97
- do_sample=False # deterministic to avoid repeating instructions
98
  )
99
 
100
- raw = outputs[0]["generated_text"]
101
- return clean_chat_output(raw)
102
-
103
 
104
  # ------------------ PIPELINE ------------------
105
  def full_pipeline(image):
@@ -121,7 +73,7 @@ with gr.Blocks() as demo:
121
  explain_output = gr.Textbox(
122
  label="Detailed Recycling & Disposal Advice",
123
  elem_id="explainbox",
124
- lines=4
125
  )
126
 
127
  analyze_btn = gr.Button("Analyze", variant="primary")
 
9
  from transformers import (
10
  AutoFeatureExtractor,
11
  AutoModelForImageClassification,
12
+ T5Tokenizer,
13
+ T5ForConditionalGeneration
 
14
  )
15
 
16
  # ------------------ LOAD CLASSIFIER ------------------
 
34
  return {id2label[i]: float(probs[i]) for i in top3}
35
 
36
 
37
+ # ------------------ LOAD FLAN-T5 ------------------
38
+ tokenizer = T5Tokenizer.from_pretrained("google/flan-t5-large")
39
+ chat_model = T5ForConditionalGeneration.from_pretrained("google/flan-t5-large")
40
 
41
+ def explain_recycling(class_label):
42
+ prompt = f"""
43
+ Give recycling instructions for item {class_label}.
44
+ """
45
 
46
+ inputs = tokenizer(prompt, return_tensors="pt").input_ids
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
47
 
48
+ outputs = chat_model.generate(
49
+ inputs,
50
+ max_length=180,
51
+ do_sample=False
52
  )
53
 
54
+ return tokenizer.decode(outputs[0], skip_special_tokens=True)
 
 
55
 
56
  # ------------------ PIPELINE ------------------
57
  def full_pipeline(image):
 
73
  explain_output = gr.Textbox(
74
  label="Detailed Recycling & Disposal Advice",
75
  elem_id="explainbox",
76
+ lines=18
77
  )
78
 
79
  analyze_btn = gr.Button("Analyze", variant="primary")