File size: 1,395 Bytes
191dff2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
from PIL import Image
import io
import os
from dotenv import load_dotenv
import google.generativeai as genai

# Load API key from .env or environment
load_dotenv()
genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))

# Initialize the Gemini model
model = genai.GenerativeModel("gemini-1.5-flash")

def analyze_artwork(image: Image.Image):
    if image is None:
        return "Please upload an image."

    # Convert image to bytes
    img_bytes = io.BytesIO()
    image.save(img_bytes, format="JPEG")
    img_bytes = img_bytes.getvalue()

    try:
        response = model.generate_content([
            "You are an expert art historian. Analyze this artwork image in detail. "
            "Comment on its style, brushstrokes, color palette, composition, possible historical context, "
            "and whether it might be a forgery.",
            {"mime_type": "image/jpeg", "data": img_bytes}
        ])
        return response.text

    except Exception as e:
        return f"Error calling Gemini API:\n{e}"

# Gradio interface
gInterface = gr.Interface(
    fn=analyze_artwork,
    inputs=gr.Image(type="pil", label="Upload Artwork"),
    outputs="text",
    title="🖼️ M.O.N.A - Machine for Objective Neural Art-analysis",
    description="Upload an artwork and get analysis that provides objective insight. Powered by Google's Gemini AI."
)

gInterface.launch()