nikosteskos commited on
Commit
0e2e09d
·
verified ·
1 Parent(s): eb0273f

Upload 2 files

Browse files
Files changed (2) hide show
  1. app_gradio.py +79 -0
  2. requirements_hf.txt +5 -0
app_gradio.py ADDED
@@ -0,0 +1,79 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import gradio as gr
3
+ import torch
4
+ from PIL import Image
5
+ from fastai.learner import load_learner
6
+
7
+ # Model loading function
8
+ def load_model():
9
+ model_path = 'models/jimi_classifier'
10
+ try:
11
+ if os.path.isdir(model_path):
12
+ learn = load_learner(model_path)
13
+ return learn
14
+ except Exception as e:
15
+ print(f"Error loading model: {e}")
16
+
17
+ # Fallback stub model for testing
18
+ class StubLearner:
19
+ def predict(self, img):
20
+ import random
21
+ is_jimis = random.choice([True, False])
22
+ pred_class = 'jimis' if is_jimis else 'not_jimis'
23
+ pred_idx = 0 if is_jimis else 1
24
+ probs = torch.tensor([0.8, 0.2]) if is_jimis else torch.tensor([0.2, 0.8])
25
+ return pred_class, pred_idx, probs
26
+
27
+ return StubLearner()
28
+
29
+ # Prediction function
30
+ def predict_image(img):
31
+ if img is None:
32
+ return "Please upload an image", 0
33
+
34
+ model = load_model()
35
+
36
+ try:
37
+ # Process the image
38
+ pred_class, pred_idx, probs = model.predict(img)
39
+ confidence = float(probs[pred_idx]) * 100
40
+
41
+ result = "Jimis" if str(pred_class).lower() == "jimis" else "Not Jimis"
42
+ return result, round(confidence, 2)
43
+ except Exception as e:
44
+ print(f"Error during prediction: {e}")
45
+ import traceback
46
+ traceback.print_exc()
47
+ return f"Error processing image: {str(e)}", 0
48
+
49
+ # Example images for the demo
50
+ examples = [
51
+ # You can add example image paths here if you have them
52
+ ]
53
+
54
+ # Create the Gradio interface
55
+ demo = gr.Interface(
56
+ fn=predict_image,
57
+ inputs=gr.Image(type="pil", label="Upload an image"),
58
+ outputs=[
59
+ gr.Label(label="Prediction"),
60
+ gr.Number(label="Confidence (%)")
61
+ ],
62
+ title="Jimis Classifier",
63
+ description="Upload an image to check if it contains Jimis",
64
+ examples=examples,
65
+ article="""
66
+ ## How it works
67
+
68
+ This application uses a machine learning model trained to recognize Jimis in images.
69
+ The model was trained on a custom dataset of Jimis and non-Jimis images using the
70
+ fastai library and a ResNet architecture.
71
+
72
+ Simply upload an image, and the model will tell you whether it contains Jimis and
73
+ how confident it is about its prediction.
74
+ """
75
+ )
76
+
77
+ # Launch the app
78
+ if __name__ == "__main__":
79
+ demo.launch()
requirements_hf.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ gradio>=4.0.0
2
+ fastai>=2.7.9
3
+ torch>=1.12.0
4
+ torchvision>=0.13.0
5
+ Pillow>=9.0.0