AhmedIbrahim007 commited on
Commit
9c9c79e
1 Parent(s): 6264cf3

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -0
app.py ADDED
@@ -0,0 +1,32 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastai.vision.all import *
2
+
3
+ # Load the pre-trained model
4
+ learn = load_learner('model.pkl')
5
+
6
+ # Define categories and map them to indices
7
+ searches = ['formal', 'casual', 'athletic']
8
+ searches = sorted(searches) # Ensure the categories are in sorted order
9
+ values = [i for i in range(0, len(searches))]
10
+ class_dict = dict(zip(searches, values))
11
+
12
+ def classify_image(image_path):
13
+ # Load the image from the provided path
14
+ img = PILImage.create(image_path)
15
+
16
+ # Make the prediction
17
+ classification, _, probs = learn.predict(img)
18
+
19
+ # Convert the prediction to a confidence dictionary
20
+ confidences = {label: float(probs[i]) for i, label in enumerate(class_dict)}
21
+
22
+ # If classification is not formal, return 'informal'
23
+ if classification != 'formal':
24
+ informal_confidence = sum(confidences[label] for label in class_dict if label != 'formal')
25
+ return {'informal': informal_confidence}
26
+ else:
27
+ return {'formal': confidences['formal']}
28
+
29
+ # Example usage with an image path
30
+ image_path = 'path_to_image.jpg' # Replace with the actual image path
31
+ result = classify_image(image_path)
32
+ print(result)