Wiuhh commited on
Commit
f39cc1d
·
verified ·
1 Parent(s): 27a7b61

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +92 -150
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
- # --- IMPORTANT SECURITY UPDATE ---
17
- # API Key is now loaded from an environment variable for security.
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-1.5-flash-latest"):
39
- """
40
- Generates content using the Gemini model by uploading a file.
41
- """
42
- # Using the globally configured API key.
43
- client = genai.GenerativeModel(model_name=model)
44
 
45
- print(f"Uploading file: {file_name}")
46
- # The new File API for gemini-1.5-flash expects a different upload method
47
- uploaded_file = genai.upload_file(path=file_name)
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
- uploaded_file,
63
- text
 
 
 
 
 
 
 
 
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
- # Generate content
74
- response = client.generate_content(
75
- contents,
76
- generation_config=generation_config
77
  )
78
 
79
- # Process the response to extract the generated image
80
- output_image_path = None
81
- if response.candidates and response.candidates[0].content and response.candidates[0].content.parts:
82
- for part in response.candidates[0].content.parts:
83
- # The edited image is returned as a blob
84
- if part.blob:
85
- if 'image' in part.blob.mime_type:
86
- # Save the image to a temporary file
87
- with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as tmp:
88
- tmp.write(part.blob.data)
89
- output_image_path = tmp.name
90
- print(
91
- f"Image of mime type {part.blob.mime_type} saved to: {output_image_path}"
92
- )
93
- break # Exit after finding the first image
94
- # Sometimes the response might be text (e.g., if it can't fulfill the request)
95
- elif part.text:
96
- print(f"Model returned text: {part.text}")
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
- # Convert to RGB to ensure compatibility
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
- # Updated model name to a stable public version
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
- # Build a Blocks-based interface with an improved and cleaner UI/UX.
146
- with gr.Blocks(theme=gr.themes.Soft()) as demo:
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 her hand"],
179
- ["data/3.webp", 'change the text to "Make It"'],
180
- ["data/1777043.jpg", "add joker style makeup to his face"],
181
- ["data/2807615.jpg", "add red lipstick on her lips only"],
182
- ["data/2807615.jpg", "make her look happier"],
 
 
 
183
  ]
184
 
185
- gr.Examples(
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
- # Set up the button's click event.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
195
  submit_btn.click(
196
  fn=process_image_and_prompt,
197
- inputs=[image_input, prompt_input],
198
  outputs=output_gallery,
 
 
 
 
 
 
199
  )
200
 
201
- # Launch the Gradio app
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)