Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -35,117 +35,29 @@ def upload_image(image_data_url):
|
|
35 |
|
36 |
return client.files.upload(file=temp_filepath)
|
37 |
|
38 |
-
def
|
39 |
-
"""
|
40 |
-
action_verbs = {'remove', 'delete', 'erase', 'eliminate'}
|
41 |
-
words = object_type.lower().split()
|
42 |
-
filtered_words = [word for word in words if word not in action_verbs]
|
43 |
-
return ' '.join(filtered_words) if filtered_words else object_type.lower()
|
44 |
-
|
45 |
-
def check_if_person_animal(uploaded_file, object_type):
|
46 |
-
"""Check if the object to remove is a person or animal"""
|
47 |
-
model = "gemini-2.0-flash-lite"
|
48 |
-
parts = [
|
49 |
-
types.Part.from_uri(file_uri=uploaded_file.uri, mime_type=uploaded_file.mime_type),
|
50 |
-
types.Part.from_text(text=f"Remove {object_type}")
|
51 |
-
]
|
52 |
-
contents = [types.Content(role="user", parts=parts)]
|
53 |
-
generate_content_config = types.GenerateContentConfig(
|
54 |
-
system_instruction=[
|
55 |
-
types.Part.from_text(text="""Determine if the user wants to remove a person or animal.
|
56 |
-
Respond ONLY with 'Yes' or 'No'. Examples:
|
57 |
-
- Remove person → Yes
|
58 |
-
- Remove dog → Yes
|
59 |
-
- Remove sunglasses → No""")
|
60 |
-
],
|
61 |
-
temperature=0.0,
|
62 |
-
max_output_tokens=1,
|
63 |
-
)
|
64 |
-
try:
|
65 |
-
response = client.models.generate_content(
|
66 |
-
model=model,
|
67 |
-
contents=contents,
|
68 |
-
config=generate_content_config
|
69 |
-
)
|
70 |
-
if response.candidates and response.candidates[0].content.parts:
|
71 |
-
return response.candidates[0].content.parts[0].text.strip().lower() == "yes"
|
72 |
-
return False
|
73 |
-
except Exception as e:
|
74 |
-
print(f"Error checking person/animal: {str(e)}")
|
75 |
-
return False
|
76 |
-
|
77 |
-
def check_other_entities(uploaded_file, object_type):
|
78 |
-
"""Check if image contains other people/animals"""
|
79 |
model = "gemini-2.0-flash-lite"
|
80 |
parts = [
|
81 |
types.Part.from_uri(file_uri=uploaded_file.uri, mime_type=uploaded_file.mime_type),
|
82 |
types.Part.from_text(text=f"Remove {object_type}")
|
83 |
]
|
|
|
84 |
contents = [types.Content(role="user", parts=parts)]
|
|
|
85 |
generate_content_config = types.GenerateContentConfig(
|
86 |
system_instruction=[
|
87 |
-
types.Part.from_text(text=
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
try:
|
94 |
-
response = client.models.generate_content(
|
95 |
-
model=model,
|
96 |
-
contents=contents,
|
97 |
-
config=generate_content_config
|
98 |
-
)
|
99 |
-
if response.candidates and response.candidates[0].content.parts:
|
100 |
-
return response.candidates[0].content.parts[0].text.strip().lower() == "yes"
|
101 |
-
return False
|
102 |
-
except Exception as e:
|
103 |
-
print(f"Error checking other entities: {str(e)}")
|
104 |
-
return False
|
105 |
-
|
106 |
-
def check_sunglasses_state(uploaded_file):
|
107 |
-
"""Check if sunglasses are being worn"""
|
108 |
-
model = "gemini-2.0-flash-lite"
|
109 |
-
parts = [
|
110 |
-
types.Part.from_uri(file_uri=uploaded_file.uri, mime_type=uploaded_file.mime_type),
|
111 |
-
types.Part.from_text(text="Are sunglasses being worn in this image?")
|
112 |
-
]
|
113 |
-
contents = [types.Content(role="user", parts=parts)]
|
114 |
-
generate_content_config = types.GenerateContentConfig(
|
115 |
-
system_instruction=[
|
116 |
-
types.Part.from_text(text="Respond ONLY with 'Yes' or 'No'.")
|
117 |
-
],
|
118 |
-
temperature=0.0,
|
119 |
-
max_output_tokens=1,
|
120 |
-
)
|
121 |
-
try:
|
122 |
-
response = client.models.generate_content(
|
123 |
-
model=model,
|
124 |
-
contents=contents,
|
125 |
-
config=generate_content_config
|
126 |
-
)
|
127 |
-
if response.candidates and response.candidates[0].content.parts:
|
128 |
-
return response.candidates[0].content.parts[0].text.strip().lower() == "yes"
|
129 |
-
return False
|
130 |
-
except Exception as e:
|
131 |
-
print(f"Error checking sunglasses: {str(e)}")
|
132 |
-
return False
|
133 |
-
|
134 |
-
def check_phone_state(uploaded_file):
|
135 |
-
"""Check if phone is in hand"""
|
136 |
-
model = "gemini-2.0-flash-lite"
|
137 |
-
parts = [
|
138 |
-
types.Part.from_uri(file_uri=uploaded_file.uri, mime_type=uploaded_file.mime_type),
|
139 |
-
types.Part.from_text(text="Is a phone being held in hand?")
|
140 |
-
]
|
141 |
-
contents = [types.Content(role="user", parts=parts)]
|
142 |
-
generate_content_config = types.GenerateContentConfig(
|
143 |
-
system_instruction=[
|
144 |
-
types.Part.from_text(text="Respond ONLY with 'Yes' or 'No'.")
|
145 |
],
|
146 |
temperature=0.0,
|
147 |
max_output_tokens=1,
|
148 |
)
|
|
|
149 |
try:
|
150 |
response = client.models.generate_content(
|
151 |
model=model,
|
@@ -156,25 +68,30 @@ def check_phone_state(uploaded_file):
|
|
156 |
return response.candidates[0].content.parts[0].text.strip().lower() == "yes"
|
157 |
return False
|
158 |
except Exception as e:
|
159 |
-
print(f"
|
160 |
return False
|
161 |
|
162 |
def generate_gemini_output(object_type, uploaded_file):
|
163 |
-
"""Generate image using
|
164 |
model = "gemini-2.0-flash-exp-image-generation"
|
165 |
parts = [
|
166 |
types.Part.from_uri(file_uri=uploaded_file.uri, mime_type=uploaded_file.mime_type),
|
167 |
types.Part.from_text(text=f"Remove {object_type} from the image")
|
168 |
]
|
|
|
169 |
contents = [types.Content(role="user", parts=parts)]
|
|
|
170 |
generate_content_config = types.GenerateContentConfig(
|
171 |
temperature=1,
|
172 |
top_p=0.95,
|
173 |
top_k=40,
|
174 |
max_output_tokens=8192,
|
175 |
response_modalities=["image", "text"],
|
176 |
-
safety_settings=[
|
|
|
|
|
177 |
)
|
|
|
178 |
result_image = None
|
179 |
for chunk in client.models.generate_content_stream(
|
180 |
model=model,
|
@@ -190,6 +107,7 @@ def generate_gemini_output(object_type, uploaded_file):
|
|
190 |
with open(result_image_path, "wb") as f:
|
191 |
f.write(part.inline_data.data)
|
192 |
result_image = result_image_path
|
|
|
193 |
return result_image
|
194 |
|
195 |
@app.route("/")
|
@@ -206,44 +124,22 @@ def process():
|
|
206 |
if not image_data or not object_type:
|
207 |
return jsonify({"success": False, "message": "Missing required data"}), 400
|
208 |
|
|
|
209 |
uploaded_file = upload_image(image_data)
|
210 |
-
|
211 |
-
|
212 |
-
|
213 |
-
if normalized_object == 'eyes':
|
214 |
return jsonify({
|
215 |
"success": False,
|
216 |
-
"message": "Sorry, I can't assist with removing
|
217 |
}), 400
|
218 |
-
|
219 |
-
#
|
220 |
-
if normalized_object == 'sunglasses':
|
221 |
-
if check_sunglasses_state(uploaded_file):
|
222 |
-
return jsonify({
|
223 |
-
"success": False,
|
224 |
-
"message": "Can't remove sunglasses while being worn."
|
225 |
-
}), 400
|
226 |
-
|
227 |
-
if normalized_object == 'phone':
|
228 |
-
if check_phone_state(uploaded_file):
|
229 |
-
return jsonify({
|
230 |
-
"success": False,
|
231 |
-
"message": "Can't remove phones while being held."
|
232 |
-
}), 400
|
233 |
-
|
234 |
-
# Person/animal checks
|
235 |
-
if check_if_person_animal(uploaded_file, normalized_object):
|
236 |
-
if check_other_entities(uploaded_file, normalized_object):
|
237 |
-
return jsonify({
|
238 |
-
"success": False,
|
239 |
-
"message": "Can't remove people/animals when others are present."
|
240 |
-
}), 400
|
241 |
-
|
242 |
-
# Generate output
|
243 |
result_image = generate_gemini_output(object_type, uploaded_file)
|
|
|
244 |
if not result_image:
|
245 |
return jsonify({"success": False, "message": "Failed to generate image"}), 500
|
246 |
-
|
247 |
return jsonify({
|
248 |
"success": True,
|
249 |
"resultPath": f"/static/{os.path.basename(result_image)}"
|
|
|
35 |
|
36 |
return client.files.upload(file=temp_filepath)
|
37 |
|
38 |
+
def is_prohibited_request(uploaded_file, object_type):
|
39 |
+
"""Check if request is to remove person/animal using gemini-2.0-flash-lite"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
40 |
model = "gemini-2.0-flash-lite"
|
41 |
parts = [
|
42 |
types.Part.from_uri(file_uri=uploaded_file.uri, mime_type=uploaded_file.mime_type),
|
43 |
types.Part.from_text(text=f"Remove {object_type}")
|
44 |
]
|
45 |
+
|
46 |
contents = [types.Content(role="user", parts=parts)]
|
47 |
+
|
48 |
generate_content_config = types.GenerateContentConfig(
|
49 |
system_instruction=[
|
50 |
+
types.Part.from_text(text="""Your ai find user ask about remove person or animal like
|
51 |
+
Remove hair
|
52 |
+
Remove mouth
|
53 |
+
Remove sunglasses form person
|
54 |
+
Remove phone thet person
|
55 |
+
If user ask remove person or animal user asking remove thet person or animal say yes or no """)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
56 |
],
|
57 |
temperature=0.0,
|
58 |
max_output_tokens=1,
|
59 |
)
|
60 |
+
|
61 |
try:
|
62 |
response = client.models.generate_content(
|
63 |
model=model,
|
|
|
68 |
return response.candidates[0].content.parts[0].text.strip().lower() == "yes"
|
69 |
return False
|
70 |
except Exception as e:
|
71 |
+
print(f"Prohibition check error: {str(e)}")
|
72 |
return False
|
73 |
|
74 |
def generate_gemini_output(object_type, uploaded_file):
|
75 |
+
"""Generate image using gemini-2.0-flash-exp-image-generation"""
|
76 |
model = "gemini-2.0-flash-exp-image-generation"
|
77 |
parts = [
|
78 |
types.Part.from_uri(file_uri=uploaded_file.uri, mime_type=uploaded_file.mime_type),
|
79 |
types.Part.from_text(text=f"Remove {object_type} from the image")
|
80 |
]
|
81 |
+
|
82 |
contents = [types.Content(role="user", parts=parts)]
|
83 |
+
|
84 |
generate_content_config = types.GenerateContentConfig(
|
85 |
temperature=1,
|
86 |
top_p=0.95,
|
87 |
top_k=40,
|
88 |
max_output_tokens=8192,
|
89 |
response_modalities=["image", "text"],
|
90 |
+
safety_settings=[
|
91 |
+
types.SafetySetting(category="HARM_CATEGORY_CIVIC_INTEGRITY", threshold="OFF"),
|
92 |
+
],
|
93 |
)
|
94 |
+
|
95 |
result_image = None
|
96 |
for chunk in client.models.generate_content_stream(
|
97 |
model=model,
|
|
|
107 |
with open(result_image_path, "wb") as f:
|
108 |
f.write(part.inline_data.data)
|
109 |
result_image = result_image_path
|
110 |
+
|
111 |
return result_image
|
112 |
|
113 |
@app.route("/")
|
|
|
124 |
if not image_data or not object_type:
|
125 |
return jsonify({"success": False, "message": "Missing required data"}), 400
|
126 |
|
127 |
+
# Upload image once
|
128 |
uploaded_file = upload_image(image_data)
|
129 |
+
|
130 |
+
# Check for prohibited requests
|
131 |
+
if is_prohibited_request(uploaded_file, object_type):
|
|
|
132 |
return jsonify({
|
133 |
"success": False,
|
134 |
+
"message": "Sorry, I can't assist with removing people or animals."
|
135 |
}), 400
|
136 |
+
|
137 |
+
# Generate output if allowed
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
138 |
result_image = generate_gemini_output(object_type, uploaded_file)
|
139 |
+
|
140 |
if not result_image:
|
141 |
return jsonify({"success": False, "message": "Failed to generate image"}), 500
|
142 |
+
|
143 |
return jsonify({
|
144 |
"success": True,
|
145 |
"resultPath": f"/static/{os.path.basename(result_image)}"
|