patricebechard commited on
Commit
9a3a339
1 Parent(s): 6862da9

add file for streamlit app

Browse files
Files changed (3) hide show
  1. app.py +31 -0
  2. kanye_lyrics.txt +0 -0
  3. requirements.txt +3 -0
app.py ADDED
@@ -0,0 +1,31 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import requests
2
+ import streamlit as st
3
+ from PIL import Image
4
+ from transformers import CLIPModel, CLIPProcessor
5
+
6
+ st.title("Most Similar Kanye West Song")
7
+
8
+ # read lyrics from all kanye west songs
9
+ with open("kanye_lyrics.txt", "r") as f:
10
+ all_lyrics = f.read().split("\n\n\n\n")
11
+
12
+ # load model and processor
13
+ model = CLIPModel.from_pretrained("openai/clip-vit-base-patch32")
14
+ processor = CLIPProcessor.from_pretrained("openai/clip-vit-base-patch32")
15
+
16
+ # get user input
17
+ url = st.text_input("Enter an image url: ", "https://i.redd.it/0vsrxk3gcfr91.jpg")
18
+
19
+ # get image from url
20
+ image = Image.open(requests.get(url, stream=True).raw)
21
+ st.image(url, width=700)
22
+
23
+ # get the image-text similarity score
24
+ inputs = processor(text=all_lyrics, images=image, return_tensors="pt", padding=True, truncation=True)
25
+ outputs = model(**inputs)
26
+ logits_per_image = outputs.logits_per_image # this is the image-text similarity score
27
+ probs = logits_per_image.softmax(dim=1) # we can take the softmax to get the label probabilities
28
+
29
+ # get the most similar kanye song
30
+ most_similar_song = all_lyrics[probs.argmax().item()]
31
+ st.write(most_similar_song)
kanye_lyrics.txt ADDED
The diff for this file is too large to render. See raw diff
 
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ transformers==4.22.2
2
+ Pillow==9.2.0
3
+ streamlit==1.13.0