shaheer-data commited on
Commit
dfbae18
·
verified ·
1 Parent(s): e4dfe19

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -52
app.py CHANGED
@@ -1,55 +1,33 @@
1
  import streamlit as st
2
- import numpy as np
3
- import tensorflow as tf
4
- from tensorflow.keras.models import load_model
5
  from PIL import Image
6
  import os
7
-
8
- def load_model_from_huggingface():
9
- """Loads the model from Hugging Face Hub."""
10
- from huggingface_hub import from_pretrained_keras
11
- model = from_pretrained_keras("shaheer-data/Yellow-Rust-Prediction")
12
- return model
13
-
14
- def preprocess_image(image):
15
- """Preprocesses the uploaded image for model prediction."""
16
- image = image.resize((224, 224)) # Assuming input size of 224x224 for the model
17
- image_array = np.array(image)
18
- image_array = image_array / 255.0 # Normalize pixel values to [0, 1]
19
- image_array = np.expand_dims(image_array, axis=0) # Add batch dimension
20
- return image_array
21
-
22
- def predict_severity(model, image):
23
- """Predicts the severity using the model."""
24
- predictions = model.predict(image)
25
- class_names = ['0', 'MR', 'MRMS', 'MS', 'R', 'S']
26
- predicted_class = class_names[np.argmax(predictions)]
27
- return predicted_class, predictions
28
-
29
- # Streamlit App
30
- st.title("Disease Severity Prediction App")
31
- st.write("Upload an image to predict the severity of the disease.")
32
-
33
- # Image Upload
34
- uploaded_image = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])
35
-
36
- if uploaded_image:
37
- st.image(uploaded_image, caption="Uploaded Image", use_column_width=True)
38
-
39
- # Load model
40
- with st.spinner("Loading model..."):
41
- model = load_model_from_huggingface()
42
-
43
- # Preprocess image
44
- image = Image.open(uploaded_image)
45
- preprocessed_image = preprocess_image(image)
46
-
47
- # Predict severity
48
- with st.spinner("Predicting severity..."):
49
- predicted_class, prediction_scores = predict_severity(model, preprocessed_image)
50
-
51
- # Display results
52
- st.success(f"Predicted Class: {predicted_class}")
53
- st.write("Prediction Scores:")
54
- for class_name, score in zip(['0', 'MR', 'MRMS', 'MS', 'R', 'S'], prediction_scores[0]):
55
- st.write(f"{class_name}: {score:.4f}")
 
1
  import streamlit as st
2
+ import torch
3
+ from transformers import pipeline
 
4
  from PIL import Image
5
  import os
6
+ from dotenv import load_dotenv
7
+
8
+ # Load environment variables
9
+ load_dotenv()
10
+
11
+ # Load Hugging Face model
12
+ model_url = os.getenv('HUGGINGFACE_MODEL_URL')
13
+ model = torch.hub.load(model_url, 'model', source='hf')
14
+
15
+ # Setup Streamlit
16
+ st.title('Yellow Rust Severity Prediction')
17
+
18
+ # File uploader
19
+ uploaded_file = st.file_uploader("Upload an image of Yellow Rust", type=["jpg", "png"])
20
+
21
+ if uploaded_file is not None:
22
+ # Display the uploaded image
23
+ image = Image.open(uploaded_file)
24
+ st.image(image, caption="Uploaded Image", use_column_width=True)
25
+
26
+ # Process the image and make prediction
27
+ classifier = pipeline('image-classification', model=model_url)
28
+ results = classifier(image)
29
+
30
+ severity_level = results[0]['label']
31
+ confidence = results[0]['score']
32
+
33
+ st.write(f"Predicted Severity Level: {severity_level} with confidence: {confidence:.2f}")