riabayonaor commited on
Commit
834d12c
1 Parent(s): 9f33da8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -36
app.py CHANGED
@@ -2,11 +2,17 @@ import streamlit as st
2
  import requests
3
  from PIL import Image
4
  from io import BytesIO
 
 
 
 
 
 
5
 
6
  # Configuración de la API
7
  API_URL = "https://api-inference.huggingface.co/models/riabayonaor/modelo_prediccion_enfermedades_pepinos"
8
  META_LLAMA_API_URL = "https://api-inference.huggingface.co/models/meta-llama/Meta-Llama-3-8B-Instruct"
9
- headers = {"Authorization": "Bearer YOUR_HUGGINGFACE_API_KEY"} # Reemplaza con tu API Key
10
 
11
  def query(image_bytes):
12
  response = requests.post(API_URL, headers=headers, data=image_bytes)
@@ -16,39 +22,45 @@ def llama_query(prompt):
16
  response = requests.post(META_LLAMA_API_URL, headers=headers, json={"inputs": prompt})
17
  return response.json()
18
 
19
- # Interfaz de Streamlit
20
- st.title("Predicción de Enfermedades en Pepinos")
21
- uploaded_file = st.file_uploader("Sube una foto de una planta de pepino o un pepino", type=["jpg", "jpeg", "png"])
22
-
23
- if uploaded_file is not None:
24
- image = Image.open(uploaded_file)
25
- st.image(image, caption='Imagen subida.', use_column_width=True)
26
- st.write("Clasificando...")
27
-
28
- # Convertir la imagen a bytes
29
- img_byte_arr = BytesIO()
30
- image.save(img_byte_arr, format='PNG')
31
- img_byte_arr = img_byte_arr.getvalue()
32
-
33
- # Enviar la imagen al modelo de Hugging Face
34
- predictions = query(img_byte_arr)
35
-
36
- if "error" not in predictions:
37
- # Suponiendo que las predicciones están en el formato [{label: "label1", score: 0.95}, {label: "label2", score: 0.05}]
38
- top_prediction = max(predictions, key=lambda x: x["score"])
39
- st.write(f"Predicción principal: {top_prediction['label']} con confianza {top_prediction['score']:.2f}")
40
-
41
- # Usar la etiqueta principal para el modelo de Meta Llama
42
- prompt = f"Esta enfermedad es {top_prediction['label']}. Explica qué es y sugiere posibles insecticidas o soluciones."
43
-
44
- # Llamar al modelo Meta Llama
45
- llama_response = llama_query(prompt)
46
-
47
- if "error" not in llama_response:
48
- explanation = llama_response[0]["generated_text"]
49
- st.write("Explicación y posibles soluciones:")
50
- st.write(explanation)
 
 
 
 
 
51
  else:
52
- st.write("No se pudo obtener una respuesta del modelo Meta Llama.")
53
- else:
54
- st.write("No se pudo clasificar la imagen.")
 
 
2
  import requests
3
  from PIL import Image
4
  from io import BytesIO
5
+ from dotenv import load_dotenv
6
+ import os
7
+
8
+ # Cargar variables de entorno desde el archivo .env
9
+ load_dotenv()
10
+ huggingface_api_key = os.getenv("HUGGINGFACE_API_KEY")
11
 
12
  # Configuración de la API
13
  API_URL = "https://api-inference.huggingface.co/models/riabayonaor/modelo_prediccion_enfermedades_pepinos"
14
  META_LLAMA_API_URL = "https://api-inference.huggingface.co/models/meta-llama/Meta-Llama-3-8B-Instruct"
15
+ headers = {"Authorization": f"Bearer {huggingface_api_key}"}
16
 
17
  def query(image_bytes):
18
  response = requests.post(API_URL, headers=headers, data=image_bytes)
 
22
  response = requests.post(META_LLAMA_API_URL, headers=headers, json={"inputs": prompt})
23
  return response.json()
24
 
25
+ def main():
26
+ st.set_page_config(page_title="Predicción de Enfermedades en Pepinos")
27
+ st.title("Predicción de Enfermedades en Pepinos")
28
+ st.write("Sube una foto de una planta de pepino o un pepino para clasificar posibles enfermedades y obtener soluciones.")
29
+
30
+ uploaded_file = st.file_uploader("Sube una foto de una planta de pepino o un pepino", type=["jpg", "jpeg", "png"])
31
+
32
+ if uploaded_file is not None:
33
+ image = Image.open(uploaded_file)
34
+ st.image(image, caption='Imagen subida.', use_column_width=True)
35
+ st.write("Clasificando...")
36
+
37
+ # Convertir la imagen a bytes
38
+ img_byte_arr = BytesIO()
39
+ image.save(img_byte_arr, format='PNG')
40
+ img_byte_arr = img_byte_arr.getvalue()
41
+
42
+ # Enviar la imagen al modelo de Hugging Face
43
+ predictions = query(img_byte_arr)
44
+
45
+ if "error" not in predictions:
46
+ # Suponiendo que las predicciones están en el formato [{label: "label1", score: 0.95}, {label: "label2", score: 0.05}]
47
+ top_prediction = max(predictions, key=lambda x: x["score"])
48
+ st.write(f"Predicción principal: {top_prediction['label']} con confianza {top_prediction['score']:.2f}")
49
+
50
+ # Usar la etiqueta principal para el modelo de Meta Llama
51
+ prompt = f"Esta enfermedad es {top_prediction['label']}. Explica qué es y sugiere posibles insecticidas o soluciones."
52
+
53
+ # Llamar al modelo Meta Llama
54
+ llama_response = llama_query(prompt)
55
+
56
+ if "error" not in llama_response:
57
+ explanation = llama_response[0]["generated_text"]
58
+ st.write("Explicación y posibles soluciones:")
59
+ st.write(explanation)
60
+ else:
61
+ st.write("No se pudo obtener una respuesta del modelo Meta Llama.")
62
  else:
63
+ st.write("No se pudo clasificar la imagen.")
64
+
65
+ if __name__ == "__main__":
66
+ main()