import streamlit as st import torch import torch from torchvision import transforms from PIL import Image import openai # Load CLIP model clip_model, preprocess = openai.load("clip-vit-base-patch32") # Function to preprocess images for CLIP def preprocess_image(image_path): image = Image.open(image_path).convert("RGB") image = preprocess(image) return image.unsqueeze(0) import openai import timm import base64 import numpy as np from PIL import Image, ImageEnhance from torchvision import transforms from io import BytesIO # Load models device = "cuda" if torch.cuda.is_available() else "cpu" clip_model, clip_preprocess = clip.load("ViT-B/32", device=device) aesthetic_model = timm.create_model('hf_hub:timm/convnext_base', pretrained=True, num_classes=1) aesthetic_model.eval() # Image Preprocessing transform = transforms.Compose([ transforms.Resize((224, 224)), transforms.ToTensor(), transforms.Normalize(mean=[0.5], std=[0.5]) ]) # Function: Predict Aesthetic Score def predict_aesthetic_score(image): image = transform(image).unsqueeze(0) with torch.no_grad(): score = aesthetic_model(image).item() return round(score, 2) # Function: Encode image for GPT-4 Vision def encode_image(image): buffered = BytesIO() image.save(buffered, format="JPEG") return base64.b64encode(buffered.getvalue()).decode("utf-8") # Function: Get GPT-4 Vision Feedback def analyze_image_with_gpt4(image, prompt): encoded_image = encode_image(image) try: response = openai.ChatCompletion.create( model="gpt-4-vision-preview", messages=[ {"role": "system", "content": "You are an expert image critic analyzing AI-generated images."}, {"role": "user", "content": [ {"type": "text", "text": prompt}, {"type": "image", "image_url": f"data:image/jpeg;base64,{encoded_image}"} ]} ], max_tokens=500 ) return response["choices"][0]["message"]["content"] except Exception as e: return f"GPT-4 Error: {e}" # Function: AI Auto-Enhance Image def auto_enhance_image(image, feedback): if "dim lighting" in feedback.lower(): enhancer = ImageEnhance.Brightness(image) image = enhancer.enhance(1.2) # Increase brightness by 20% if "low contrast" in feedback.lower(): enhancer = ImageEnhance.Contrast(image) image = enhancer.enhance(1.3) if "blurry" in feedback.lower(): enhancer = ImageEnhance.Sharpness(image) image = enhancer.enhance(2.0) return image # Streamlit UI st.title("AI-Powered Image Evaluator & Enhancer") uploaded_file = st.file_uploader("Upload an Image", type=["jpg", "png"]) if uploaded_file: image = Image.open(uploaded_file) st.image(image, caption="Uploaded Image", use_column_width=True) # Aesthetic Scoring aesthetic_score = predict_aesthetic_score(image) st.write(f"**Aesthetic Score:** {aesthetic_score}/10") # GPT-4 Feedback prompt = "Evaluate this image. Does it match the description 'A futuristic cyberpunk city with neon lights and rain'? Analyze composition, color, and realism." feedback = analyze_image_with_gpt4(image, prompt) st.write("**AI Feedback:**", feedback) # Auto-Enhancement Option if st.button("Auto-Enhance Image"): enhanced_image = auto_enhance_image(image, feedback) st.image(enhanced_image, caption="Enhanced Image", use_column_width=True) st.success("Image enhanced successfully!")