Spaces:
Sleeping
Sleeping
riabayonaor
commited on
Commit
•
4d53bee
1
Parent(s):
bb9b29e
Update app.py
Browse files
app.py
CHANGED
@@ -1,43 +1,68 @@
|
|
1 |
-
import
|
2 |
import requests
|
|
|
|
|
|
|
3 |
|
4 |
-
#
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
response
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
import requests
|
3 |
+
from PIL import Image
|
4 |
+
from io import BytesIO
|
5 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
6 |
|
7 |
+
# Configuración de la API
|
8 |
+
API_URL = "https://api-inference.huggingface.co/models/riabayonaor/modelo_prediccion_enfermedades_pepinos"
|
9 |
+
|
10 |
+
# Cargar el modelo y el tokenizador de BLOOM
|
11 |
+
tokenizer = AutoTokenizer.from_pretrained("bigscience/bloom")
|
12 |
+
model = AutoModelForCausalLM.from_pretrained("bigscience/bloom")
|
13 |
+
|
14 |
+
def query(image_bytes):
|
15 |
+
response = requests.post(API_URL, data=image_bytes)
|
16 |
+
if response.status_code != 200:
|
17 |
+
st.error(f"Error al clasificar la imagen: {response.status_code}")
|
18 |
+
return None
|
19 |
+
return response.json()
|
20 |
+
|
21 |
+
def bloom_query(prompt):
|
22 |
+
inputs = tokenizer(prompt, return_tensors="pt")
|
23 |
+
outputs = model.generate(inputs["input_ids"], max_length=500, num_return_sequences=1)
|
24 |
+
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
25 |
+
return response
|
26 |
+
|
27 |
+
def main():
|
28 |
+
st.set_page_config(page_title="Predicción de Enfermedades en Pepinos")
|
29 |
+
st.title("Predicción de Enfermedades en Pepinos")
|
30 |
+
st.write("Sube una foto de una planta de pepino o un pepino para clasificar posibles enfermedades y obtener soluciones.")
|
31 |
+
|
32 |
+
uploaded_file = st.file_uploader("Sube una foto de una planta de pepino o un pepino", type=["jpg", "jpeg", "png"])
|
33 |
+
|
34 |
+
if uploaded_file is not None:
|
35 |
+
image = Image.open(uploaded_file)
|
36 |
+
st.image(image, caption='Imagen subida.', use_column_width=True)
|
37 |
+
st.write("Clasificando...")
|
38 |
+
|
39 |
+
# Convertir la imagen a bytes
|
40 |
+
img_byte_arr = BytesIO()
|
41 |
+
image.save(img_byte_arr, format='PNG')
|
42 |
+
img_byte_arr = img_byte_arr.getvalue()
|
43 |
+
|
44 |
+
# Enviar la imagen al modelo de Hugging Face
|
45 |
+
predictions = query(img_byte_arr)
|
46 |
+
|
47 |
+
if predictions is not None:
|
48 |
+
if "error" not in predictions:
|
49 |
+
# Suponiendo que las predicciones están en el formato [{label: "Downy Mildew", score: 0.95}, {label: "Fresh Cucumber", score: 0.05}]
|
50 |
+
top_prediction = max(predictions, key=lambda x: x["score"])
|
51 |
+
class_label = top_prediction['label']
|
52 |
+
st.write(f"Predicción principal: {class_label} con confianza {top_prediction['score']:.2f}")
|
53 |
+
|
54 |
+
# Usar la etiqueta principal para el modelo BLOOM
|
55 |
+
prompt = f"Esta enfermedad es {class_label}. Explica qué es y sugiere posibles insecticidas o soluciones en español."
|
56 |
+
|
57 |
+
# Llamar al modelo BLOOM
|
58 |
+
explanation = bloom_query(prompt)
|
59 |
+
|
60 |
+
st.write(f"Esta enfermedad es {class_label}:")
|
61 |
+
st.write(explanation)
|
62 |
+
else:
|
63 |
+
st.write("No se pudo clasificar la imagen.")
|
64 |
+
else:
|
65 |
+
st.write("No se pudo clasificar la imagen.")
|
66 |
+
|
67 |
+
if __name__ == "__main__":
|
68 |
+
main()
|