Update app.py
Browse files
app.py
CHANGED
@@ -2,9 +2,13 @@ import streamlit as st
|
|
2 |
from transformers import pipeline
|
3 |
import google.generativeai as genai
|
4 |
from pytube import Search
|
|
|
|
|
|
|
|
|
5 |
|
6 |
# Load sentiment analysis model using PyTorch backend
|
7 |
-
mood_classifier = pipeline("sentiment-analysis", framework="pt")
|
8 |
|
9 |
# Functions
|
10 |
def detect_mood(text):
|
@@ -16,11 +20,46 @@ def detect_mood(text):
|
|
16 |
else:
|
17 |
return "neutral"
|
18 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
def get_song_recommendations(mood, api_key):
|
20 |
try:
|
21 |
genai.configure(api_key=api_key)
|
22 |
model = genai.GenerativeModel('gemini-pro')
|
23 |
-
|
24 |
# System prompt to guide the AI
|
25 |
system_prompt = """
|
26 |
You are a music recommendation assistant specialized in Indian songs. Your task is to suggest popular Indian songs based on the user's mood.
|
@@ -59,16 +98,30 @@ mood_options = ["happy", "sad", "energetic", "romantic", "calm"]
|
|
59 |
# Input for Gemini API key
|
60 |
gemini_api_key = st.sidebar.text_input("Enter your Gemini API Key:", type="password")
|
61 |
|
62 |
-
# Add
|
63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
64 |
|
65 |
# Playlist
|
66 |
if 'playlist' not in st.session_state:
|
67 |
st.session_state.playlist = []
|
68 |
|
69 |
# Main content
|
70 |
-
if user_mood and gemini_api_key:
|
71 |
-
mood = detect_mood(user_mood)
|
72 |
st.write(f"🎭 Detected Mood: **{mood}**")
|
73 |
|
74 |
st.write("🎵 Recommended Songs:")
|
|
|
2 |
from transformers import pipeline
|
3 |
import google.generativeai as genai
|
4 |
from pytube import Search
|
5 |
+
import speech_recognition as sr
|
6 |
+
import tempfile
|
7 |
+
from pydub import AudioSegment
|
8 |
+
import numpy as np
|
9 |
|
10 |
# Load sentiment analysis model using PyTorch backend
|
11 |
+
mood_classifier = pipeline("sentiment-analysis", framework="pt")
|
12 |
|
13 |
# Functions
|
14 |
def detect_mood(text):
|
|
|
20 |
else:
|
21 |
return "neutral"
|
22 |
|
23 |
+
def speech_to_text():
|
24 |
+
# Initialize recognizer
|
25 |
+
r = sr.Recognizer()
|
26 |
+
|
27 |
+
# Create a temporary file to store the recorded audio
|
28 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as fp:
|
29 |
+
# Add audio recording widget
|
30 |
+
audio_bytes = st.audio_recorder(
|
31 |
+
text="Click to record your mood",
|
32 |
+
recording_color="#e8b62c",
|
33 |
+
neutral_color="#6aa36f"
|
34 |
+
)
|
35 |
+
|
36 |
+
if audio_bytes:
|
37 |
+
# Save audio bytes to temporary file
|
38 |
+
fp.write(audio_bytes)
|
39 |
+
temp_filename = fp.name
|
40 |
+
|
41 |
+
# Read the audio file
|
42 |
+
with sr.AudioFile(temp_filename) as source:
|
43 |
+
# Adjust for ambient noise and record
|
44 |
+
r.adjust_for_ambient_noise(source)
|
45 |
+
audio = r.record(source)
|
46 |
+
|
47 |
+
try:
|
48 |
+
# Use Google Speech Recognition
|
49 |
+
text = r.recognize_google(audio)
|
50 |
+
return text
|
51 |
+
except sr.UnknownValueError:
|
52 |
+
st.error("Could not understand the audio")
|
53 |
+
return None
|
54 |
+
except sr.RequestError:
|
55 |
+
st.error("Could not request results from speech recognition service")
|
56 |
+
return None
|
57 |
+
return None
|
58 |
+
|
59 |
def get_song_recommendations(mood, api_key):
|
60 |
try:
|
61 |
genai.configure(api_key=api_key)
|
62 |
model = genai.GenerativeModel('gemini-pro')
|
|
|
63 |
# System prompt to guide the AI
|
64 |
system_prompt = """
|
65 |
You are a music recommendation assistant specialized in Indian songs. Your task is to suggest popular Indian songs based on the user's mood.
|
|
|
98 |
# Input for Gemini API key
|
99 |
gemini_api_key = st.sidebar.text_input("Enter your Gemini API Key:", type="password")
|
100 |
|
101 |
+
# Add option to choose between text and speech input
|
102 |
+
input_method = st.sidebar.radio("Choose input method:", ["Text", "Speech"])
|
103 |
+
|
104 |
+
if input_method == "Text":
|
105 |
+
# Text input
|
106 |
+
user_mood = st.sidebar.selectbox("Select your mood:", mood_options)
|
107 |
+
else:
|
108 |
+
# Speech input
|
109 |
+
st.write("📢 Tell me about your day...")
|
110 |
+
spoken_text = speech_to_text()
|
111 |
+
|
112 |
+
if spoken_text:
|
113 |
+
st.write(f"You said: {spoken_text}")
|
114 |
+
user_mood = detect_mood(spoken_text)
|
115 |
+
else:
|
116 |
+
user_mood = None
|
117 |
|
118 |
# Playlist
|
119 |
if 'playlist' not in st.session_state:
|
120 |
st.session_state.playlist = []
|
121 |
|
122 |
# Main content
|
123 |
+
if (user_mood or spoken_text) and gemini_api_key:
|
124 |
+
mood = detect_mood(user_mood if user_mood else spoken_text)
|
125 |
st.write(f"🎭 Detected Mood: **{mood}**")
|
126 |
|
127 |
st.write("🎵 Recommended Songs:")
|