haxerwddle commited on
Commit
e82dffa
·
1 Parent(s): 7cfff02

Add chat model

Browse files
Files changed (1) hide show
  1. app.py +34 -4
app.py CHANGED
@@ -1,13 +1,14 @@
1
  import gradio as gr
2
- from transformers import AutoFeatureExtractor, AutoModelForImageClassification
 
3
  import torch
 
4
 
5
- # Load model + extractor
6
  model_name = "Aalaa/Fine_tuned_Vit_trash_classification"
7
 
8
  feature_extractor = AutoFeatureExtractor.from_pretrained(model_name)
9
  model = AutoModelForImageClassification.from_pretrained(model_name)
10
-
11
  # Label mapping
12
  id2label = model.config.id2label
13
 
@@ -29,12 +30,41 @@ def predict(image):
29
 
30
  return result
31
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32
 
33
  demo = gr.Interface(
34
  fn=predict,
35
  inputs=gr.Image(type="pil"),
36
  outputs=gr.Label(num_top_classes=3),
37
- title="AI Waste Classifier (ViT)"
38
  )
39
 
40
  demo.launch()
 
1
  import gradio as gr
2
+ from transformers import AutoFeatureExtractor, AutoModelForImageClassification #for classifer model
3
+
4
  import torch
5
+ from transformers import T5Tokenizer, T5ForConditionalGeneration #for chat model
6
 
7
+ # ------------------ Load classifier model + extractor ------------------
8
  model_name = "Aalaa/Fine_tuned_Vit_trash_classification"
9
 
10
  feature_extractor = AutoFeatureExtractor.from_pretrained(model_name)
11
  model = AutoModelForImageClassification.from_pretrained(model_name)
 
12
  # Label mapping
13
  id2label = model.config.id2label
14
 
 
30
 
31
  return result
32
 
33
+ #------------------ Load flan-t5 chat model------------------
34
+ tokenizer = T5Tokenizer.from_pretrained("google/flan-t5-base")
35
+ chat_model = T5ForConditionalGeneration.from_pretrained("google/flan-t5-base")
36
+
37
+ def explain_recycling(class_label):
38
+ prompt = f"""
39
+ Waste category: {class_label}
40
+
41
+ Explain the followings:
42
+ 1. What recycling type this is (e.g., plastic, organic, glass, paper, metal).
43
+ 2. How to properly dispose of or recycle it.
44
+
45
+ Use this format:
46
+
47
+ Recycling type: <type>
48
+ Recommendation: <instruction>
49
+ """
50
+
51
+ inputs = tokenizer(prompt, return_tensors="pt").input_ids
52
+
53
+ outputs = chat_model.generate(
54
+ inputs,
55
+ max_length=180,
56
+ do_sample=True,
57
+ temperature=0.7,
58
+ top_p=0.9
59
+ )
60
+
61
+ return tokenizer.decode(outputs[0], skip_special_tokens=True)
62
 
63
  demo = gr.Interface(
64
  fn=predict,
65
  inputs=gr.Image(type="pil"),
66
  outputs=gr.Label(num_top_classes=3),
67
+ title="AI Waste Classifier"
68
  )
69
 
70
  demo.launch()