|
|
import os |
|
|
import streamlit as st |
|
|
import requests |
|
|
from PIL import Image |
|
|
from dotenv import load_dotenv |
|
|
import google.generativeai as genai |
|
|
|
|
|
|
|
|
load_dotenv() |
|
|
api_key = os.getenv("GEMINI_API_KEY") |
|
|
model = genai.GenerativeModel("gemini-1.5-flash") |
|
|
|
|
|
|
|
|
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 |
|
|
|
|
|
|
|
|
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 |
|
|
|
|
|
|
|
|
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)) |
|
|
|
|
|
|
|
|
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") |
|
|
|
|
|
|
|
|
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}") |
|
|
|
|
|
st.rerun() |
|
|
else: |
|
|
st.info("Please upload an image to begin analysis.") |
|
|
|