@mokollmar commited on
Commit
942604a
·
1 Parent(s): 0679d1e

lighting.ai transfer

Browse files
Files changed (2) hide show
  1. .gitignore +2 -1
  2. app.py +94 -3
.gitignore CHANGED
@@ -1 +1,2 @@
1
- .python-version
 
 
1
+ .python-version
2
+ .streamlit
app.py CHANGED
@@ -1,5 +1,96 @@
1
  import streamlit as st
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
 
3
- x = st.slider('Select a value')
4
- st.write(x, 'squared is', x * x)
5
- print(x)
 
1
  import streamlit as st
2
+ import torch
3
+ import clip
4
+ from PIL import Image
5
+ import numpy as np
6
+ import hmac
7
+
8
+
9
+ # Load CLIP model and preprocessing
10
+ device = "cuda" if torch.cuda.is_available() else "cpu"
11
+ model, preprocess = clip.load("ViT-B/32", device=device)
12
+
13
+ # Function to predict descriptions and probabilities
14
+ def predict(image, descriptions):
15
+ image = preprocess(image).unsqueeze(0).to(device)
16
+ text = clip.tokenize(descriptions).to(device)
17
+
18
+ with torch.no_grad():
19
+ image_features = model.encode_image(image)
20
+ text_features = model.encode_text(text)
21
+
22
+ logits_per_image, logits_per_text = model(image, text)
23
+ probs = logits_per_image.softmax(dim=-1).cpu().numpy()
24
+
25
+ return descriptions[np.argmax(probs)], np.max(probs)
26
+
27
+ # Streamlit app
28
+ def main():
29
+ st.title("Image understanding model test")
30
+
31
+ # Instructions for the user
32
+ st.markdown("---")
33
+ st.markdown("### Upload an image to test how well the model understands it")
34
+
35
+ # Upload image through Streamlit with a unique key
36
+ uploaded_image = st.file_uploader("Upload an image...", type=["jpg", "png", "jpeg"], key="uploaded_image")
37
+
38
+ if uploaded_image is not None:
39
+ # Convert the uploaded image to PIL Image
40
+ pil_image = Image.open(uploaded_image)
41
+
42
+ # Limit the height of the displayed image to 400px
43
+ st.image(pil_image, caption="Uploaded Image.", use_column_width=True, width=200)
44
+
45
+ # Instructions for the user
46
+ st.markdown("### 2 Lies and 1 Truth")
47
+ st.markdown("Write 3 descriptions about the image, 1 must be true.")
48
+
49
+ # Get user input for descriptions
50
+ description1 = st.text_input("Description 1:", placeholder='A red apple')
51
+ description2 = st.text_input("Description 2:", placeholder='A car parked in a garage')
52
+ description3 = st.text_input("Description 3:", placeholder='An orange fruit on a tree')
53
+
54
+ descriptions = [description1, description2, description3]
55
+
56
+ # Button to trigger prediction
57
+ if st.button("Predict"):
58
+ if all(descriptions):
59
+ # Make predictions
60
+ best_description, best_prob = predict(pil_image, descriptions)
61
+
62
+ # Display the highest probability description and its probability
63
+ st.write(f"**Best Description:** {best_description}")
64
+ st.write(f"**Prediction Probability:** {best_prob:.2%}")
65
+
66
+ # Display progress bar for the highest probability
67
+ st.progress(float(best_prob))
68
+
69
+ # user has correct Password?
70
+ def check_password():
71
+ def password_entered():
72
+ if hmac.compare_digest(st.session_state["password"], st.secrets["password"]):
73
+ st.session_state["password_correct"] = True
74
+ del st.session_state["password"] # Don't store the password.
75
+ else:
76
+ st.session_state["password_correct"] = False
77
+
78
+ # Return True if the password is validated.
79
+ if st.session_state.get("password_correct", False):
80
+ return True
81
+
82
+ # Show input for password.
83
+ st.text_input("Password", type="password", on_change=password_entered, key="password")
84
+ if "password_correct" in st.session_state:
85
+ st.error("😕 Password incorrect")
86
+ return False
87
+
88
+
89
+ if __name__ == "__main__":
90
+ if not check_password():
91
+ st.stop() # Do not continue if check_password is not True.
92
+
93
+ # Main Streamlit app starts here
94
+ main()
95
+
96