shaheer-data commited on
Commit
02acf5d
·
verified ·
1 Parent(s): 41a87d0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -22
app.py CHANGED
@@ -1,33 +1,60 @@
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}")
 
1
  import streamlit as st
2
+ import tensorflow as tf
 
3
  from PIL import Image
4
  import os
5
+ from huggingface_hub import hf_hub_url, set_access_token # Import Hugging Face utilities
6
 
7
+ # Title of the Streamlit app
8
+ st.title("Yellow Rust Severity Prediction")
9
 
10
+ # Load Hugging Face API token from environment
11
+ huggingface_api_token = os.getenv("HUGGINGFACE_TOKEN")
12
+ if huggingface_api_token is None:
13
+ st.error("YellowRust API token not found in environment. Please set it.")
14
+ st.stop()
15
 
16
+ # Set Hugging Face token for authentication
17
+ set_access_token(huggingface_api_token)
18
 
19
+ # Model repository details
20
+ model_repo_id = "your_huggingface_model_repo_id"
21
+ model_file_path = "final_meta_model.keras"
22
+
23
+ # Construct the model URL
24
+ st.write("Loading model from Hugging Face repo:", model_repo_id)
25
+ model_url = hf_hub_url(model_repo_id, model_file_path)
26
+ loaded_model = tf.keras.models.load_model(model_url) # Load model using tf.keras directly
27
+
28
+ # Function to make predictions
29
+ def predict_image(image):
30
+ image = image.resize((224, 224)) # Resize to match model input dimensions
31
+ image_array = tf.keras.preprocessing.image.img_to_array(image)
32
+ image_array = tf.expand_dims(image_array, axis=0) # Expand dimensions for batch prediction
33
+ predictions = loaded_model.predict(image_array)
34
+ return predictions
35
+
36
+ # Class labels for Yellow Rust severity levels
37
+ CLASS_LABELS = [
38
+ "Healthy",
39
+ "Mild Severity",
40
+ "Moderate Severity",
41
+ "Severe Severity",
42
+ "Very Severe",
43
+ "Extreme Severity"
44
+ ]
45
+
46
+ # Image upload widget
47
+ uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
48
 
49
  if uploaded_file is not None:
 
50
  image = Image.open(uploaded_file)
51
  st.image(image, caption="Uploaded Image", use_column_width=True)
52
+
53
+ # Display progress bar
54
+ with st.spinner("Making predictions..."):
55
+ predictions = predict_image(image)
56
+ predicted_class = predictions.argmax(axis=-1)
57
+ st.write(f"Predicted Severity Level: {CLASS_LABELS[predicted_class[0]]} with confidence {predictions[0][predicted_class[0]]:.2f}")
58
+
59
+ else:
60
+ st.write("Please upload an image file to make predictions.")