File size: 8,253 Bytes
fb3e30b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2debe57
 
fb3e30b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1fe5a91
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# -*- coding: utf-8 -*-
"""Noddy.ipynb

Automatically generated by Colaboratory.

Original file is located at
    https://colab.research.google.com/drive/1qBNYLNHT87B8kwlwXQ4mMM-0OLDGxtum
"""

import pandas as pd
import numpy as np
from datetime import date, datetime
from PIL import Image
import requests
import transformers as trf
import torch
import math as Math
import tensorflow as tf

processor = trf.AutoProcessor.from_pretrained("openai/clip-vit-base-patch32")
model = trf.AutoModelForZeroShotImageClassification.from_pretrained("openai/clip-vit-base-patch32")

#get bert model
tokenizer = trf.AutoTokenizer.from_pretrained("fabriceyhc/bert-base-uncased-imdb")
model1 = trf.AutoModelForSequenceClassification.from_pretrained("fabriceyhc/bert-base-uncased-imdb")

def get_mood(image):
    #image = image.convert('RGB')
    #image = image.resize((224, 224))
    image = np.array(image)
    image = np.expand_dims(image, axis=0)
    inputs = processor(text=["happy", "sad", "angry", "surprised", "disgusted", "calm", "neutral"], images=image, return_tensors="pt", padding=True)
    outputs = model(**inputs)
    logits_per_image = outputs.logits_per_image # this is the image-text similarity score
    probs = logits_per_image.softmax(dim=1) # we can take the softmax to get the label probabilities
    return probs

def get_productivity(image):
    #image = image.convert('RGB')
    #image = image.resize((224, 224))
    image = np.array(image)
    image = np.expand_dims(image, axis=0)
    inputs = processor(text=["low productivity", "medium productivity", "high productivity"], images=image, return_tensors="pt", padding=True)
    outputs = model(**inputs)
    logits_per_image = outputs.logits_per_image # this is the image-text similarity score
    probs = logits_per_image.softmax(dim=1) # we can take the softmax to get the label probabilities
    return probs

#from the prob array, get the index of the highest prob and return the label
def get_label_mood(probs):
    labels = ["happy", "sad", "angry", "surprised", "disgusted", "calm", "neutral"]
    index = torch.argmax(probs)
    return labels[index]

#from the prob array, get the index of the highest prob and return the label
def get_label_productivity(probs):
    labels = ["low", "medium", "high"]
    index = torch.argmax(probs)
    return labels[index]

#using bert model, get the sentiment of the text
def get_sentiment(mood, productivity):
    text = ["I am feeling " + str(mood) + " and my productivity is " + str(productivity)]
    inputs = tokenizer(text, return_tensors="pt")
    outputs = model1(**inputs)
    probs = outputs.logits.softmax(dim=1)
    labels = ["negative", "positive"]
    index = torch.argmax(probs)
    return labels[index]

# Get user input - use image file from local directory, not streamlit
# user_upload = Image.open("sad_sid.jpg")
# user_upload.resize((224, 224))

# #use the uploaded image to get mood and productivity
# mood = get_mood(user_upload)
# mood

# moodname = get_label_mood(mood)
# moodname

# productivity = get_productivity(user_upload)
# productivity

# pr = get_label_productivity(productivity)
# pr

# #use mood and productivity to get sentiment score using bert base model of huggingface
# sentiment_score = get_sentiment(moodname, pr)
# sentiment_score

# give playlist based on mood and productivity

def get_playlist(mood, productivity):
    if(mood == "happy" and (productivity == "high"or productivity == "medium" or productivity =="low")):
        return "https://open.spotify.com/playlist/37i9dQZF1DXdPec7aLTmlC"
    elif(mood == "sad" and (productivity == "high" or productivity == "medium")):
        return "https://open.spotify.com/album/7xSEO4RLeiTcVuwUFLb3UG"
    elif(mood == "sad" and (productivity == "low")):
        return "https://open.spotify.com/album/0qRhFH2PLpHPPYUGxwFwHO"
    elif(mood == "calm" and (productivity == "high"or productivity == "medium")):
        return "https://open.spotify.com/playlist/37i9dQZF1DWZeKCadgRdKQ"
    elif(mood == "calm" and (productivity == "low")):
        return "https://open.spotify.com/playlist/37i9dQZF1DWWQRwui0ExPn"
    elif(mood == "angry" and (productivity == "high"or productivity == "medium"or productivity =="low")):
        return "https://open.spotify.com/playlist/37i9dQZF1DX4sWSpwq3LiO"
    elif (mood == "disgusted" and (productivity == "high" or productivity == "medium" or productivity == "low")):
        return "https://open.spotify.com/playlist/37i9dQZF1DXa2SPUyWl8Y5"
    elif (mood == "surprised" and (productivity == "high" or productivity == "medium" or productivity == "low")):
        return "https://open.spotify.com/playlist/37i9dQZF1DWXti3N4Wp5xy"
    else:
        return "https://open.spotify.com/playlist/37i9dQZF1DX4SBhb3fqCJd"

#make an output function that takes in the image and returns the mood and productivity, and then the playlist
def out(image):
    mood = get_label_mood(get_mood(image))
    productivity = get_label_productivity(get_productivity(image))
    playlist = get_playlist(mood, productivity)
    sentiment = get_sentiment(mood, productivity)
    #output fun message based on mood
    if(mood == "happy"):
        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>"
    elif(mood == "sad"):
        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>"
    elif(mood == "angry"):
        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>"
    elif(mood == "surprised"):
        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>"
    elif(mood == "disgusted"):
        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>"
    elif(mood == "calm"):
        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>"
    else:
        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>"
    return m


# c = out(user_upload)
# c

#import gradio
import gradio as gr

#image input
image = gr.inputs.Image(shape=(224, 224))

#output
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>")

#interface
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()