Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -10,154 +10,177 @@ import mimetypes
|
|
10 |
|
11 |
from google import genai
|
12 |
from google.genai import types
|
|
|
13 |
|
14 |
-
#
|
15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
|
17 |
def save_binary_file(file_name, data):
|
18 |
with open(file_name, "wb") as f:
|
19 |
f.write(data)
|
20 |
|
21 |
-
def generate(text, file_name, model="gemini-
|
22 |
-
#
|
23 |
-
|
|
|
24 |
|
25 |
-
|
26 |
-
|
27 |
-
]
|
28 |
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
),
|
37 |
-
types.Part.from_text(text=text),
|
38 |
-
],
|
39 |
-
),
|
40 |
]
|
41 |
-
|
|
|
|
|
42 |
temperature=1,
|
43 |
top_p=0.95,
|
44 |
top_k=40,
|
45 |
max_output_tokens=8192,
|
46 |
-
|
47 |
-
"image",
|
48 |
-
"text",
|
49 |
-
],
|
50 |
-
response_mime_type="text/plain",
|
51 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
52 |
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
model=model,
|
57 |
-
contents=contents,
|
58 |
-
config=generate_content_config,
|
59 |
-
):
|
60 |
-
if not chunk.candidates or not chunk.candidates[0].content or not chunk.candidates[0].content.parts:
|
61 |
-
continue
|
62 |
-
inline_data = chunk.candidates[0].content.parts[0].inline_data
|
63 |
-
if inline_data:
|
64 |
-
save_binary_file(temp_path, inline_data.data)
|
65 |
-
print(
|
66 |
-
"File of mime type "
|
67 |
-
f"{inline_data.mime_type} saved to: {temp_path} and prompt input :{text}"
|
68 |
-
)
|
69 |
-
else:
|
70 |
-
print(chunk.text)
|
71 |
|
72 |
-
del files
|
73 |
-
return temp_path
|
74 |
-
|
75 |
|
76 |
def process_image_and_prompt(composite_pil, prompt):
|
77 |
-
|
|
|
|
|
|
|
|
|
|
|
78 |
with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as tmp:
|
79 |
composite_path = tmp.name
|
80 |
-
|
|
|
|
|
|
|
81 |
|
82 |
file_name = composite_path
|
83 |
input_text = prompt
|
84 |
-
model = "gemini-
|
85 |
-
|
86 |
-
|
87 |
-
print("
|
88 |
-
|
89 |
-
if
|
90 |
-
result_img =
|
91 |
-
|
92 |
-
|
93 |
-
#
|
94 |
-
|
95 |
-
|
96 |
-
|
|
|
|
|
|
|
97 |
"""
|
98 |
-
<div style='
|
99 |
-
|
100 |
-
|
101 |
-
</div>
|
102 |
-
<div>
|
103 |
-
<h1>Gen AI Image Editing</h1>
|
104 |
-
<p>Gemini using for Image Editing</p>
|
105 |
-
<p>Powered by <a href="https://gradio.app/">Gradio</a> ⚡️</p>
|
106 |
-
<p>Get an API Key <a href="https://aistudio.google.com/apikey">here</a></p>
|
107 |
-
<p>Follow me on Instagram: <a href="https://www.instagram.com/nihal_gazi_io/">nihal_gazi_io</a></p>
|
108 |
-
<p>Or contact me through Discord: <b><i>@nihal_gazi_io</i></b></p>
|
109 |
-
</div>
|
110 |
</div>
|
111 |
"""
|
112 |
)
|
113 |
|
114 |
-
#
|
115 |
examples = [
|
116 |
-
|
117 |
-
|
118 |
-
["
|
119 |
-
["
|
120 |
-
["
|
121 |
-
["
|
122 |
-
["data/1777043.jpg", "add joker style only on face"],
|
123 |
-
["data/2807615.jpg","add lipstick on lip only "],
|
124 |
-
|
125 |
-
["data/76860.jpg", "add lipstick on lip only "],
|
126 |
-
["data/2807615.jpg", "make it happy looking face only"],
|
127 |
-
|
128 |
-
|
129 |
]
|
130 |
|
131 |
-
gr.
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
151 |
submit_btn.click(
|
152 |
fn=process_image_and_prompt,
|
153 |
-
inputs=[image_input, prompt_input],
|
154 |
outputs=output_gallery,
|
155 |
-
|
156 |
-
)
|
157 |
-
gr.Examples(
|
158 |
-
examples=examples,
|
159 |
-
inputs=[image_input, prompt_input], # Inputs se API key hata di gayi hai
|
160 |
-
label="Try these examples"
|
161 |
)
|
162 |
|
163 |
-
demo.launch(share=True)
|
|
|
10 |
|
11 |
from google import genai
|
12 |
from google.genai import types
|
13 |
+
from dotenv import load_dotenv
|
14 |
|
15 |
+
# .env file se environment variables (API_KEY) load karein
|
16 |
+
load_dotenv()
|
17 |
+
|
18 |
+
def get_api_key():
|
19 |
+
"""Environment variable se Gemini API Key haasil karein."""
|
20 |
+
api_key = os.environ.get("GEMINI_API_KEY")
|
21 |
+
if not api_key:
|
22 |
+
# Agar key na mile to Gradio app mein ek error dikhayein
|
23 |
+
raise gr.Error("Gemini API Key nahi mili. Kripya apni .env file check karein.")
|
24 |
+
return api_key
|
25 |
|
26 |
def save_binary_file(file_name, data):
|
27 |
with open(file_name, "wb") as f:
|
28 |
f.write(data)
|
29 |
|
30 |
+
def generate(text, file_name, model="gemini-1.5-flash-latest"): # Model ko update kar diya gaya hai
|
31 |
+
# API key environment se lein
|
32 |
+
api_key = get_api_key()
|
33 |
+
genai.configure(api_key=api_key)
|
34 |
|
35 |
+
# Model ko initialize karein
|
36 |
+
model_instance = genai.GenerativeModel(model)
|
|
|
37 |
|
38 |
+
# File upload karein (naya, behtar tareeka)
|
39 |
+
uploaded_file = genai.upload_file(path=file_name)
|
40 |
+
|
41 |
+
# Prompt tayyar karein
|
42 |
+
prompt_parts = [
|
43 |
+
uploaded_file,
|
44 |
+
text,
|
|
|
|
|
|
|
|
|
45 |
]
|
46 |
+
|
47 |
+
# Generation config (isey zaroorat ke mutabik rakha ja sakta hai)
|
48 |
+
generation_config = genai.GenerationConfig(
|
49 |
temperature=1,
|
50 |
top_p=0.95,
|
51 |
top_k=40,
|
52 |
max_output_tokens=8192,
|
53 |
+
response_mime_type="image/png" # Seedhe image response maangein
|
|
|
|
|
|
|
|
|
54 |
)
|
55 |
+
|
56 |
+
try:
|
57 |
+
response = model_instance.generate_content(
|
58 |
+
prompt_parts,
|
59 |
+
generation_config=generation_config,
|
60 |
+
stream=True
|
61 |
+
)
|
62 |
+
|
63 |
+
# Response se image data collect karein
|
64 |
+
image_data = bytearray()
|
65 |
+
for chunk in response:
|
66 |
+
if chunk.parts:
|
67 |
+
for part in chunk.parts:
|
68 |
+
if part.inline_data:
|
69 |
+
image_data.extend(part.inline_data.data)
|
70 |
+
|
71 |
+
if not image_data:
|
72 |
+
# Agar koi image generate na ho, to text response check karein
|
73 |
+
full_response_text = "".join(chunk.text for chunk in response)
|
74 |
+
print("Model se koi image nahi aayi. Response:", full_response_text)
|
75 |
+
raise gr.Error("Maaf kijiye, image generate nahi ho saki. Kripya apna prompt badal kar koshish karein.")
|
76 |
+
|
77 |
+
# Image data ko temporary file mein save karein
|
78 |
+
with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as tmp:
|
79 |
+
temp_path = tmp.name
|
80 |
+
tmp.write(image_data)
|
81 |
+
|
82 |
+
print(f"File {temp_path} par save ho gayi. Prompt: {text}")
|
83 |
+
return temp_path
|
84 |
|
85 |
+
finally:
|
86 |
+
# Temporary uploaded file ko delete karein
|
87 |
+
genai.delete_file(uploaded_file.name)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
88 |
|
|
|
|
|
|
|
89 |
|
90 |
def process_image_and_prompt(composite_pil, prompt):
|
91 |
+
if composite_pil is None:
|
92 |
+
raise gr.Error("Kripya pehle ek image upload karein!")
|
93 |
+
if not prompt.strip():
|
94 |
+
raise gr.Error("Kripya prompt enter karein ki image ke saath kya karna hai.")
|
95 |
+
|
96 |
+
# Input image ko temporary file mein save karein.
|
97 |
with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as tmp:
|
98 |
composite_path = tmp.name
|
99 |
+
# Ensure image is in RGB format before saving if it has an alpha channel
|
100 |
+
if composite_pil.mode == 'RGBA':
|
101 |
+
composite_pil = composite_pil.convert('RGB')
|
102 |
+
composite_pil.save(composite_path, "PNG")
|
103 |
|
104 |
file_name = composite_path
|
105 |
input_text = prompt
|
106 |
+
model = "gemini-1.5-flash-latest" # Sabse naya aur behtar model istemal karein
|
107 |
+
|
108 |
+
edited_image_path = generate(text=input_text, file_name=file_name, model=model)
|
109 |
+
print("Generated image path: ", edited_image_path)
|
110 |
+
|
111 |
+
if edited_image_path:
|
112 |
+
result_img = Image.open(edited_image_path)
|
113 |
+
return [result_img]
|
114 |
+
else:
|
115 |
+
# Agar koi path na mile to error dikhayein
|
116 |
+
raise gr.Error("Image banane mein asamarth. Koshish karein dobara.")
|
117 |
+
|
118 |
+
|
119 |
+
# Behtar UI/UX ke liye Gradio Blocks interface
|
120 |
+
with gr.Blocks(theme=gr.themes.Soft(primary_hue="sky")) as demo:
|
121 |
+
gr.Markdown(
|
122 |
"""
|
123 |
+
<div style='text-align: center; margin-bottom: 20px;'>
|
124 |
+
<h1 style='font-size: 2.5em; color: #333;'>🎨 AI Image Editor 🎨</h1>
|
125 |
+
<p style='font-size: 1.2em; color: #666;'>Upload an image and tell me how you want to change it.</p>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
126 |
</div>
|
127 |
"""
|
128 |
)
|
129 |
|
130 |
+
# Udaharan (Examples) jo interface mein dikhaye jaayenge
|
131 |
examples = [
|
132 |
+
["assets/1.webp", 'change the text on the t-shirt to "AMEER"'],
|
133 |
+
["assets/2.webp", "remove the spoon from the hand"],
|
134 |
+
["assets/3.webp", 'change the text to "MAKE IT AWESOME"'],
|
135 |
+
["assets/1.jpg", "apply a joker style makeup on the face"],
|
136 |
+
["assets/1777043.jpg", "add cyberpunk neon lights in the background"],
|
137 |
+
["assets/2807615.jpg", "add subtle red lipstick on the lips"],
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
138 |
]
|
139 |
|
140 |
+
with gr.Row(gap="20"):
|
141 |
+
with gr.Column(scale=1):
|
142 |
+
with gr.Group():
|
143 |
+
gr.Markdown("### 1. Upload Your Image")
|
144 |
+
image_input = gr.Image(
|
145 |
+
type="pil",
|
146 |
+
label="Image Input",
|
147 |
+
height=400,
|
148 |
+
image_mode="RGBA",
|
149 |
+
show_label=False
|
150 |
+
)
|
151 |
+
with gr.Group():
|
152 |
+
gr.Markdown("### 2. Describe Your Edit")
|
153 |
+
prompt_input = gr.Textbox(
|
154 |
+
lines=3,
|
155 |
+
placeholder="e.g., 'remove the person in the background' or 'make the sky look like a sunset'",
|
156 |
+
label="Prompt",
|
157 |
+
show_label=False,
|
158 |
+
)
|
159 |
+
submit_btn = gr.Button("✨ Apply Edits", variant="primary", size="lg")
|
160 |
+
|
161 |
+
with gr.Column(scale=1):
|
162 |
+
with gr.Group():
|
163 |
+
gr.Markdown("### 3. See the Result")
|
164 |
+
output_gallery = gr.Gallery(
|
165 |
+
label="Generated Output",
|
166 |
+
height=500,
|
167 |
+
show_label=False,
|
168 |
+
preview=True
|
169 |
+
)
|
170 |
+
|
171 |
+
gr.Examples(
|
172 |
+
examples=examples,
|
173 |
+
inputs=[image_input, prompt_input],
|
174 |
+
outputs=output_gallery,
|
175 |
+
fn=process_image_and_prompt,
|
176 |
+
cache_examples=True,
|
177 |
+
label="Or try one of these examples 👇"
|
178 |
+
)
|
179 |
+
|
180 |
submit_btn.click(
|
181 |
fn=process_image_and_prompt,
|
182 |
+
inputs=[image_input, prompt_input],
|
183 |
outputs=output_gallery,
|
|
|
|
|
|
|
|
|
|
|
|
|
184 |
)
|
185 |
|
186 |
+
demo.launch(share=True, debug=True)
|