M.O.N.A / app.py
Michael Rey
added working app
191dff2
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()