File size: 3,585 Bytes
23f0310
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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.")