import os import streamlit as st import requests from PIL import Image from dotenv import load_dotenv import google.generativeai as genai # Load environment variables load_dotenv() api_key = os.getenv("GEMINI_API_KEY") model = genai.GenerativeModel("gemini-1.5-flash") # Fetch analysis from Gemini API def get_gemini_response(prompt, image): try: return model.generate_content([prompt, image]).text except Exception as e: st.error(f"Gemini API error: {e}") return None # Fetch artwork info from MET Museum def fetch_met_data(object_id): try: url = f"https://collectionapi.metmuseum.org/public/collection/v1/objects/{object_id}" response = requests.get(url) return response.json() if response.status_code == 200 else None except Exception as e: st.error(f"Error fetching MET data: {e}") return None # --- Streamlit App --- st.set_page_config(layout="wide") st.title('🎨 AI-Assisted Art Authenticity and Style Analysis') st.write("Upload an artwork image to analyze its authenticity, brushstroke, color palette, and composition.") uploaded_image = st.file_uploader("Upload Artwork", type=["jpg", "jpeg", "png"]) if uploaded_image: image = Image.open(uploaded_image).resize((200, 200)) # Resize for consistency # MET artwork example met_id = 436121 met_data = fetch_met_data(met_id) met_image = None if met_data and met_data.get("primaryImage"): met_image = Image.open(requests.get(met_data['primaryImage'], stream=True).raw).resize((512, 512)) col1, col2 = st.columns(2) with col1: st.image(image, caption="📥 Uploaded Artwork", use_container_width=True) with col2: if met_image: st.image(met_image, caption=f"🖼️ {met_data['title']}", use_container_width=True) st.markdown(f"**👨‍🎨 Artist:** {met_data['artistDisplayName']}") st.markdown(f"**📅 Year:** {met_data['objectDate']}") st.markdown(f"**🎨 Medium:** {met_data['medium']}") else: st.warning("Could not load MET comparison image.") st.divider() st.markdown("## 🧠 Comparative Art Analysis") prompt_uploaded = ( "Analyze this artwork. Focus on: " "1. Brushstroke technique, 2. Color palette, 3. Composition style and structure. " "Identify any stylistic clues for authenticity or imitation." ) prompt_met = ( "Analyze this famous artwork from the MET collection. Focus on: " "1. Brushstroke technique, 2. Color palette, 3. Composition style and structure." ) col3, col4 = st.columns(2) with col3: st.markdown("### 🎨 Uploaded Artwork") analysis_uploaded = get_gemini_response(prompt_uploaded, image) st.write(analysis_uploaded or "No analysis returned.") with col4: st.markdown("### 🖼️ MET Artwork") if met_image: analysis_met = get_gemini_response(prompt_met, met_image) st.write(analysis_met or "No analysis returned.") st.divider() st.subheader("📝 Expert Feedback") # Feedback form with st.form("feedback_form"): feedback_text = st.text_area("Your feedback on the analysis:") submitted = st.form_submit_button("Send") if submitted and feedback_text.strip(): st.success("Thanks for your feedback!") st.markdown(f"> {feedback_text}") # Force a rerun to clear the feedback st.rerun() else: st.info("Please upload an image to begin analysis.")