Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -14,25 +14,34 @@ from google.genai import types
|
|
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-
|
22 |
-
# Client ko hardcoded API key se initialize karein.
|
23 |
-
client = genai.Client(api_key=GEMINI_API_KEY)
|
24 |
|
25 |
-
|
26 |
-
|
27 |
-
|
|
|
|
|
|
|
|
|
28 |
|
29 |
contents = [
|
30 |
types.Content(
|
31 |
role="user",
|
32 |
parts=[
|
33 |
types.Part.from_uri(
|
34 |
-
file_uri=
|
35 |
-
mime_type=
|
36 |
),
|
37 |
types.Part.from_text(text=text),
|
38 |
],
|
@@ -52,65 +61,91 @@ def generate(text, file_name, model="gemini-2.0-flash-exp"):
|
|
52 |
|
53 |
with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as tmp:
|
54 |
temp_path = tmp.name
|
55 |
-
|
56 |
-
|
|
|
57 |
contents=contents,
|
58 |
-
|
59 |
-
)
|
60 |
-
|
61 |
-
|
62 |
-
|
|
|
|
|
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 |
-
|
71 |
-
|
72 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
-
|
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 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
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(
|
@@ -118,7 +153,6 @@ with gr.Blocks() as demo:
|
|
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...",
|
@@ -131,13 +165,13 @@ with gr.Blocks() as demo:
|
|
131 |
# Set up the interaction.
|
132 |
submit_btn.click(
|
133 |
fn=process_image_and_prompt,
|
134 |
-
inputs=[image_input, prompt_input],
|
135 |
outputs=output_gallery,
|
136 |
-
|
137 |
)
|
138 |
gr.Examples(
|
139 |
examples=examples,
|
140 |
-
inputs=[image_input, prompt_input],
|
141 |
label="Try these examples"
|
142 |
)
|
143 |
|
|
|
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-1.5-flash-latest"): # <<< CHANGE: Model ko stable flash version se update kiya gaya
|
|
|
|
|
29 |
|
30 |
+
# Client ko global scope se istemal karein
|
31 |
+
print(f"Uploading file: {file_name}")
|
32 |
+
uploaded_file = file_client.upload_file(path=file_name)
|
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=uploaded_file.uri,
|
44 |
+
mime_type=uploaded_file.mime_type,
|
45 |
),
|
46 |
types.Part.from_text(text=text),
|
47 |
],
|
|
|
61 |
|
62 |
with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as tmp:
|
63 |
temp_path = tmp.name
|
64 |
+
print("Generating content...")
|
65 |
+
# Stream ki jagah seedha generate karein, image generation ke liye yeh aam taur par behtar hai
|
66 |
+
response = model_instance.generate_content(
|
67 |
contents=contents,
|
68 |
+
generation_config=generate_content_config,
|
69 |
+
)
|
70 |
+
print("Content generated.")
|
71 |
+
|
72 |
+
# Response se image data extract karein
|
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 |
+
f"File of mime type {inline_data.mime_type} saved to: {temp_path}"
|
|
|
79 |
)
|
80 |
else:
|
81 |
+
# Agar image nahi hai, to text response print karein
|
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 |
+
## <<< CHANGE START: Image ko API par bhejne se pehle resize karein
|
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 |
+
# Agar image mein transparency (alpha channel) hai to use RGB mein convert karein
|
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 |
+
gr.Markdown("## Gen AI Image Editing\nUpload an image and enter a prompt to generate outputs in the gallery. Do not Use NFSW Images")
|
136 |
+
|
137 |
# Define examples to be shown within the Gradio interface
|
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():
|
151 |
image_input = gr.Image(
|
|
|
153 |
label="Upload Image",
|
154 |
image_mode="RGBA"
|
155 |
)
|
|
|
156 |
prompt_input = gr.Textbox(
|
157 |
lines=2,
|
158 |
placeholder="Enter prompt here...",
|
|
|
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 |
+
show_progress="full" # <<< CHANGE: UI mein loading progress dikhayein
|
171 |
)
|
172 |
gr.Examples(
|
173 |
examples=examples,
|
174 |
+
inputs=[image_input, prompt_input],
|
175 |
label="Try these examples"
|
176 |
)
|
177 |
|