changes
Browse files- app.py +98 -0
- requirements.txt +9 -0
app.py
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import streamlit as st
|
| 3 |
+
import requests
|
| 4 |
+
from PIL import Image
|
| 5 |
+
from dotenv import load_dotenv
|
| 6 |
+
import google.generativeai as genai
|
| 7 |
+
|
| 8 |
+
# Load environment variables
|
| 9 |
+
load_dotenv()
|
| 10 |
+
api_key = os.getenv("GEMINI_API_KEY")
|
| 11 |
+
model = genai.GenerativeModel("gemini-1.5-flash")
|
| 12 |
+
|
| 13 |
+
# Fetch analysis from Gemini API
|
| 14 |
+
def get_gemini_response(prompt, image):
|
| 15 |
+
try:
|
| 16 |
+
return model.generate_content([prompt, image]).text
|
| 17 |
+
except Exception as e:
|
| 18 |
+
st.error(f"Gemini API error: {e}")
|
| 19 |
+
return None
|
| 20 |
+
|
| 21 |
+
# Fetch artwork info from MET Museum
|
| 22 |
+
def fetch_met_data(object_id):
|
| 23 |
+
try:
|
| 24 |
+
url = f"https://collectionapi.metmuseum.org/public/collection/v1/objects/{object_id}"
|
| 25 |
+
response = requests.get(url)
|
| 26 |
+
return response.json() if response.status_code == 200 else None
|
| 27 |
+
except Exception as e:
|
| 28 |
+
st.error(f"Error fetching MET data: {e}")
|
| 29 |
+
return None
|
| 30 |
+
|
| 31 |
+
# --- Streamlit App ---
|
| 32 |
+
st.set_page_config(layout="wide")
|
| 33 |
+
st.title('π¨ AI-Assisted Art Authenticity and Style Analysis')
|
| 34 |
+
st.write("Upload an artwork image to analyze its authenticity, brushstroke, color palette, and composition.")
|
| 35 |
+
|
| 36 |
+
uploaded_image = st.file_uploader("Upload Artwork", type=["jpg", "jpeg", "png"])
|
| 37 |
+
|
| 38 |
+
if uploaded_image:
|
| 39 |
+
image = Image.open(uploaded_image).resize((200, 200)) # Resize for consistency
|
| 40 |
+
|
| 41 |
+
# MET artwork example
|
| 42 |
+
met_id = 436121
|
| 43 |
+
met_data = fetch_met_data(met_id)
|
| 44 |
+
met_image = None
|
| 45 |
+
if met_data and met_data.get("primaryImage"):
|
| 46 |
+
met_image = Image.open(requests.get(met_data['primaryImage'], stream=True).raw).resize((512, 512))
|
| 47 |
+
|
| 48 |
+
col1, col2 = st.columns(2)
|
| 49 |
+
with col1:
|
| 50 |
+
st.image(image, caption="π₯ Uploaded Artwork", use_container_width=True)
|
| 51 |
+
with col2:
|
| 52 |
+
if met_image:
|
| 53 |
+
st.image(met_image, caption=f"πΌοΈ {met_data['title']}", use_container_width=True)
|
| 54 |
+
st.markdown(f"**π¨βπ¨ Artist:** {met_data['artistDisplayName']}")
|
| 55 |
+
st.markdown(f"**π
Year:** {met_data['objectDate']}")
|
| 56 |
+
st.markdown(f"**π¨ Medium:** {met_data['medium']}")
|
| 57 |
+
else:
|
| 58 |
+
st.warning("Could not load MET comparison image.")
|
| 59 |
+
|
| 60 |
+
st.divider()
|
| 61 |
+
st.markdown("## π§ Comparative Art Analysis")
|
| 62 |
+
|
| 63 |
+
prompt_uploaded = (
|
| 64 |
+
"Analyze this artwork. Focus on: "
|
| 65 |
+
"1. Brushstroke technique, 2. Color palette, 3. Composition style and structure. "
|
| 66 |
+
"Identify any stylistic clues for authenticity or imitation."
|
| 67 |
+
)
|
| 68 |
+
prompt_met = (
|
| 69 |
+
"Analyze this famous artwork from the MET collection. Focus on: "
|
| 70 |
+
"1. Brushstroke technique, 2. Color palette, 3. Composition style and structure."
|
| 71 |
+
)
|
| 72 |
+
|
| 73 |
+
col3, col4 = st.columns(2)
|
| 74 |
+
with col3:
|
| 75 |
+
st.markdown("### π¨ Uploaded Artwork")
|
| 76 |
+
analysis_uploaded = get_gemini_response(prompt_uploaded, image)
|
| 77 |
+
st.write(analysis_uploaded or "No analysis returned.")
|
| 78 |
+
with col4:
|
| 79 |
+
st.markdown("### πΌοΈ MET Artwork")
|
| 80 |
+
if met_image:
|
| 81 |
+
analysis_met = get_gemini_response(prompt_met, met_image)
|
| 82 |
+
st.write(analysis_met or "No analysis returned.")
|
| 83 |
+
|
| 84 |
+
st.divider()
|
| 85 |
+
st.subheader("π Expert Feedback")
|
| 86 |
+
|
| 87 |
+
# Feedback form
|
| 88 |
+
with st.form("feedback_form"):
|
| 89 |
+
feedback_text = st.text_area("Your feedback on the analysis:")
|
| 90 |
+
submitted = st.form_submit_button("Send")
|
| 91 |
+
|
| 92 |
+
if submitted and feedback_text.strip():
|
| 93 |
+
st.success("Thanks for your feedback!")
|
| 94 |
+
st.markdown(f"> {feedback_text}")
|
| 95 |
+
# Force a rerun to clear the feedback
|
| 96 |
+
st.rerun()
|
| 97 |
+
else:
|
| 98 |
+
st.info("Please upload an image to begin analysis.")
|
requirements.txt
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
Streamlit
|
| 2 |
+
GenerativeAI
|
| 3 |
+
PIL
|
| 4 |
+
numpy
|
| 5 |
+
requests
|
| 6 |
+
tensorflow
|
| 7 |
+
scikit-learn
|
| 8 |
+
io
|
| 9 |
+
os
|