Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,5 +1,3 @@
|
|
1 |
-
# --- START OF FILE app.py ---
|
2 |
-
|
3 |
import json
|
4 |
import os
|
5 |
import time
|
@@ -13,190 +11,134 @@ import mimetypes
|
|
13 |
from google import genai
|
14 |
from google.genai import types
|
15 |
|
16 |
-
#
|
17 |
-
|
18 |
-
# DO NOT hardcode your API key here.
|
19 |
-
# Set it in your terminal before running:
|
20 |
-
# export GEMINI_API_KEY="YOUR_API_KEY" (for Linux/macOS)
|
21 |
-
# set GEMINI_API_KEY=YOUR_API_KEY (for Windows)
|
22 |
-
|
23 |
-
GEMINI_API_KEY = os.getenv("AIzaSyCrhWiAEQmCidtE2QZw3CTiLt7F8yv5M7A")
|
24 |
-
|
25 |
-
# Check if the API key is available
|
26 |
-
if not GEMINI_API_KEY:
|
27 |
-
raise ValueError("AIzaSyCrhWiAEQmCidtE2QZw3CTiLt7F8yv5M7A")
|
28 |
-
|
29 |
-
# Configure the genai library with the API key
|
30 |
-
genai.configure(api_key=AIzaSyCrhWiAEQmCidtE2QZw3CTiLt7F8yv5M7A)
|
31 |
-
|
32 |
|
33 |
def save_binary_file(file_name, data):
|
34 |
-
"""Saves binary data to a file."""
|
35 |
with open(file_name, "wb") as f:
|
36 |
f.write(data)
|
37 |
|
38 |
-
def generate(text, file_name, model="gemini-
|
39 |
-
|
40 |
-
|
41 |
-
"""
|
42 |
-
# Using the globally configured API key.
|
43 |
-
client = genai.GenerativeModel(model_name=model)
|
44 |
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
# Wait for the file to be processed
|
50 |
-
while uploaded_file.state.name == "PROCESSING":
|
51 |
-
print('.', end='')
|
52 |
-
time.sleep(2)
|
53 |
-
uploaded_file = genai.get_file(uploaded_file.name)
|
54 |
-
|
55 |
-
if uploaded_file.state.name == "FAILED":
|
56 |
-
raise ValueError(f"File upload failed: {uploaded_file.state}")
|
57 |
-
|
58 |
-
print(f"\nFile uploaded successfully: {uploaded_file.uri}")
|
59 |
|
60 |
-
# Create the prompt content
|
61 |
contents = [
|
62 |
-
|
63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
64 |
]
|
65 |
-
|
66 |
-
generation_config = types.GenerationConfig(
|
67 |
temperature=1,
|
68 |
top_p=0.95,
|
69 |
top_k=40,
|
70 |
max_output_tokens=8192,
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
generation_config=generation_config
|
77 |
)
|
78 |
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
print(
|
97 |
-
|
98 |
-
|
99 |
-
if output_image_path is None:
|
100 |
-
# In case the model did not return an image, we can return the original to avoid an error
|
101 |
-
print("Warning: Model did not return an image. Returning the original.")
|
102 |
-
return file_name # Return original image path
|
103 |
-
|
104 |
-
return output_image_path
|
105 |
|
|
|
|
|
|
|
106 |
|
107 |
def process_image_and_prompt(composite_pil, prompt):
|
108 |
-
"""
|
109 |
-
Saves the input PIL image to a temporary file and calls the generation function.
|
110 |
-
"""
|
111 |
-
if composite_pil is None:
|
112 |
-
raise gr.Error("Please upload an image.")
|
113 |
-
if not prompt:
|
114 |
-
raise gr.Error("Please enter a prompt.")
|
115 |
-
|
116 |
# Save the composite image to a temporary file.
|
117 |
with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as tmp:
|
118 |
composite_path = tmp.name
|
119 |
-
|
120 |
-
if composite_pil.mode == "RGBA":
|
121 |
-
composite_pil = composite_pil.convert("RGB")
|
122 |
-
composite_pil.save(composite_path, "PNG")
|
123 |
|
124 |
file_name = composite_path
|
125 |
input_text = prompt
|
126 |
-
|
127 |
-
model = "gemini-1.5-flash-latest"
|
128 |
-
|
129 |
-
edited_image_path = generate(text=input_text, file_name=file_name, model=model)
|
130 |
-
|
131 |
-
if not edited_image_path or not os.path.exists(edited_image_path):
|
132 |
-
raise gr.Error("Failed to generate the image. The model might not have returned an image for this prompt.")
|
133 |
-
|
134 |
-
print("Generated image path:", edited_image_path)
|
135 |
-
result_img = Image.open(edited_image_path)
|
136 |
-
|
137 |
-
# Clean up temporary files
|
138 |
-
os.remove(composite_path)
|
139 |
-
if edited_image_path != composite_path: # Don't delete twice
|
140 |
-
os.remove(edited_image_path)
|
141 |
|
|
|
|
|
|
|
|
|
|
|
142 |
return [result_img]
|
143 |
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
# A clean title and description for the application.
|
148 |
-
gr.Markdown(
|
149 |
-
"""
|
150 |
-
<div style='text-align: center;'>
|
151 |
-
<h1>Gemini AI Image Editor</h1>
|
152 |
-
<p>Upload an image and provide a prompt to edit it. Let's see what you can create!</p>
|
153 |
-
</div>
|
154 |
-
"""
|
155 |
-
)
|
156 |
-
|
157 |
-
with gr.Row():
|
158 |
-
with gr.Column(scale=1): # Input column
|
159 |
-
with gr.Box():
|
160 |
-
image_input = gr.Image(
|
161 |
-
type="pil",
|
162 |
-
label="Upload Image",
|
163 |
-
height=350
|
164 |
-
)
|
165 |
-
prompt_input = gr.Textbox(
|
166 |
-
lines=3,
|
167 |
-
placeholder="e.g., 'make the sky purple', 'add a superhero cape', 'remove the car'...",
|
168 |
-
label="Editing Instruction (Prompt)"
|
169 |
-
)
|
170 |
-
submit_btn = gr.Button("Generate Image", variant="primary")
|
171 |
-
|
172 |
-
with gr.Column(scale=2): # Output column (wider for better viewing)
|
173 |
-
output_gallery = gr.Gallery(label="Generated Image", height=500, object_fit="contain")
|
174 |
|
175 |
# Define examples to be shown within the Gradio interface
|
176 |
examples = [
|
|
|
|
|
177 |
["data/1.webp", 'change text to "AMEER"'],
|
178 |
-
["data/2.webp", "remove the spoon from
|
179 |
-
["data/3.webp", 'change
|
180 |
-
["data/
|
181 |
-
|
182 |
-
|
|
|
|
|
|
|
183 |
]
|
184 |
|
185 |
-
gr.
|
186 |
-
examples=examples,
|
187 |
-
inputs=[image_input, prompt_input],
|
188 |
-
outputs=output_gallery,
|
189 |
-
fn=process_image_and_prompt,
|
190 |
-
cache_examples=False, # Set to True if example images are large and you want to speed up demos
|
191 |
-
label="Try these examples"
|
192 |
-
)
|
193 |
|
194 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
195 |
submit_btn.click(
|
196 |
fn=process_image_and_prompt,
|
197 |
-
inputs=[image_input, prompt_input],
|
198 |
outputs=output_gallery,
|
|
|
|
|
|
|
|
|
|
|
|
|
199 |
)
|
200 |
|
201 |
-
|
202 |
-
demo.launch(share=True, debug=True)
|
|
|
|
|
|
|
1 |
import json
|
2 |
import os
|
3 |
import time
|
|
|
11 |
from google import genai
|
12 |
from google.genai import types
|
13 |
|
14 |
+
# Aapki Gemini API Key yahan daal di gayi hai.
|
15 |
+
GEMINI_API_KEY = "AIzaSyCrhWiAEQmCidtE2QZw3CTiLt7F8yv5M7A"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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-2.0-flash-exp"):
|
22 |
+
# Client ko hardcoded API key se initialize karein.
|
23 |
+
client = genai.Client(api_key=GEMINI_API_KEY)
|
|
|
|
|
|
|
24 |
|
25 |
+
files = [
|
26 |
+
client.files.upload(file=file_name),
|
27 |
+
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
|
|
|
29 |
contents = [
|
30 |
+
types.Content(
|
31 |
+
role="user",
|
32 |
+
parts=[
|
33 |
+
types.Part.from_uri(
|
34 |
+
file_uri=files[0].uri,
|
35 |
+
mime_type=files[0].mime_type,
|
36 |
+
),
|
37 |
+
types.Part.from_text(text=text),
|
38 |
+
],
|
39 |
+
),
|
40 |
]
|
41 |
+
generate_content_config = types.GenerateContentConfig(
|
|
|
42 |
temperature=1,
|
43 |
top_p=0.95,
|
44 |
top_k=40,
|
45 |
max_output_tokens=8192,
|
46 |
+
response_modalities=[
|
47 |
+
"image",
|
48 |
+
"text",
|
49 |
+
],
|
50 |
+
response_mime_type="text/plain",
|
|
|
51 |
)
|
52 |
|
53 |
+
with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as tmp:
|
54 |
+
temp_path = tmp.name
|
55 |
+
for chunk in client.models.generate_content_stream(
|
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 |
# Save the composite image to a temporary file.
|
78 |
with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as tmp:
|
79 |
composite_path = tmp.name
|
80 |
+
composite_pil.save(composite_path)
|
|
|
|
|
|
|
81 |
|
82 |
file_name = composite_path
|
83 |
input_text = prompt
|
84 |
+
model = "gemini-2.0-flash-exp"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
85 |
|
86 |
+
gemma_edited_image_path = generate(text=input_text, file_name=file_name, model=model)
|
87 |
+
print("image_path ", gemma_edited_image_path)
|
88 |
+
result_img = Image.open(gemma_edited_image_path)
|
89 |
+
if result_img.mode == "RGBA":
|
90 |
+
result_img = result_img.convert("RGB")
|
91 |
return [result_img]
|
92 |
|
93 |
+
# Build a Blocks-based interface.
|
94 |
+
with gr.Blocks() as demo:
|
95 |
+
# Yahan se HTML Header हटा दिया गया है।
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
96 |
|
97 |
# Define examples to be shown within the Gradio interface
|
98 |
examples = [
|
99 |
+
# Each example is a list corresponding to the inputs:
|
100 |
+
# [Input Image, Prompt]
|
101 |
["data/1.webp", 'change text to "AMEER"'],
|
102 |
+
["data/2.webp", "remove the spoon from hand only"],
|
103 |
+
["data/3.webp", 'change text to "Make it "'],
|
104 |
+
["data/1.jpg", "add joker style only on face"],
|
105 |
+
["data/1777043.jpg", "add joker style only on face"],
|
106 |
+
["data/2807615.jpg","add lipstick on lip only "],
|
107 |
+
|
108 |
+
["data/76860.jpg", "add lipstick on lip only "],
|
109 |
+
["data/2807615.jpg", "make it happy looking face only"],
|
110 |
]
|
111 |
|
112 |
+
gr.Markdown("## Gen AI Image Editing\nUpload an image and enter a prompt to generate outputs in the gallery. Do not Use NFSW Images")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
113 |
|
114 |
+
with gr.Row():
|
115 |
+
with gr.Column():
|
116 |
+
image_input = gr.Image(
|
117 |
+
type="pil",
|
118 |
+
label="Upload Image",
|
119 |
+
image_mode="RGBA"
|
120 |
+
)
|
121 |
+
# API Key Textbox ko yahan se hata diya gaya hai
|
122 |
+
prompt_input = gr.Textbox(
|
123 |
+
lines=2,
|
124 |
+
placeholder="Enter prompt here...",
|
125 |
+
label="Prompt"
|
126 |
+
)
|
127 |
+
submit_btn = gr.Button("Generate")
|
128 |
+
with gr.Column():
|
129 |
+
output_gallery = gr.Gallery(label="Generated Outputs")
|
130 |
+
|
131 |
+
# Set up the interaction.
|
132 |
submit_btn.click(
|
133 |
fn=process_image_and_prompt,
|
134 |
+
inputs=[image_input, prompt_input], # Inputs se API key hata di gayi hai
|
135 |
outputs=output_gallery,
|
136 |
+
|
137 |
+
)
|
138 |
+
gr.Examples(
|
139 |
+
examples=examples,
|
140 |
+
inputs=[image_input, prompt_input], # Inputs se API key hata di gayi hai
|
141 |
+
label="Try these examples"
|
142 |
)
|
143 |
|
144 |
+
demo.launch(share=True)
|
|