Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -14,34 +14,25 @@ from google.genai import types
|
|
14 |
# Aapki Gemini API Key yahan daal di gayi hai.
|
15 |
GEMINI_API_KEY = "AIzaSyCrhWiAEQmCidtE2QZw3CTiLt7F8yv5M7A"
|
16 |
|
17 |
-
## <<< CHANGE START: API Client ko globally initialize karein taaki baar-baar na bane.
|
18 |
-
# API key configure karein
|
19 |
-
genai.configure(api_key=GEMINI_API_KEY)
|
20 |
-
# File service ke liye client banayein
|
21 |
-
file_client = genai.FilesClient(api_key=GEMINI_API_KEY)
|
22 |
-
## <<< CHANGE END
|
23 |
-
|
24 |
def save_binary_file(file_name, data):
|
25 |
with open(file_name, "wb") as f:
|
26 |
f.write(data)
|
27 |
|
28 |
-
def generate(text, file_name, model="gemini-
|
|
|
|
|
29 |
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
print(f"Completed uploading file: {uploaded_file.name}")
|
34 |
-
|
35 |
-
# Model ko initialize karein
|
36 |
-
model_instance = genai.GenerativeModel(model_name=model)
|
37 |
|
38 |
contents = [
|
39 |
types.Content(
|
40 |
role="user",
|
41 |
parts=[
|
42 |
types.Part.from_uri(
|
43 |
-
file_uri=
|
44 |
-
mime_type=
|
45 |
),
|
46 |
types.Part.from_text(text=text),
|
47 |
],
|
@@ -61,90 +52,49 @@ def generate(text, file_name, model="gemini-1.5-flash-latest"): # <<< CHANGE: Mo
|
|
61 |
|
62 |
with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as tmp:
|
63 |
temp_path = tmp.name
|
64 |
-
|
65 |
-
|
66 |
-
response = model_instance.generate_content(
|
67 |
contents=contents,
|
68 |
-
|
69 |
-
)
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
if response.candidates and response.candidates[0].content and response.candidates[0].content.parts:
|
74 |
-
inline_data = response.candidates[0].content.parts[0].inline_data
|
75 |
if inline_data:
|
76 |
save_binary_file(temp_path, inline_data.data)
|
77 |
print(
|
78 |
-
|
|
|
79 |
)
|
80 |
else:
|
81 |
-
|
82 |
-
if response.text:
|
83 |
-
print(f"Model Response (Text): {response.text}")
|
84 |
-
else:
|
85 |
-
print("No image data or text found in the response.")
|
86 |
-
else:
|
87 |
-
print("Could not get a valid response from the model.")
|
88 |
|
89 |
-
|
90 |
-
# Upload ki gayi file ko delete karein taaki storage na bhare
|
91 |
-
print(f"Deleting uploaded file: {uploaded_file.name}")
|
92 |
-
file_client.delete_file(name=uploaded_file.name)
|
93 |
-
|
94 |
return temp_path
|
95 |
|
96 |
|
97 |
def process_image_and_prompt(composite_pil, prompt):
|
98 |
-
|
99 |
-
MAX_SIZE = (1024, 1024) # Maximum dimensions
|
100 |
-
composite_pil.thumbnail(MAX_SIZE, Image.Resampling.LANCZOS)
|
101 |
-
print(f"Image resized to fit within {MAX_SIZE} dimensions.")
|
102 |
-
## <<< CHANGE END
|
103 |
-
|
104 |
-
# Resized image ko temporary file mein save karein
|
105 |
with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as tmp:
|
106 |
composite_path = tmp.name
|
107 |
-
|
108 |
-
if composite_pil.mode == "RGBA":
|
109 |
-
composite_pil = composite_pil.convert("RGB")
|
110 |
-
composite_pil.save(composite_path, "PNG")
|
111 |
|
112 |
file_name = composite_path
|
113 |
input_text = prompt
|
114 |
-
|
115 |
-
gemma_edited_image_path = generate(text=input_text, file_name=file_name)
|
116 |
-
|
117 |
-
if os.path.exists(gemma_edited_image_path) and os.path.getsize(gemma_edited_image_path) > 0:
|
118 |
-
print("image_path ", gemma_edited_image_path)
|
119 |
-
result_img = Image.open(gemma_edited_image_path)
|
120 |
-
# Safai ke liye temporary files delete karein
|
121 |
-
os.remove(composite_path)
|
122 |
-
os.remove(gemma_edited_image_path)
|
123 |
-
return [result_img]
|
124 |
-
else:
|
125 |
-
# Agar koi image generate nahi hui to error handle karein
|
126 |
-
print("Failed to generate image or the generated file is empty.")
|
127 |
-
os.remove(composite_path) # Input temp file ko phir bhi delete karein
|
128 |
-
# Aap yahan user ko ek error message dikha sakte hain
|
129 |
-
# For now, we return nothing, which will clear the gallery
|
130 |
-
return []
|
131 |
|
|
|
|
|
|
|
|
|
|
|
|
|
132 |
|
133 |
# Build a Blocks-based interface.
|
134 |
with gr.Blocks() as demo:
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
examples = [
|
139 |
-
["data/1.webp", 'change text to "AMEER"'],
|
140 |
-
["data/2.webp", "remove the spoon from hand only"],
|
141 |
-
["data/3.webp", 'change text to "Make it "'],
|
142 |
-
["data/1.jpg", "add joker style only on face"],
|
143 |
-
["data/1777043.jpg", "add joker style only on face"],
|
144 |
-
["data/2807615.jpg","add lipstick on lip only "],
|
145 |
-
["data/76860.jpg", "add lipstick on lip only "],
|
146 |
-
["data/2807615.jpg", "make it happy looking face only"],
|
147 |
-
]
|
148 |
|
149 |
with gr.Row():
|
150 |
with gr.Column():
|
@@ -153,6 +103,7 @@ with gr.Blocks() as demo:
|
|
153 |
label="Upload Image",
|
154 |
image_mode="RGBA"
|
155 |
)
|
|
|
156 |
prompt_input = gr.Textbox(
|
157 |
lines=2,
|
158 |
placeholder="Enter prompt here...",
|
@@ -162,16 +113,33 @@ with gr.Blocks() as demo:
|
|
162 |
with gr.Column():
|
163 |
output_gallery = gr.Gallery(label="Generated Outputs")
|
164 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
165 |
# Set up the interaction.
|
166 |
submit_btn.click(
|
167 |
fn=process_image_and_prompt,
|
168 |
-
inputs=[image_input, prompt_input],
|
169 |
outputs=output_gallery,
|
170 |
-
|
171 |
)
|
172 |
gr.Examples(
|
173 |
examples=examples,
|
174 |
-
inputs=[image_input, prompt_input],
|
175 |
label="Try these examples"
|
176 |
)
|
177 |
|
|
|
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 |
],
|
|
|
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 |
+
# HTML Header ko yahan se hata diya gaya hai.
|
96 |
+
gr.Markdown("## Gen AI Image Editing")
|
97 |
+
gr.Markdown("Upload an image and enter a prompt to generate outputs in the gallery. Do not Use NFSW Images")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
98 |
|
99 |
with gr.Row():
|
100 |
with gr.Column():
|
|
|
103 |
label="Upload Image",
|
104 |
image_mode="RGBA"
|
105 |
)
|
106 |
+
# API Key Textbox ko yahan se hata diya gaya hai
|
107 |
prompt_input = gr.Textbox(
|
108 |
lines=2,
|
109 |
placeholder="Enter prompt here...",
|
|
|
113 |
with gr.Column():
|
114 |
output_gallery = gr.Gallery(label="Generated Outputs")
|
115 |
|
116 |
+
# Define examples to be shown within the Gradio interface
|
117 |
+
examples = [
|
118 |
+
# Each example is a list corresponding to the inputs:
|
119 |
+
# [Input Image, Prompt]
|
120 |
+
["data/1.webp", 'change text to "AMEER"'],
|
121 |
+
["data/2.webp", "remove the spoon from hand only"],
|
122 |
+
["data/3.webp", 'change text to "Make it "'],
|
123 |
+
["data/1.jpg", "add joker style only on face"],
|
124 |
+
["data/1777043.jpg", "add joker style only on face"],
|
125 |
+
["data/2807615.jpg","add lipstick on lip only "],
|
126 |
+
|
127 |
+
["data/76860.jpg", "add lipstick on lip only "],
|
128 |
+
["data/2807615.jpg", "make it happy looking face only"],
|
129 |
+
|
130 |
+
|
131 |
+
]
|
132 |
+
|
133 |
# Set up the interaction.
|
134 |
submit_btn.click(
|
135 |
fn=process_image_and_prompt,
|
136 |
+
inputs=[image_input, prompt_input], # Inputs se API key hata di gayi hai
|
137 |
outputs=output_gallery,
|
138 |
+
|
139 |
)
|
140 |
gr.Examples(
|
141 |
examples=examples,
|
142 |
+
inputs=[image_input, prompt_input], # Inputs se API key hata di gayi hai
|
143 |
label="Try these examples"
|
144 |
)
|
145 |
|