Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,166 +1,83 @@
|
|
1 |
-
import
|
|
|
2 |
import os
|
3 |
-
import time
|
4 |
-
import uuid
|
5 |
import tempfile
|
6 |
-
from PIL import Image
|
7 |
-
import gradio as gr
|
8 |
import base64
|
9 |
-
import
|
10 |
|
11 |
-
|
12 |
-
|
13 |
|
14 |
-
|
15 |
-
|
16 |
-
f.write(data)
|
17 |
|
18 |
-
|
19 |
-
|
20 |
-
client = genai.Client(api_key=(api_key.strip() if api_key and api_key.strip() != ""
|
21 |
-
else os.environ.get("GEMINI_API_KEY")))
|
22 |
-
|
23 |
-
files = [
|
24 |
-
client.files.upload(file=file_name),
|
25 |
-
]
|
26 |
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
parts=[
|
31 |
-
types.Part.from_uri(
|
32 |
-
file_uri=files[0].uri,
|
33 |
-
mime_type=files[0].mime_type,
|
34 |
-
),
|
35 |
-
types.Part.from_text(text=text),
|
36 |
-
],
|
37 |
-
),
|
38 |
-
]
|
39 |
-
generate_content_config = types.GenerateContentConfig(
|
40 |
-
temperature=1,
|
41 |
-
top_p=0.95,
|
42 |
-
top_k=40,
|
43 |
-
max_output_tokens=8192,
|
44 |
-
response_modalities=[
|
45 |
-
"image",
|
46 |
-
"text",
|
47 |
-
],
|
48 |
-
response_mime_type="text/plain",
|
49 |
-
)
|
50 |
-
|
51 |
-
with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as tmp:
|
52 |
-
temp_path = tmp.name
|
53 |
-
for chunk in client.models.generate_content_stream(
|
54 |
-
model=model,
|
55 |
-
contents=contents,
|
56 |
-
config=generate_content_config,
|
57 |
-
):
|
58 |
-
if not chunk.candidates or not chunk.candidates[0].content or not chunk.candidates[0].content.parts:
|
59 |
-
continue
|
60 |
-
inline_data = chunk.candidates[0].content.parts[0].inline_data
|
61 |
-
if inline_data:
|
62 |
-
save_binary_file(temp_path, inline_data.data)
|
63 |
-
print(
|
64 |
-
"File of mime type "
|
65 |
-
f"{inline_data.mime_type} saved to: {temp_path} and prompt input :{text}"
|
66 |
-
)
|
67 |
-
else:
|
68 |
-
print(chunk.text)
|
69 |
-
|
70 |
-
del files
|
71 |
-
return temp_path
|
72 |
-
|
73 |
-
|
74 |
-
def process_image_and_prompt(composite_pil, prompt, gemini_api_key):
|
75 |
-
# Save the composite image to a temporary file.
|
76 |
-
with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as tmp:
|
77 |
-
composite_path = tmp.name
|
78 |
-
composite_pil.save(composite_path)
|
79 |
|
80 |
-
|
81 |
-
|
82 |
-
|
|
|
|
|
|
|
|
|
83 |
|
84 |
-
|
85 |
-
|
86 |
-
result_img = Image.open(gemma_edited_image_path)
|
87 |
-
if result_img.mode == "RGBA":
|
88 |
-
result_img = result_img.convert("RGB")
|
89 |
-
return [result_img]
|
90 |
|
91 |
-
#
|
92 |
-
|
93 |
-
# HTML Header for the application.
|
94 |
-
gr.HTML(
|
95 |
-
"""
|
96 |
-
<div style='display: flex; align-items: center; justify-content: center; gap: 20px'>
|
97 |
-
<div style="background-color: var(--block-background-fill); border-radius: 8px">
|
98 |
-
<img src="https://www.gstatic.com/lamda/images/gemini_favicon_f069958c85030456e93de685481c559f160ea06b.png" style="width: 100px; height: 100px;">
|
99 |
-
</div>
|
100 |
-
<div>
|
101 |
-
<h1>Gen AI Image Editing</h1>
|
102 |
-
<p>Gemini using for Image Editing</p>
|
103 |
-
<p>Powered by <a href="https://gradio.app/">Gradio</a> ⚡️</p>
|
104 |
-
<p>Get an API Key <a href="https://aistudio.google.com/apikey">here</a></p>
|
105 |
-
<p>Follow me on Twitter: <a href="https://x.com/Ameerazam18">Ameerazam18</a></p>
|
106 |
-
</div>
|
107 |
-
</div>
|
108 |
-
"""
|
109 |
-
)
|
110 |
|
111 |
-
|
|
|
|
|
|
|
|
|
112 |
|
113 |
-
|
114 |
-
|
115 |
-
# Each example is a list corresponding to the inputs:
|
116 |
-
# [Input Image, Prompt, Guidance Scale, Number of Steps, LoRA Name]
|
117 |
-
["data/1.webp", 'change text to "AMEER"'],
|
118 |
-
["data/2.webp", "remove the spoon from hand only"],
|
119 |
-
["data/3.webp", 'change text to "Make it "'],
|
120 |
-
["data/1.jpg", "add joker style only on face"],
|
121 |
-
["data/1777043.jpg", "add joker style only on face"],
|
122 |
-
["data/2807615.jpg","add lipstick on lip only "],
|
123 |
-
|
124 |
-
["data/76860.jpg", "add lipstick on lip only "],
|
125 |
-
["data/2807615.jpg", "make it happy looking face only"],
|
126 |
-
|
127 |
-
|
128 |
-
]
|
129 |
|
130 |
-
|
|
|
131 |
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
|
|
|
|
|
1 |
+
from flask import Flask, render_template, request, jsonify
|
2 |
+
import google.generativeai as genai
|
3 |
import os
|
|
|
|
|
4 |
import tempfile
|
|
|
|
|
5 |
import base64
|
6 |
+
from dotenv import load_dotenv
|
7 |
|
8 |
+
# Load environment variables
|
9 |
+
load_dotenv()
|
10 |
|
11 |
+
# Configure Flask app
|
12 |
+
app = Flask(__name__)
|
|
|
13 |
|
14 |
+
# Configure Gemini API
|
15 |
+
genai.configure(api_key=os.getenv("GEMINI_API_KEY"))
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
|
17 |
+
@app.route("/")
|
18 |
+
def home():
|
19 |
+
return render_template("index.html")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
|
21 |
+
@app.route("/process", methods=["POST"])
|
22 |
+
def process_image():
|
23 |
+
try:
|
24 |
+
# Get data from request
|
25 |
+
data = request.json
|
26 |
+
image_data = data.get("image")
|
27 |
+
object_type = data.get("objectType")
|
28 |
|
29 |
+
if not image_data or not object_type:
|
30 |
+
return jsonify({"success": False, "message": "Invalid input data"})
|
|
|
|
|
|
|
|
|
31 |
|
32 |
+
# Decode base64 image data
|
33 |
+
image_bytes = base64.b64decode(image_data.split(",")[1])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
34 |
|
35 |
+
# Create temporary directory
|
36 |
+
temp_dir = tempfile.mkdtemp()
|
37 |
+
input_path = os.path.join(temp_dir, "input.png")
|
38 |
+
with open(input_path, "wb") as f:
|
39 |
+
f.write(image_bytes)
|
40 |
|
41 |
+
# Create the model
|
42 |
+
model = genai.GenerativeModel('gemini-2.0-flash-exp-image-generation')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
43 |
|
44 |
+
# Build the prompt
|
45 |
+
prompt = f"Remove the {object_type} from the image and fill the area naturally."
|
46 |
|
47 |
+
# Generate content
|
48 |
+
response = model.generate_content(
|
49 |
+
[
|
50 |
+
prompt,
|
51 |
+
genai.upload_file(input_path)
|
52 |
+
],
|
53 |
+
generation_config={
|
54 |
+
"temperature": 1,
|
55 |
+
"top_p": 0.95,
|
56 |
+
"top_k": 40,
|
57 |
+
"max_output_tokens": 8192,
|
58 |
+
},
|
59 |
+
safety_settings={
|
60 |
+
"HARM_CATEGORY_CIVIC_INTEGRITY": "BLOCK_NONE"
|
61 |
+
}
|
62 |
+
)
|
63 |
+
|
64 |
+
# Process response
|
65 |
+
output_path = os.path.join(temp_dir, "result.png")
|
66 |
+
for chunk in response:
|
67 |
+
if chunk.candidates:
|
68 |
+
for part in chunk.candidates[0].content.parts:
|
69 |
+
if hasattr(part, 'inline_data'):
|
70 |
+
with open(output_path, "wb") as f:
|
71 |
+
f.write(part.inline_data.data)
|
72 |
+
return jsonify({
|
73 |
+
"success": True,
|
74 |
+
"resultPath": output_path
|
75 |
+
})
|
76 |
+
|
77 |
+
return jsonify({"success": False, "message": "No valid image data found in response"})
|
78 |
+
|
79 |
+
except Exception as e:
|
80 |
+
return jsonify({"success": False, "message": str(e)})
|
81 |
+
|
82 |
+
if __name__ == "__main__":
|
83 |
+
app.run(host="0.0.0.0", port=5000)
|