Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,20 +1,201 @@
|
|
|
|
1 |
|
|
|
2 |
|
3 |
-
|
4 |
-
from transformers import pipeline
|
5 |
|
6 |
-
|
|
|
|
|
7 |
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
|
|
|
|
|
|
18 |
|
19 |
-
|
20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from PIL import Image, ImageEnhance
|
2 |
|
3 |
+
from transformers import CLIPProcessor, CLIPModel
|
4 |
|
5 |
+
from io import BytesIO
|
|
|
6 |
|
7 |
+
# Set OpenAI API Key
|
8 |
+
import os
|
9 |
+
openai.api_key = os.getenv("OPENAI_API_KEY")
|
10 |
|
11 |
+
# Load CLIP model and processor
|
12 |
+
clip_model = CLIPModel.from_pretrained("openai/clip-vit-base-patch16")
|
13 |
+
clip_processor = CLIPProcessor.from_pretrained("openai/clip-vit-base-patch16")
|
14 |
|
15 |
+
# Expanded object labels
|
16 |
+
object_labels = [
|
17 |
+
"cat", "dog", "house", "tree", "car", "mountain", "flower", "bird", "person", "robot",
|
18 |
+
"a digital artwork", "a portrait", "a landscape", "a futuristic cityscape", "horse",
|
19 |
+
"lion", "tiger", "elephant", "giraffe", "airplane", "train", "ship", "book", "laptop",
|
20 |
+
"keyboard", "pen", "clock", "cup", "bottle", "backpack", "chair", "table", "sofa",
|
21 |
+
"bed", "building", "street", "forest", "desert", "waterfall", "sunset", "beach",
|
22 |
+
"bridge", "castle", "statue", "3D model"
|
23 |
+
]
|
24 |
|
25 |
+
# Example image for contrast check
|
26 |
+
EXAMPLE_IMAGE_URL = "https://www.watercoloraffair.com/wp-content/uploads/2023/04/monet-houses-of-parliament-low-key.jpg" # Square example image
|
27 |
+
example_image = Image.open(BytesIO(requests.get(EXAMPLE_IMAGE_URL).content))
|
28 |
+
|
29 |
+
# Function to handle chatbot queries with streaming
|
30 |
+
def process_chat(user_text):
|
31 |
+
if not user_text.strip():
|
32 |
+
yield "⚠️ Please enter a question."
|
33 |
+
return
|
34 |
+
|
35 |
+
stream = openai.ChatCompletion.create(
|
36 |
+
model="gpt-4",
|
37 |
+
messages=[
|
38 |
+
{"role": "system", "content": "You are a helpful assistant named Diane specializing in digital art advice."},
|
39 |
+
{"role": "user", "content": user_text}
|
40 |
+
],
|
41 |
+
stream=True
|
42 |
+
)
|
43 |
+
|
44 |
+
response = ""
|
45 |
+
for chunk in stream:
|
46 |
+
if chunk.choices[0].delta.get("content"):
|
47 |
+
token = chunk.choices[0].delta["content"]
|
48 |
+
response += token
|
49 |
+
yield response
|
50 |
+
|
51 |
+
# Function to analyze image contrast
|
52 |
+
def analyze_contrast_opencv(image_path):
|
53 |
+
img = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)
|
54 |
+
contrast = img.std()
|
55 |
+
return contrast
|
56 |
+
|
57 |
+
# Function to identify objects using CLIP
|
58 |
+
def identify_objects_with_clip(image_path):
|
59 |
+
image = Image.open(image_path).convert("RGB")
|
60 |
+
inputs = clip_processor(text=object_labels, images=image, return_tensors="pt", padding=True)
|
61 |
+
with torch.no_grad():
|
62 |
+
outputs = clip_model(**inputs)
|
63 |
+
logits_per_image = outputs.logits_per_image
|
64 |
+
probs = logits_per_image.softmax(dim=1).numpy().flatten()
|
65 |
+
best_match_label = object_labels[probs.argmax()]
|
66 |
+
return best_match_label
|
67 |
+
|
68 |
+
# Function to enhance image contrast
|
69 |
+
def enhance_contrast(image):
|
70 |
+
enhancer = ImageEnhance.Contrast(image)
|
71 |
+
enhanced_image = enhancer.enhance(1.5)
|
72 |
+
enhanced_path = "enhanced_image.png"
|
73 |
+
enhanced_image.save(enhanced_path)
|
74 |
+
return enhanced_path
|
75 |
+
|
76 |
+
# Function to provide additional suggestions with streaming
|
77 |
+
def provide_suggestions_streaming(object_identified):
|
78 |
+
if not object_identified:
|
79 |
+
yield "Sorry, I couldn't find an object in your artwork. Try a different image."
|
80 |
+
return
|
81 |
+
|
82 |
+
stream = openai.ChatCompletion.create(
|
83 |
+
model="gpt-4",
|
84 |
+
messages=[
|
85 |
+
{"role": "system", "content": "You are an expert digital art advisor."},
|
86 |
+
{"role": "user", "content": f"Suggest ways to improve a digital artwork featuring a {object_identified}."}
|
87 |
+
],
|
88 |
+
stream=True
|
89 |
+
)
|
90 |
+
|
91 |
+
response = ""
|
92 |
+
for chunk in stream:
|
93 |
+
if chunk.choices[0].delta.get("content"):
|
94 |
+
token = chunk.choices[0].delta["content"]
|
95 |
+
response += token
|
96 |
+
yield response
|
97 |
+
|
98 |
+
# Main image processing function
|
99 |
+
def process_image(image):
|
100 |
+
if not image:
|
101 |
+
return "⚠️ Please upload an image.", None, None
|
102 |
+
image.save("uploaded_image.png")
|
103 |
+
contrast = analyze_contrast_opencv("uploaded_image.png")
|
104 |
+
object_identified = identify_objects_with_clip("uploaded_image.png")
|
105 |
+
if contrast < 25:
|
106 |
+
enhanced_image_path = enhance_contrast(Image.open("uploaded_image.png"))
|
107 |
+
return (
|
108 |
+
f"Hey, great artwork of {object_identified}! However, it looks like the contrast is a little low. I've improved the contrast for you. ✨",
|
109 |
+
enhanced_image_path,
|
110 |
+
object_identified
|
111 |
+
)
|
112 |
+
return (
|
113 |
+
f"Hey, great artwork of {object_identified}! Looks like the color contrast is great. Be proud of yourself! 🌟",
|
114 |
+
None,
|
115 |
+
object_identified
|
116 |
+
)
|
117 |
+
|
118 |
+
# Gradio Blocks Interface
|
119 |
+
demo = gr.Blocks(css="""
|
120 |
+
#upload-image, #example-image {
|
121 |
+
height: 300px !important;
|
122 |
+
}
|
123 |
+
.button {
|
124 |
+
height: 50px;
|
125 |
+
font-size: 16px;
|
126 |
+
}
|
127 |
+
""")
|
128 |
+
|
129 |
+
with demo:
|
130 |
+
gr.Markdown("## 🎨 DIANE (Digital Imaging and Art Neural Enhancer)")
|
131 |
+
gr.Markdown("DIANE is here to assist you in refining your digital art. She can answer questions about digital art, analyze your images, and provide creative suggestions to enhance your work.")
|
132 |
+
|
133 |
+
# Chatbot Section
|
134 |
+
with gr.Row():
|
135 |
+
with gr.Column():
|
136 |
+
gr.Markdown("### 💬 Ask me about digital art")
|
137 |
+
user_text = gr.Textbox(label="Enter your question", placeholder="What is the best tool for a beginner?...")
|
138 |
+
chat_output = gr.Textbox(label="Answer", interactive=False)
|
139 |
+
chat_button = gr.Button("Ask", elem_classes="button")
|
140 |
+
|
141 |
+
chat_button.click(process_chat, inputs=user_text, outputs=chat_output)
|
142 |
+
|
143 |
+
# Image Analysis Section
|
144 |
+
with gr.Row():
|
145 |
+
with gr.Column():
|
146 |
+
gr.Markdown("### 🖼️ Upload an image to check its contrast levels")
|
147 |
+
with gr.Row(equal_height=True):
|
148 |
+
# Left: Image upload field
|
149 |
+
with gr.Column():
|
150 |
+
image_input = gr.Image(label="Upload an image", type="pil", elem_id="upload-image")
|
151 |
+
image_button = gr.Button("Check", elem_classes="button")
|
152 |
+
|
153 |
+
# Right: Example image field
|
154 |
+
with gr.Column():
|
155 |
+
gr.Image(value=example_image, label="Example Image", interactive=False, elem_id="example-image")
|
156 |
+
example_button = gr.Button("Use Example Image", elem_classes="button")
|
157 |
+
image_output_text = gr.Textbox(label="Analysis", interactive=False)
|
158 |
+
image_output_image = gr.Image(label="Improved Image", interactive=False)
|
159 |
+
suggestion_button = gr.Button("I want to improve this artwork. Any suggestions?", visible=False)
|
160 |
+
suggestions_output = gr.Textbox(label="Suggestions", interactive=True)
|
161 |
+
state_object = gr.State() # To store identified object
|
162 |
+
|
163 |
+
# Load example image into the input
|
164 |
+
def use_example_image():
|
165 |
+
return example_image
|
166 |
+
|
167 |
+
example_button.click(
|
168 |
+
use_example_image,
|
169 |
+
inputs=None,
|
170 |
+
outputs=image_input
|
171 |
+
)
|
172 |
+
|
173 |
+
# Analyze button
|
174 |
+
def update_suggestions_visibility(analysis, enhanced_image, identified_object):
|
175 |
+
return gr.update(visible=True), analysis, enhanced_image
|
176 |
+
|
177 |
+
image_button.click(
|
178 |
+
process_image,
|
179 |
+
inputs=image_input,
|
180 |
+
outputs=[
|
181 |
+
image_output_text,
|
182 |
+
image_output_image,
|
183 |
+
state_object
|
184 |
+
]
|
185 |
+
)
|
186 |
+
|
187 |
+
# Automatically enable suggestions after image processing
|
188 |
+
image_button.click(
|
189 |
+
update_suggestions_visibility,
|
190 |
+
inputs=[image_output_text, image_output_image, state_object],
|
191 |
+
outputs=[suggestion_button, image_output_text, image_output_image]
|
192 |
+
)
|
193 |
+
|
194 |
+
# Suggestion button functionality with streaming
|
195 |
+
suggestion_button.click(
|
196 |
+
provide_suggestions_streaming,
|
197 |
+
inputs=state_object,
|
198 |
+
outputs=suggestions_output
|
199 |
+
)
|
200 |
+
|
201 |
+
demo.launch()
|