haxerwddle commited on
Commit
5091547
·
1 Parent(s): 87f424d
Files changed (1) hide show
  1. app.py +12 -21
app.py CHANGED
@@ -7,10 +7,7 @@ from pydantic import BaseModel
7
 
8
  import random
9
  import torch
10
- from transformers import ( T5Tokenizer, T5ForConditionalGeneration,
11
- AutoTokenizer, AutoModelForCausalLM,
12
- pipeline
13
- )
14
 
15
  # =========================
16
  # FASTAPI APP
@@ -43,40 +40,34 @@ def classify_image(image):
43
  # ------------------ LOAD CHAT MODEL
44
  tiny_model = "google/flan-t5-small"
45
 
 
46
  tokenizer = T5Tokenizer.from_pretrained(tiny_model)
47
- chat_model = T5ForConditionalGeneration.from_pretrained(
48
- tiny_model,
49
- device_map="cpu"
50
- )
51
 
 
52
  pipe = pipeline(
53
- "text2text-generation",
54
  model=chat_model,
55
  tokenizer=tokenizer,
 
56
  max_new_tokens=80
57
  )
58
 
59
  def explain_recycling(class_label):
 
60
  prompt = (
61
  "You are an expert in waste sorting. "
62
- "You ALWAYS answer using exactly two bullet points:\n"
63
  "• Recycling type: <Item category>\n"
64
  "• Disposal: <clear, detailed correct sentence>\n"
65
  f"Item: {class_label}\n"
66
  "Return the two bullet points now."
67
  )
68
 
69
- output = pipe(prompt)[0]["generated_text"]
70
- return output.strip()
71
-
72
- pipe = pipeline(
73
- "text-generation",
74
- model=chat_model,
75
- tokenizer=tokenizer,
76
- device_map="auto",
77
- max_new_tokens=80
78
- )
79
-
80
 
81
 
82
  # =========================
 
7
 
8
  import random
9
  import torch
10
+ from transformers import T5Tokenizer, T5ForConditionalGeneration, pipeline
 
 
 
11
 
12
  # =========================
13
  # FASTAPI APP
 
40
  # ------------------ LOAD CHAT MODEL
41
  tiny_model = "google/flan-t5-small"
42
 
43
+ # tokenizer + model
44
  tokenizer = T5Tokenizer.from_pretrained(tiny_model)
45
+ chat_model = T5ForConditionalGeneration.from_pretrained(tiny_model)
 
 
 
46
 
47
+ # Use text2text-generation for T5-style models
48
  pipe = pipeline(
49
+ task="text2text-generation",
50
  model=chat_model,
51
  tokenizer=tokenizer,
52
+ device=-1, # -1 = CPU (safe)
53
  max_new_tokens=80
54
  )
55
 
56
  def explain_recycling(class_label):
57
+ # Construct a simple single-string prompt for T5
58
  prompt = (
59
  "You are an expert in waste sorting. "
60
+ "Always answer using exactly two bullet points:\n"
61
  "• Recycling type: <Item category>\n"
62
  "• Disposal: <clear, detailed correct sentence>\n"
63
  f"Item: {class_label}\n"
64
  "Return the two bullet points now."
65
  )
66
 
67
+ outputs = pipe(prompt, max_new_tokens=80, do_sample=False)
68
+ # outputs is a list of dicts: [{"generated_text": "..."}]
69
+ text = outputs[0].get("generated_text", "").strip()
70
+ return text
 
 
 
 
 
 
 
71
 
72
 
73
  # =========================