Dileep7729 commited on
Commit
4d41f6e
·
verified ·
1 Parent(s): bdea63d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -8
app.py CHANGED
@@ -1,6 +1,7 @@
1
  import gradio as gr
2
  from transformers import CLIPModel, CLIPProcessor
3
  from PIL import Image
 
4
 
5
  # Step 1: Load Fine-Tuned Model from Hugging Face Model Hub
6
  model_name = "quadranttechnologies/retail-content-safety-clip-finetuned"
@@ -16,7 +17,35 @@ except Exception as e:
16
  print(f"Error loading the model or processor: {e}")
17
  raise RuntimeError(f"Failed to load model: {e}")
18
 
19
- # Step 2: Define the Inference Function
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
  def classify_image(image):
21
  """
22
  Classify an image as 'safe' or 'unsafe' and return probabilities.
@@ -25,7 +54,7 @@ def classify_image(image):
25
  image (PIL.Image.Image): Uploaded image.
26
 
27
  Returns:
28
- str: Predicted category ("safe" or "unsafe").
29
  dict: Probabilities for "safe" and "unsafe".
30
  """
31
  try:
@@ -52,13 +81,13 @@ def classify_image(image):
52
  print(f"Model outputs: {outputs}")
53
 
54
  # Calculate probabilities
55
- logits_per_image = outputs.logits_per_image # Image-text similarity scores
56
- probs = logits_per_image.softmax(dim=1) # Convert logits to probabilities
57
  print(f"Probabilities: {probs}")
58
 
59
  # Extract probabilities for each category
60
- safe_prob = probs[0][0].item() * 100 # Safe percentage
61
- unsafe_prob = probs[0][1].item() * 100 # Unsafe percentage
62
 
63
  # Determine the predicted category
64
  predicted_category = "safe" if safe_prob > unsafe_prob else "unsafe"
@@ -71,7 +100,7 @@ def classify_image(image):
71
  print(f"Error during classification: {e}")
72
  return f"Error: {str(e)}", {}
73
 
74
- # Step 3: Set Up Gradio Interface
75
  iface = gr.Interface(
76
  fn=classify_image,
77
  inputs=gr.Image(type="pil"),
@@ -83,7 +112,7 @@ iface = gr.Interface(
83
  description="Upload an image to classify it as 'safe' or 'unsafe' with corresponding probabilities.",
84
  )
85
 
86
- # Step 4: Launch Gradio Interface
87
  if __name__ == "__main__":
88
  print("Launching Gradio interface...")
89
  iface.launch()
@@ -107,5 +136,6 @@ if __name__ == "__main__":
107
 
108
 
109
 
 
110
 
111
 
 
1
  import gradio as gr
2
  from transformers import CLIPModel, CLIPProcessor
3
  from PIL import Image
4
+ import requests
5
 
6
  # Step 1: Load Fine-Tuned Model from Hugging Face Model Hub
7
  model_name = "quadranttechnologies/retail-content-safety-clip-finetuned"
 
17
  print(f"Error loading the model or processor: {e}")
18
  raise RuntimeError(f"Failed to load model: {e}")
19
 
20
+ # Step 2: Minimal Test Case to Verify Model and Processor
21
+ try:
22
+ print("Running a minimal test case with the model...")
23
+
24
+ # Test Image URL
25
+ url = "https://huggingface.co/datasets/Narsil/image_dummy/raw/main/parrots.png"
26
+ image = Image.open(requests.get(url, stream=True).raw)
27
+
28
+ # Define test categories
29
+ test_categories = ["safe", "unsafe"]
30
+
31
+ # Process the image
32
+ test_inputs = processor(text=test_categories, images=image, return_tensors="pt", padding=True)
33
+ print(f"Test inputs processed: {test_inputs}")
34
+
35
+ # Perform inference
36
+ test_outputs = model(**test_inputs)
37
+ print(f"Test outputs: {test_outputs}")
38
+
39
+ # Check probabilities
40
+ test_logits = test_outputs.logits_per_image
41
+ test_probs = test_logits.softmax(dim=1)
42
+ print(f"Test probabilities: {test_probs}")
43
+
44
+ except Exception as e:
45
+ print(f"Error during the minimal test case: {e}")
46
+ raise RuntimeError(f"Test case failed: {e}")
47
+
48
+ # Step 3: Define the Inference Function
49
  def classify_image(image):
50
  """
51
  Classify an image as 'safe' or 'unsafe' and return probabilities.
 
54
  image (PIL.Image.Image): Uploaded image.
55
 
56
  Returns:
57
+ str: Predicted category.
58
  dict: Probabilities for "safe" and "unsafe".
59
  """
60
  try:
 
81
  print(f"Model outputs: {outputs}")
82
 
83
  # Calculate probabilities
84
+ logits_per_image = outputs.logits_per_image
85
+ probs = logits_per_image.softmax(dim=1)
86
  print(f"Probabilities: {probs}")
87
 
88
  # Extract probabilities for each category
89
+ safe_prob = probs[0][0].item() * 100
90
+ unsafe_prob = probs[0][1].item() * 100
91
 
92
  # Determine the predicted category
93
  predicted_category = "safe" if safe_prob > unsafe_prob else "unsafe"
 
100
  print(f"Error during classification: {e}")
101
  return f"Error: {str(e)}", {}
102
 
103
+ # Step 4: Set Up Gradio Interface
104
  iface = gr.Interface(
105
  fn=classify_image,
106
  inputs=gr.Image(type="pil"),
 
112
  description="Upload an image to classify it as 'safe' or 'unsafe' with corresponding probabilities.",
113
  )
114
 
115
+ # Step 5: Launch Gradio Interface
116
  if __name__ == "__main__":
117
  print("Launching Gradio interface...")
118
  iface.launch()
 
136
 
137
 
138
 
139
+
140
 
141