Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files- ai_image_editor_demo.py.txt +92 -0
- requirements.txt +8 -0
ai_image_editor_demo.py.txt
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import torch
|
| 3 |
+
import clip
|
| 4 |
+
import openai
|
| 5 |
+
import timm
|
| 6 |
+
import base64
|
| 7 |
+
import numpy as np
|
| 8 |
+
from PIL import Image, ImageEnhance
|
| 9 |
+
from torchvision import transforms
|
| 10 |
+
from io import BytesIO
|
| 11 |
+
|
| 12 |
+
# Load models
|
| 13 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 14 |
+
clip_model, clip_preprocess = clip.load("ViT-B/32", device=device)
|
| 15 |
+
|
| 16 |
+
aesthetic_model = timm.create_model('hf_hub:timm/convnext_base', pretrained=True, num_classes=1)
|
| 17 |
+
aesthetic_model.eval()
|
| 18 |
+
|
| 19 |
+
# Image Preprocessing
|
| 20 |
+
transform = transforms.Compose([
|
| 21 |
+
transforms.Resize((224, 224)),
|
| 22 |
+
transforms.ToTensor(),
|
| 23 |
+
transforms.Normalize(mean=[0.5], std=[0.5])
|
| 24 |
+
])
|
| 25 |
+
|
| 26 |
+
# Function: Predict Aesthetic Score
|
| 27 |
+
def predict_aesthetic_score(image):
|
| 28 |
+
image = transform(image).unsqueeze(0)
|
| 29 |
+
with torch.no_grad():
|
| 30 |
+
score = aesthetic_model(image).item()
|
| 31 |
+
return round(score, 2)
|
| 32 |
+
|
| 33 |
+
# Function: Encode image for GPT-4 Vision
|
| 34 |
+
def encode_image(image):
|
| 35 |
+
buffered = BytesIO()
|
| 36 |
+
image.save(buffered, format="JPEG")
|
| 37 |
+
return base64.b64encode(buffered.getvalue()).decode("utf-8")
|
| 38 |
+
|
| 39 |
+
# Function: Get GPT-4 Vision Feedback
|
| 40 |
+
def analyze_image_with_gpt4(image, prompt):
|
| 41 |
+
encoded_image = encode_image(image)
|
| 42 |
+
try:
|
| 43 |
+
response = openai.ChatCompletion.create(
|
| 44 |
+
model="gpt-4-vision-preview",
|
| 45 |
+
messages=[
|
| 46 |
+
{"role": "system", "content": "You are an expert image critic analyzing AI-generated images."},
|
| 47 |
+
{"role": "user", "content": [
|
| 48 |
+
{"type": "text", "text": prompt},
|
| 49 |
+
{"type": "image", "image_url": f"data:image/jpeg;base64,{encoded_image}"}
|
| 50 |
+
]}
|
| 51 |
+
],
|
| 52 |
+
max_tokens=500
|
| 53 |
+
)
|
| 54 |
+
return response["choices"][0]["message"]["content"]
|
| 55 |
+
except Exception as e:
|
| 56 |
+
return f"GPT-4 Error: {e}"
|
| 57 |
+
|
| 58 |
+
# Function: AI Auto-Enhance Image
|
| 59 |
+
def auto_enhance_image(image, feedback):
|
| 60 |
+
if "dim lighting" in feedback.lower():
|
| 61 |
+
enhancer = ImageEnhance.Brightness(image)
|
| 62 |
+
image = enhancer.enhance(1.2) # Increase brightness by 20%
|
| 63 |
+
if "low contrast" in feedback.lower():
|
| 64 |
+
enhancer = ImageEnhance.Contrast(image)
|
| 65 |
+
image = enhancer.enhance(1.3)
|
| 66 |
+
if "blurry" in feedback.lower():
|
| 67 |
+
enhancer = ImageEnhance.Sharpness(image)
|
| 68 |
+
image = enhancer.enhance(2.0)
|
| 69 |
+
return image
|
| 70 |
+
|
| 71 |
+
# Streamlit UI
|
| 72 |
+
st.title("AI-Powered Image Evaluator & Enhancer")
|
| 73 |
+
|
| 74 |
+
uploaded_file = st.file_uploader("Upload an Image", type=["jpg", "png"])
|
| 75 |
+
if uploaded_file:
|
| 76 |
+
image = Image.open(uploaded_file)
|
| 77 |
+
st.image(image, caption="Uploaded Image", use_column_width=True)
|
| 78 |
+
|
| 79 |
+
# Aesthetic Scoring
|
| 80 |
+
aesthetic_score = predict_aesthetic_score(image)
|
| 81 |
+
st.write(f"**Aesthetic Score:** {aesthetic_score}/10")
|
| 82 |
+
|
| 83 |
+
# GPT-4 Feedback
|
| 84 |
+
prompt = "Evaluate this image. Does it match the description 'A futuristic cyberpunk city with neon lights and rain'? Analyze composition, color, and realism."
|
| 85 |
+
feedback = analyze_image_with_gpt4(image, prompt)
|
| 86 |
+
st.write("**AI Feedback:**", feedback)
|
| 87 |
+
|
| 88 |
+
# Auto-Enhancement Option
|
| 89 |
+
if st.button("Auto-Enhance Image"):
|
| 90 |
+
enhanced_image = auto_enhance_image(image, feedback)
|
| 91 |
+
st.image(enhanced_image, caption="Enhanced Image", use_column_width=True)
|
| 92 |
+
st.success("Image enhanced successfully!")
|
requirements.txt
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
torch
|
| 2 |
+
torchvision
|
| 3 |
+
timm
|
| 4 |
+
openai
|
| 5 |
+
clip-by-openai
|
| 6 |
+
pillow
|
| 7 |
+
streamlit
|
| 8 |
+
numpy
|