Balajim57 commited on
Commit
09dc666
·
verified ·
1 Parent(s): b1648c2

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +81 -0
app.py ADDED
@@ -0,0 +1,81 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import torch
3
+ from PIL import Image
4
+ import open_clip
5
+ import matplotlib.pyplot as plt
6
+
7
+ # Check if CUDA is available
8
+ device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
9
+ model_path = "X:clip_logs/f1/f2/sb/2024_05_19-02_16_40-model_ViT-B-32-lr_5e-06-b_16-j_4-p_amp/checkpoints/epoch_20.pt"
10
+ model_name = "ViT-B-32"
11
+
12
+ # Load model and tokenizer
13
+ model, _, preprocess = open_clip.create_model_and_transforms(model_name=model_name, pretrained=model_path)
14
+ tokenizer = open_clip.get_tokenizer(model_name)
15
+
16
+ # Move model to device
17
+ model.to(device)
18
+
19
+ def predict_emotion(image, prompts):
20
+ # Preprocess the image
21
+ image = preprocess(image).unsqueeze(0).to(device)
22
+
23
+ # Tokenize the prompts
24
+ text = tokenizer(prompts).to(device)
25
+
26
+ # Perform inference
27
+ with torch.no_grad(), torch.cuda.amp.autocast():
28
+ image_features = model.encode_image(image)
29
+ text_features = model.encode_text(text)
30
+ image_features /= image_features.norm(dim=-1, keepdim=True)
31
+ text_features /= text_features.norm(dim=-1, keepdim=True)
32
+
33
+ text_probs = (100.0 * image_features @ text_features.T).softmax(dim=-1)
34
+
35
+ return text_probs.cpu().numpy()
36
+
37
+ def main():
38
+ st.title("Emotion Detection with OpenAI CLIP")
39
+
40
+ # Image upload
41
+ uploaded_image = st.file_uploader("Upload an image:", type=["jpg", "jpeg", "png"])
42
+
43
+ if uploaded_image is not None:
44
+ # Display uploaded image
45
+ image = Image.open(uploaded_image)
46
+ st.image(image, caption="Uploaded Image", use_column_width=True)
47
+
48
+ # Prompt inputs
49
+ st.write("Enter four prompts:")
50
+ prompt1 = st.text_input("Prompt 1:")
51
+ prompt2 = st.text_input("Prompt 2:")
52
+ prompt3 = st.text_input("Prompt 3:")
53
+ prompt4 = st.text_input("Prompt 4:")
54
+
55
+ prompts = [prompt1, prompt2, prompt3, prompt4]
56
+
57
+ # Predict emotion on button click
58
+ if st.button("Predict"):
59
+ with st.spinner("Predicting..."):
60
+ probabilities = predict_emotion(image, prompts)
61
+
62
+ # Print label probs in the specified format
63
+ formatted_probs = ["{:.5f}".format(prob) for prob in probabilities[0]]
64
+ results = dict(zip(prompts, formatted_probs))
65
+
66
+ # Display results
67
+ st.write("Emotion Probabilities:")
68
+ for prompt, prob in results.items():
69
+ st.write(f"{prompt}: {prob}")
70
+
71
+ # Plot the probabilities
72
+ plt.figure(figsize=(8, 6))
73
+ plt.bar(prompts, probabilities[0], color='skyblue')
74
+ plt.title('Emotion Probabilities')
75
+ plt.xlabel('Prompt')
76
+ plt.ylabel('Probability')
77
+ plt.ylim(0, 1) # Set y-axis limits to range [0, 1]
78
+ st.pyplot(plt)
79
+
80
+ if __name__ == "__main__":
81
+ main()