Spaces:
Configuration error
Configuration error
| 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") |