haxerwddle commited on
Commit
e689198
·
1 Parent(s): f402398

UI change

Browse files
Files changed (1) hide show
  1. app.py +31 -13
app.py CHANGED
@@ -6,6 +6,7 @@ except RuntimeError:
6
 
7
  import gradio as gr
8
  from transformers import (
 
9
  T5Tokenizer,
10
  T5ForConditionalGeneration,
11
  pipeline
@@ -26,26 +27,43 @@ def classify_image(image):
26
  return results
27
 
28
 
29
- # ------------------ LOAD FLAN-T5 ------------------
30
- tokenizer = T5Tokenizer.from_pretrained("google/flan-t5-small")
31
- chat_model = T5ForConditionalGeneration.from_pretrained("google/flan-t5-small")
 
32
 
33
  def explain_recycling(class_label):
34
- prompt = f"""
35
- Provide recycling Instructions for {class_label}:
36
- Provide waste reduction tips for {class_label}:
37
- """
38
-
39
- inputs = tokenizer(prompt, return_tensors="pt").input_ids
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40
 
41
  outputs = chat_model.generate(
42
- inputs,
43
- max_length=250,
44
  do_sample=True,
45
- top_p=0.9,
 
46
  )
 
47
 
48
- return tokenizer.decode(outputs[0])
49
 
50
 
51
  # ------------------ PIPELINE ------------------
 
6
 
7
  import gradio as gr
8
  from transformers import (
9
+ AutoTokenizer, AutoModelForCausalLM,
10
  T5Tokenizer,
11
  T5ForConditionalGeneration,
12
  pipeline
 
27
  return results
28
 
29
 
30
+ # ------------------ LOAD CHAT MODEL ------------------
31
+ model_name = "TinyLlama/TinyLlama-1.1B-Chat-v1.0"
32
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
33
+ chat_model = AutoModelForCausalLM.from_pretrained(model_name)
34
 
35
  def explain_recycling(class_label):
36
+ system_msg = {
37
+ "role": "system",
38
+ "content": (
39
+ "You are an expert in waste sorting. "
40
+ "You ALWAYS answer using exactly two bullet points:\n"
41
+ "• Recycling type: <one short category>\n"
42
+ "• Disposal: <clear correct sentence>\n"
43
+ "No extra text, no introductions, no explanations."
44
+ )
45
+ }
46
+ user_msg = {
47
+ "role": "user",
48
+ "content": f"Item: {class_label}\nReturn the two bullet points now."
49
+ }
50
+ messages = [system_msg, user_msg]
51
+
52
+ prompt = tokenizer.apply_chat_template(
53
+ messages,
54
+ tokenize=False,
55
+ add_generation_prompt=True
56
+ )
57
 
58
  outputs = chat_model.generate(
59
+ prompt,
 
60
  do_sample=True,
61
+ temperature=0.3,
62
+ top_p=0.9
63
  )
64
+ text = tokenizer.decode(outputs[0], skip_special_tokens=True)
65
 
66
+ return outputs[0]["generated_text"]
67
 
68
 
69
  # ------------------ PIPELINE ------------------