Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,142 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
import emoji
|
| 4 |
+
import requests
|
| 5 |
+
|
| 6 |
+
# Convert Google Drive link to a direct image link
|
| 7 |
+
logo_url = "https://drive.google.com/uc?export=view&id=1cKAxqifPx3ytEsjpzFuHs7NGe7Ml2toW"
|
| 8 |
+
|
| 9 |
+
# Load the emotion detection pipeline (Hugging Face model)
|
| 10 |
+
emotion_detector = pipeline("text-classification", model="j-hartmann/emotion-english-distilroberta-base")
|
| 11 |
+
|
| 12 |
+
# Function to convert emojis to text
|
| 13 |
+
def emoji_to_text(text):
|
| 14 |
+
return emoji.demojize(text, delimiters=(" ", " ")) # Convert emojis to text descriptions
|
| 15 |
+
|
| 16 |
+
# Function to detect emotion from text
|
| 17 |
+
def detect_emotion(text):
|
| 18 |
+
text_with_emojis = emoji_to_text(text) # Convert emojis to text
|
| 19 |
+
result = emotion_detector(text_with_emojis)[0] # Use the emotion detection model
|
| 20 |
+
return result['label']
|
| 21 |
+
|
| 22 |
+
# Function to append emoji to input text
|
| 23 |
+
def append_emoji(text, selected_emoji):
|
| 24 |
+
return text + selected_emoji # Append the selected emoji to the text input
|
| 25 |
+
|
| 26 |
+
# Function to get mood description from slider value
|
| 27 |
+
def get_mood_from_slider(mood_value):
|
| 28 |
+
if mood_value < 0.1:
|
| 29 |
+
return "very sad"
|
| 30 |
+
elif mood_value < 0.2:
|
| 31 |
+
return "sad"
|
| 32 |
+
elif mood_value < 0.3:
|
| 33 |
+
return "slightly sad"
|
| 34 |
+
elif mood_value < 0.4:
|
| 35 |
+
return "neutral"
|
| 36 |
+
elif mood_value < 0.5:
|
| 37 |
+
return "calm"
|
| 38 |
+
elif mood_value < 0.6:
|
| 39 |
+
return "slightly happy"
|
| 40 |
+
elif mood_value < 0.7:
|
| 41 |
+
return "happy"
|
| 42 |
+
elif mood_value < 0.8:
|
| 43 |
+
return "very happy"
|
| 44 |
+
elif mood_value < 0.9:
|
| 45 |
+
return "excited"
|
| 46 |
+
else:
|
| 47 |
+
return "ecstatic"
|
| 48 |
+
|
| 49 |
+
# Function to get tempo description from slider value
|
| 50 |
+
def get_tempo_from_slider(tempo_value):
|
| 51 |
+
if tempo_value < 0.1:
|
| 52 |
+
return "very slow"
|
| 53 |
+
elif tempo_value < 0.2:
|
| 54 |
+
return "slow"
|
| 55 |
+
elif tempo_value < 0.3:
|
| 56 |
+
return "moderately slow"
|
| 57 |
+
elif tempo_value < 0.4:
|
| 58 |
+
return "medium slow"
|
| 59 |
+
elif tempo_value < 0.5:
|
| 60 |
+
return "medium"
|
| 61 |
+
elif tempo_value < 0.6:
|
| 62 |
+
return "medium fast"
|
| 63 |
+
elif tempo_value < 0.7:
|
| 64 |
+
return "fast"
|
| 65 |
+
elif tempo_value < 0.8:
|
| 66 |
+
return "very fast"
|
| 67 |
+
elif tempo_value < 0.9:
|
| 68 |
+
return "rapid"
|
| 69 |
+
else:
|
| 70 |
+
return "extremely fast"
|
| 71 |
+
|
| 72 |
+
# Function to search YouTube for a video based on mood and tempo
|
| 73 |
+
def search_youtube_music(mood_value, tempo_value):
|
| 74 |
+
mood_query = get_mood_from_slider(mood_value)
|
| 75 |
+
tempo_query = get_tempo_from_slider(tempo_value)
|
| 76 |
+
search_query = f"{mood_query} {tempo_query} music"
|
| 77 |
+
|
| 78 |
+
# YouTube API request (API_KEY to be added if required)
|
| 79 |
+
params = {
|
| 80 |
+
"part": "snippet",
|
| 81 |
+
"q": search_query,
|
| 82 |
+
"key": "YOUR_API_KEY", # Replace with a valid YouTube API key
|
| 83 |
+
"type": "video",
|
| 84 |
+
"videoCategoryId": "10", # Music category
|
| 85 |
+
"maxResults": 1 # Only get 1 result
|
| 86 |
+
}
|
| 87 |
+
|
| 88 |
+
response = requests.get("https://www.googleapis.com/youtube/v3/search", params=params)
|
| 89 |
+
if response.status_code != 200:
|
| 90 |
+
return "Error fetching YouTube data"
|
| 91 |
+
|
| 92 |
+
json_response = response.json()
|
| 93 |
+
if "items" not in json_response or len(json_response["items"]) == 0:
|
| 94 |
+
return "No videos found"
|
| 95 |
+
|
| 96 |
+
video_id = json_response["items"][0]["id"]["videoId"]
|
| 97 |
+
video_url = f"https://www.youtube.com/embed/{video_id}"
|
| 98 |
+
|
| 99 |
+
youtube_video_link = f"https://www.youtube.com/watch?v={video_id}"
|
| 100 |
+
iframe_html = f'<iframe width="560" height="315" src="{video_url}" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>'
|
| 101 |
+
|
| 102 |
+
return iframe_html
|
| 103 |
+
|
| 104 |
+
# Streamlit UI
|
| 105 |
+
st.set_page_config(page_title="Emotion Detector & Music Finder", layout="centered")
|
| 106 |
+
|
| 107 |
+
# Display logo
|
| 108 |
+
st.image(logo_url, use_column_width=True)
|
| 109 |
+
|
| 110 |
+
st.title("π AI Emotion Detector & Music Finder")
|
| 111 |
+
|
| 112 |
+
# Tabs for emotion detection and music finder
|
| 113 |
+
tab1, tab2 = st.tabs(["Emotion Detection", "Mood & Tempo Music Finder"])
|
| 114 |
+
|
| 115 |
+
# Tab 1: Emotion Detection
|
| 116 |
+
with tab1:
|
| 117 |
+
st.subheader("Emotion Detection from Text")
|
| 118 |
+
|
| 119 |
+
text_input = st.text_input("Enter your text here", placeholder="Type something here...")
|
| 120 |
+
emoji_list = ["π", "π’", "π‘", "π", "π", "π", "π€", "π΄", "π", "π"]
|
| 121 |
+
selected_emoji = st.selectbox("Choose an emoji to add", options=emoji_list)
|
| 122 |
+
|
| 123 |
+
if st.button("Add Emoji to Text"):
|
| 124 |
+
text_input = append_emoji(text_input, selected_emoji)
|
| 125 |
+
|
| 126 |
+
if st.button("Analyze Emotion"):
|
| 127 |
+
if text_input:
|
| 128 |
+
detected_emotion = detect_emotion(text_input)
|
| 129 |
+
st.success(f"Detected Emotion: **{detected_emotion}**")
|
| 130 |
+
else:
|
| 131 |
+
st.error("Please enter some text before analyzing.")
|
| 132 |
+
|
| 133 |
+
# Tab 2: Mood & Tempo Music Finder
|
| 134 |
+
with tab2:
|
| 135 |
+
st.subheader("Find Music Based on Mood and Tempo")
|
| 136 |
+
|
| 137 |
+
mood_slider = st.slider("Mood", min_value=0.0, max_value=1.0, step=0.1)
|
| 138 |
+
tempo_slider = st.slider("Tempo", min_value=0.0, max_value=1.0, step=0.1)
|
| 139 |
+
|
| 140 |
+
if st.button("Find Music"):
|
| 141 |
+
youtube_embed = search_youtube_music(mood_slider, tempo_slider)
|
| 142 |
+
st.markdown(youtube_embed, unsafe_allow_html=True)
|