eriksarriegui commited on
Commit
b8090b5
·
verified ·
1 Parent(s): 9698646

Upload 4 files

Browse files
Files changed (4) hide show
  1. Model.py +10 -0
  2. app.py +51 -0
  3. description.md +3 -0
  4. requirements.txt +6 -0
Model.py ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import pipeline
2
+
3
+ classifier = pipeline("image-classification", "JJNeila/fish_classification")
4
+
5
+ class FishModel:
6
+ def __init__(self):
7
+ self.classifier = classifier
8
+
9
+ def get_fish_species(self, image):
10
+ return self.classifier(image)
app.py ADDED
@@ -0,0 +1,51 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import requests
3
+ from PIL import Image
4
+ import io
5
+
6
+ from Model import FishModel
7
+
8
+ Classifier = FishModel()
9
+
10
+ def inference(image_input, url_input) -> dict[str, float]:
11
+ image = load_image(image_input, url_input)
12
+
13
+ result = Classifier.get_fish_species(image)
14
+
15
+ data = dict()
16
+
17
+ for element in result:
18
+ data[element['label']] = round(element['score'], 2)
19
+
20
+ return data # {"dog" : 30, "cat" : 70}
21
+
22
+ def load_image(image_input, url_input):
23
+ if image_input is not None:
24
+ return image_input
25
+
26
+ elif url_input:
27
+ try:
28
+ response = requests.get(url_input)
29
+ response.raise_for_status()
30
+ return Image.open(io.BytesIO(response.content))
31
+
32
+ except Exception as e:
33
+ raise gr.Error(f"No se pudo cargar la imagen desde la URL. Error: {e}")
34
+
35
+
36
+ demo = gr.Interface(
37
+ title="🐳📸 Fish Classification",
38
+ description=open("description.md", "r", encoding="utf8").read(),
39
+ fn=inference,
40
+ inputs=[
41
+ gr.Image(label="Sube una imagen", type="pil"),
42
+ gr.Textbox(label="O pega una URL de imagen aquí")
43
+ ],
44
+ outputs=gr.Label(label="Resultado"),
45
+ # examples=[
46
+ # [None, "https://gradio-builds.s3.amazonaws.com/demo-files/goldfish.jpg"],
47
+ # ["images/salmon.jpg", None]
48
+ # ]
49
+ )
50
+
51
+ demo.launch()
description.md ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ Este proyecto es para clasificar peces en las siguientes categorías:
2
+ * Grandes
3
+ * Pequeños
requirements.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ mpmath
2
+ transformers
3
+ gradio
4
+ requests
5
+ PIL
6
+ io