Doja2002 commited on
Commit
e57e0b7
·
verified ·
1 Parent(s): 72485ae

updated app.py

Browse files
Files changed (1) hide show
  1. app.py +56 -17
app.py CHANGED
@@ -1,36 +1,75 @@
1
  import gradio as gr
2
  from transformers import pipeline
3
 
4
- # Use a specific image classification model from Hugging Face
5
  MODEL_ID = "nateraw/food"
6
  clf = pipeline("image-classification", model=MODEL_ID)
7
 
8
- def predict(img):
9
  """
10
- Classifies an input image and returns the top 3 predicted labels
11
- with their corresponding scores.
12
  """
13
- if img is None:
14
  return "Please upload an image."
15
 
16
  # Perform classification
17
- out = clf(img)
18
 
19
  # Sort results and get top 3
20
  out = sorted(out, key=lambda r: r["score"], reverse=True)[:3]
21
 
22
- # Format the output as a dictionary for Gradio's Label component
23
- return {r["label"]: float(r["score"]) for r in out}
 
24
 
25
- # Create the Gradio interface without examples
26
- iface = gr.Interface(
27
- fn=predict,
28
- inputs=gr.Image(type="pil", label="Upload image"),
29
- outputs=gr.Label(num_top_classes=3),
30
- title="Image Classifier",
31
- description="Upload an image to see its top 3 predicted classifications.",
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32
  )
33
 
34
- # Launch the interface
35
  if __name__ == "__main__":
36
- iface.launch()
 
1
  import gradio as gr
2
  from transformers import pipeline
3
 
4
+ # Load the image classification model
5
  MODEL_ID = "nateraw/food"
6
  clf = pipeline("image-classification", model=MODEL_ID)
7
 
8
+ def classify_image(image):
9
  """
10
+ Classifies an image and formats the results as a string message.
 
11
  """
12
+ if image is None:
13
  return "Please upload an image."
14
 
15
  # Perform classification
16
+ out = clf(image)
17
 
18
  # Sort results and get top 3
19
  out = sorted(out, key=lambda r: r["score"], reverse=True)[:3]
20
 
21
+ # Format the results into a human-readable string for the chatbot
22
+ results = [f"- {r['label']}: {r['score']:.2f}" for r in out]
23
+ return "I've classified the image! Here are the top predictions:\n" + "\n".join(results)
24
 
25
+ def respond(
26
+ message,
27
+ history,
28
+ # The ChatInterface expects a message, but we'll ignore it for this
29
+ # image classification task and use our image input instead.
30
+ # The "message" here will be None because we've set `gr.Image`
31
+ # as the input.
32
+ ):
33
+ """
34
+ This function handles the chatbot's response.
35
+ It receives the image and history, then calls our classification function.
36
+ """
37
+ # The message input from the chatbot interface will be None here
38
+ # since our primary input is the image. We can get the image from `history`.
39
+ if not history:
40
+ return "" # Don't respond if there's no history or image
41
+
42
+ # Extract the image from the last user message in the history
43
+ # The input is the image, but the chatbot wraps it as a message
44
+ last_user_message = history[-1][0]
45
+
46
+ # We must call the classification function with the image
47
+ classification_result = classify_image(last_user_message)
48
+
49
+ # Yield the result as the chatbot's response
50
+ yield classification_result
51
+
52
+ # Create the ChatInterface
53
+ chatbot = gr.ChatInterface(
54
+ fn=respond,
55
+ # The input component must be specified for the user to upload an image.
56
+ # The message component will be hidden.
57
+ chatbot=gr.Chatbot(height=500),
58
+ textbox=gr.Textbox(
59
+ placeholder="Upload an image to get a classification!",
60
+ visible=False
61
+ ),
62
+ # Use gr.Image for the user input
63
+ submit_btn="Classify Image",
64
+ clear_btn="Clear History",
65
+ additional_inputs=[
66
+ gr.Image(type="pil", label="Upload an Image"),
67
+ ],
68
+ title="Image Classification Chatbot",
69
+ description="Upload an image and I'll classify it for you!",
70
+ examples=[]
71
  )
72
 
73
+ # Launch the demo
74
  if __name__ == "__main__":
75
+ chatbot.launch()