Ahmed766 commited on
Commit
45354dc
·
verified ·
1 Parent(s): 853796c

Upload app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +53 -0
app.py ADDED
@@ -0,0 +1,53 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from image_classifier import ImageClassifier
3
+ import numpy as np
4
+
5
+ classifier = ImageClassifier()
6
+
7
+ def classify_image(image):
8
+ # Convert Gradio image to the format expected by our classifier
9
+ # Our classifier expects a file path or URL, so we'll save the image temporarily
10
+ import tempfile
11
+ import os
12
+ from PIL import Image
13
+
14
+ # Save the image temporarily
15
+ with tempfile.NamedTemporaryFile(suffix='.jpg', delete=False) as tmp:
16
+ if isinstance(image, np.ndarray):
17
+ # Convert numpy array to PIL Image
18
+ pil_img = Image.fromarray(image.astype('uint8'), 'RGB')
19
+ else:
20
+ pil_img = image
21
+ pil_img.save(tmp.name)
22
+ tmp_path = tmp.name
23
+
24
+ # Classify the image
25
+ try:
26
+ results = classifier.classify_image(tmp_path)
27
+ # Format results for display
28
+ labels = [res['label'] for res in results]
29
+ confidences = [res['probability'] for res in results]
30
+
31
+ # Clean up temporary file
32
+ os.remove(tmp_path)
33
+
34
+ return labels, confidences
35
+ except Exception as e:
36
+ # Clean up temporary file even if there's an error
37
+ os.remove(tmp_path)
38
+ raise e
39
+
40
+ demo = gr.Interface(
41
+ fn=classify_image,
42
+ inputs=gr.Image(type="pil", label="Upload an image for classification"),
43
+ outputs=[
44
+ gr.Label(num_top_classes=5, label="Top Predictions"),
45
+ gr.BarPlot(x="Label", y="Confidence", title="Confidence Scores", width=500, height=300)
46
+ ],
47
+ title="🖼️ Computer Vision Model",
48
+ description="This model performs image classification using a pre-trained ResNet model.",
49
+ examples=[]
50
+ )
51
+
52
+ if __name__ == "__main__":
53
+ demo.launch()