Spaces:
Sleeping
Sleeping
bhumikamittal
commited on
Commit
•
fb3e30b
1
Parent(s):
e48808e
Upload 2 files
Browse files- app.py +151 -0
- requirements.txt +6 -0
app.py
ADDED
@@ -0,0 +1,151 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# -*- coding: utf-8 -*-
|
2 |
+
"""Noddy.ipynb
|
3 |
+
|
4 |
+
Automatically generated by Colaboratory.
|
5 |
+
|
6 |
+
Original file is located at
|
7 |
+
https://colab.research.google.com/drive/1qBNYLNHT87B8kwlwXQ4mMM-0OLDGxtum
|
8 |
+
"""
|
9 |
+
|
10 |
+
import pandas as pd
|
11 |
+
import numpy as np
|
12 |
+
from datetime import date, datetime
|
13 |
+
from PIL import Image
|
14 |
+
import requests
|
15 |
+
import transformers as trf
|
16 |
+
import torch
|
17 |
+
import math as Math
|
18 |
+
import tensorflow as tf
|
19 |
+
|
20 |
+
processor = trf.AutoProcessor.from_pretrained("openai/clip-vit-base-patch32")
|
21 |
+
model = trf.AutoModelForZeroShotImageClassification.from_pretrained("openai/clip-vit-base-patch32")
|
22 |
+
|
23 |
+
#get bert model
|
24 |
+
tokenizer = trf.AutoTokenizer.from_pretrained("fabriceyhc/bert-base-uncased-imdb")
|
25 |
+
model1 = trf.AutoModelForSequenceClassification.from_pretrained("fabriceyhc/bert-base-uncased-imdb")
|
26 |
+
|
27 |
+
def get_mood(image):
|
28 |
+
#image = image.convert('RGB')
|
29 |
+
#image = image.resize((224, 224))
|
30 |
+
image = np.array(image)
|
31 |
+
image = np.expand_dims(image, axis=0)
|
32 |
+
inputs = processor(text=["happy", "sad", "angry", "surprised", "disgusted", "calm", "neutral"], images=image, return_tensors="pt", padding=True)
|
33 |
+
outputs = model(**inputs)
|
34 |
+
logits_per_image = outputs.logits_per_image # this is the image-text similarity score
|
35 |
+
probs = logits_per_image.softmax(dim=1) # we can take the softmax to get the label probabilities
|
36 |
+
return probs
|
37 |
+
|
38 |
+
def get_productivity(image):
|
39 |
+
#image = image.convert('RGB')
|
40 |
+
#image = image.resize((224, 224))
|
41 |
+
image = np.array(image)
|
42 |
+
image = np.expand_dims(image, axis=0)
|
43 |
+
inputs = processor(text=["low productivity", "medium productivity", "high productivity"], images=image, return_tensors="pt", padding=True)
|
44 |
+
outputs = model(**inputs)
|
45 |
+
logits_per_image = outputs.logits_per_image # this is the image-text similarity score
|
46 |
+
probs = logits_per_image.softmax(dim=1) # we can take the softmax to get the label probabilities
|
47 |
+
return probs
|
48 |
+
|
49 |
+
#from the prob array, get the index of the highest prob and return the label
|
50 |
+
def get_label_mood(probs):
|
51 |
+
labels = ["happy", "sad", "angry", "surprised", "disgusted", "calm", "neutral"]
|
52 |
+
index = torch.argmax(probs)
|
53 |
+
return labels[index]
|
54 |
+
|
55 |
+
#from the prob array, get the index of the highest prob and return the label
|
56 |
+
def get_label_productivity(probs):
|
57 |
+
labels = ["low", "medium", "high"]
|
58 |
+
index = torch.argmax(probs)
|
59 |
+
return labels[index]
|
60 |
+
|
61 |
+
#using bert model, get the sentiment of the text
|
62 |
+
def get_sentiment(mood, productivity):
|
63 |
+
text = ["I am feeling " + str(mood) + " and my productivity is " + str(productivity)]
|
64 |
+
inputs = tokenizer(text, return_tensors="pt")
|
65 |
+
outputs = model1(**inputs)
|
66 |
+
probs = outputs.logits.softmax(dim=1)
|
67 |
+
labels = ["negative", "positive"]
|
68 |
+
index = torch.argmax(probs)
|
69 |
+
return labels[index]
|
70 |
+
|
71 |
+
# Get user input - use image file from local directory, not streamlit
|
72 |
+
user_upload = Image.open("sad_sid.jpg")
|
73 |
+
user_upload.resize((224, 224))
|
74 |
+
|
75 |
+
# #use the uploaded image to get mood and productivity
|
76 |
+
# mood = get_mood(user_upload)
|
77 |
+
# mood
|
78 |
+
|
79 |
+
# moodname = get_label_mood(mood)
|
80 |
+
# moodname
|
81 |
+
|
82 |
+
# productivity = get_productivity(user_upload)
|
83 |
+
# productivity
|
84 |
+
|
85 |
+
# pr = get_label_productivity(productivity)
|
86 |
+
# pr
|
87 |
+
|
88 |
+
# #use mood and productivity to get sentiment score using bert base model of huggingface
|
89 |
+
# sentiment_score = get_sentiment(moodname, pr)
|
90 |
+
# sentiment_score
|
91 |
+
|
92 |
+
# give playlist based on mood and productivity
|
93 |
+
|
94 |
+
def get_playlist(mood, productivity):
|
95 |
+
if(mood == "happy" and (productivity == "high"or productivity == "medium" or productivity =="low")):
|
96 |
+
return "https://open.spotify.com/playlist/37i9dQZF1DXdPec7aLTmlC"
|
97 |
+
elif(mood == "sad" and (productivity == "high" or productivity == "medium")):
|
98 |
+
return "https://open.spotify.com/album/7xSEO4RLeiTcVuwUFLb3UG"
|
99 |
+
elif(mood == "sad" and (productivity == "low")):
|
100 |
+
return "https://open.spotify.com/album/0qRhFH2PLpHPPYUGxwFwHO"
|
101 |
+
elif(mood == "calm" and (productivity == "high"or productivity == "medium")):
|
102 |
+
return "https://open.spotify.com/playlist/37i9dQZF1DWZeKCadgRdKQ"
|
103 |
+
elif(mood == "calm" and (productivity == "low")):
|
104 |
+
return "https://open.spotify.com/playlist/37i9dQZF1DWWQRwui0ExPn"
|
105 |
+
elif(mood == "angry" and (productivity == "high"or productivity == "medium"or productivity =="low")):
|
106 |
+
return "https://open.spotify.com/playlist/37i9dQZF1DX4sWSpwq3LiO"
|
107 |
+
elif (mood == "disgusted" and (productivity == "high" or productivity == "medium" or productivity == "low")):
|
108 |
+
return "https://open.spotify.com/playlist/37i9dQZF1DXa2SPUyWl8Y5"
|
109 |
+
elif (mood == "surprised" and (productivity == "high" or productivity == "medium" or productivity == "low")):
|
110 |
+
return "https://open.spotify.com/playlist/37i9dQZF1DWXti3N4Wp5xy"
|
111 |
+
else:
|
112 |
+
return "https://open.spotify.com/playlist/37i9dQZF1DX4SBhb3fqCJd"
|
113 |
+
|
114 |
+
#make an output function that takes in the image and returns the mood and productivity, and then the playlist
|
115 |
+
def out(image):
|
116 |
+
mood = get_label_mood(get_mood(image))
|
117 |
+
productivity = get_label_productivity(get_productivity(image))
|
118 |
+
playlist = get_playlist(mood, productivity)
|
119 |
+
sentiment = get_sentiment(mood, productivity)
|
120 |
+
#output fun message based on mood
|
121 |
+
if(mood == "happy"):
|
122 |
+
m = "Yayy! Your current mood is " + str(mood) + " and your productivity is " + str(productivity) + ". Your overall sentiment is " +str(sentiment) +". \n You are doing great! Keep it up! \n Here is a playlist for you: " + "<a href="+str(playlist)+">"+str(playlist)+"</a>"
|
123 |
+
elif(mood == "sad"):
|
124 |
+
m = "Aww! Your current mood is " + str(mood) + " and your productivity is " + str(productivity) + ". Your overall sentiment is " +str(sentiment) +". \n Don't worry, everything will be alright! \n Here is a playlist to cheer you up: " + "<a href="+str(playlist)+">"+str(playlist)+"</a>"
|
125 |
+
elif(mood == "angry"):
|
126 |
+
m = "Calm down Angry Bird! Your current mood is " + str(mood) + " and your productivity is " + str(productivity) + ". Your overall sentiment is " +str(sentiment) +". \n Take a deep breath and relax! \n Listn to some music: " + "<a href="+str(playlist)+">"+str(playlist)+"</a>"
|
127 |
+
elif(mood == "surprised"):
|
128 |
+
m = "Woah! Your current mood is " + str(mood) + " and your productivity is " + str(productivity) + ". Your overall sentiment is " +str(sentiment) +". \n All of us love surprieses. Here is another one for you " + "<a href="+str(playlist)+">"+str(playlist)+"</a>"
|
129 |
+
elif(mood == "disgusted"):
|
130 |
+
m = "Eww! Your current mood is " + str(mood) + " and your productivity is " + str(productivity) + ". Your overall sentiment is " +str(sentiment) +". \n Don't worry, everything will be alright! \n Here is a playlist to cheer you up: " +"<a href="+str(playlist)+">"+str(playlist)+"</a>"
|
131 |
+
elif(mood == "calm"):
|
132 |
+
m = "Amazing! Your current mood is " + str(mood) + " and your productivity is " + str(productivity) + ". Your overall sentiment is " +str(sentiment) +". \n You are doing great! Keep it up! \n Here is a playlist for you: " + "<a href="+str(playlist)+">"+str(playlist)+"</a>"
|
133 |
+
else:
|
134 |
+
m = "Your current mood is " + str(mood) + " and your productivity is " + str(productivity) + ". Your overall sentiment is " +str(sentiment) +". \n Here is a playlist for you: " + "<a href="+str(playlist)+">"+str(playlist)+"</a>"
|
135 |
+
return m
|
136 |
+
|
137 |
+
|
138 |
+
# c = out(user_upload)
|
139 |
+
# c
|
140 |
+
|
141 |
+
#import gradio
|
142 |
+
import gradio as gr
|
143 |
+
|
144 |
+
#image input
|
145 |
+
image = gr.inputs.Image(shape=(224, 224))
|
146 |
+
|
147 |
+
#output
|
148 |
+
output = gr.outputs.HTML("<h2 style='font-family:Helvetica; color:Black; font-size: 20px; text-align: center'>Mood: {mood}</h2><h2 style='font-family:Helvetica; color:Black; font-size: 20px; text-align: center'>Productivity: {productivity}</h2><h2 style='font-family:Helvetica; color:Black; font-size: 20px; text-align: center'>Sentiment: {sentiment}</h2><h2 style='font-family:Helvetica; color:Black; font-size: 20px; text-align: center'>Playlist: <a href='{playlist}'>{playlist}</a></h2>")
|
149 |
+
|
150 |
+
#interface
|
151 |
+
gr.Interface(fn=out, inputs=image, outputs=output, title="Music Recommender - Noddy", description="Upload an image of yourself and we will recommend a playlist based on your mood and productivity level. \n I hope you have fun and I get grades Professor :) \n -Bhumika", allow_flagging=False, allow_screenshot=False, allow_embedding=False, theme="huggingface").launch(share=True)
|
requirements.txt
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
transformers
|
2 |
+
streamlit
|
3 |
+
pandas
|
4 |
+
numpy
|
5 |
+
torch
|
6 |
+
tensorflow
|