File size: 4,341 Bytes
7debef8
 
 
d71fd46
7debef8
9726da2
 
7debef8
 
 
9726da2
7debef8
 
 
 
 
 
 
9726da2
7debef8
 
9726da2
7debef8
 
 
 
 
 
 
 
9726da2
 
 
 
7debef8
 
 
9726da2
7debef8
9726da2
 
 
 
 
7debef8
9726da2
 
7debef8
9726da2
 
 
 
 
 
 
 
 
 
 
 
 
7debef8
9726da2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d71fd46
9726da2
 
7debef8
 
9726da2
 
 
 
 
 
 
7debef8
 
9726da2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import mimetypes
import uuid
import streamlit as st
from google import generativeai as genai
import tempfile
from PIL import Image

# Configure Gemini API
def configure_api():
    api_key = st.secrets.get("GEMINI_API_KEY", "AIzaSyBDkLcFKzE_T6r1E5XjbuXVaoW40Szn71s")  # Use secrets for API key
    genai.configure(api_key=api_key)
    return genai.GenerativeModel('gemini-2.0-flash')

def process_image(image_path, model):
    mime_type, _ = mimetypes.guess_type(image_path)
    if not mime_type:
        mime_type = "image/jpeg"
    
    with open(image_path, "rb") as img_file:
        image_data = img_file.read()
    
    response = model.generate_content(
        [
            {"mime_type": mime_type, "data": image_data},
            """Analyze the given picture, paying close attention to every detail. Based on your analysis, identify one suitable Pakistani or Hindi song released between 2007 and 2023 that you believe resonates with the person depicted in the image (considering their perceived gender). Provide only the name of the song."""
        ]
    )
    return response.text.strip()

def open_youtube_search(song_name):
    search_query = f"https://www.youtube.com/results?search_query={song_name.replace(' ', '+')}"
    return search_query

# Streamlit UI
st.set_page_config(page_title="Hindi Song Recommender", layout="centered")
st.title("🎶 Hindi Song Recommender from Image")
st.write("Upload your image to get a personalized Hindi/Pakistani song recommendation!")

# Initialize session state
if "image_path" not in st.session_state:
    st.session_state.image_path = None
if "suggested_song" not in st.session_state:
    st.session_state.suggested_song = None

# Main app workflow
uploaded_file = st.file_uploader("Drag and drop your image here", type=["jpg", "jpeg", "png"])

if uploaded_file:
    # Create a temporary directory if it doesn't exist
    temp_dir = tempfile.gettempdir()
    os.makedirs(temp_dir, exist_ok=True)
    
    # Save the uploaded file to the temporary directory
    image_path = os.path.join(temp_dir, f"temp_{uuid.uuid4().hex[:8]}.jpg")
    with open(image_path, "wb") as f:
        f.write(uploaded_file.getbuffer())
    
    # Display the image
    st.session_state.image_path = image_path
    st.image(uploaded_file, caption="Uploaded Image", use_container_width=True)

# Camera capture option (Streamlit's built-in camera functionality)
camera_option = st.checkbox("Use Camera Instead")
if camera_option:
    camera_input = st.camera_input("Take a picture")
    if camera_input:
        # Create a temporary directory if it doesn't exist
        temp_dir = tempfile.gettempdir()
        os.makedirs(temp_dir, exist_ok=True)
        
        # Save the captured image
        image_path = os.path.join(temp_dir, f"camera_{uuid.uuid4().hex[:8]}.jpg")
        with open(image_path, "wb") as f:
            f.write(camera_input.getbuffer())
        
        st.session_state.image_path = image_path

# Process the image and suggest a song
if st.session_state.image_path and st.button("Suggest Hindi Song"):
    with st.spinner("Analyzing image and finding a song..."):
        try:
            model = configure_api()
            st.session_state.suggested_song = process_image(st.session_state.image_path, model)
            st.success(f"🎵 Suggested Hindi Song: {st.session_state.suggested_song}")
            
            # Generate YouTube search link
            youtube_link = open_youtube_search(st.session_state.suggested_song)
            st.markdown(f"[Click here to listen on YouTube]({youtube_link})")
        except Exception as e:
            st.error(f"Error: {e}")

# Display song suggestion if available
if st.session_state.suggested_song:
    st.subheader("Your Song Recommendation:")
    st.write(f"🎵 {st.session_state.suggested_song}")
    
    # Create a YouTube search button
    youtube_link = open_youtube_search(st.session_state.suggested_song)
    st.markdown(f"[Listen on YouTube]({youtube_link})")

# Add app information
st.sidebar.title("About")
st.sidebar.info("""
This app uses Google's Gemini AI to analyze your image and suggest a Hindi or Pakistani song 
that resonates with the person in the image. Upload a photo or take one with your camera!
""")

# Add footer
st.markdown("---")
st.markdown("Made with ❤️ | Powered by Gemini 2.0")