ameerazam08 commited on
Commit
43bc49d
·
verified ·
1 Parent(s): 6cac758

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +166 -0
app.py ADDED
@@ -0,0 +1,166 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+ import os
3
+ import time
4
+ import uuid
5
+ import tempfile
6
+ from PIL import Image
7
+ import gradio as gr
8
+ import base64
9
+ import mimetypes
10
+
11
+ from google import genai
12
+ from google.genai import types
13
+
14
+ def save_binary_file(file_name, data):
15
+ with open(file_name, "wb") as f:
16
+ f.write(data)
17
+
18
+ def generate(text, file_name, api_key, model="gemini-2.0-flash-exp"):
19
+ # Initialize client using provided api_key (or fallback to env variable)
20
+ client = genai.Client(api_key=(api_key.strip() if api_key and api_key.strip() != ""
21
+ else os.environ.get("GEMINI_API_KEY")))
22
+
23
+ files = [
24
+ client.files.upload(file=file_name),
25
+ ]
26
+
27
+ contents = [
28
+ types.Content(
29
+ role="user",
30
+ parts=[
31
+ types.Part.from_uri(
32
+ file_uri=files[0].uri,
33
+ mime_type=files[0].mime_type,
34
+ ),
35
+ types.Part.from_text(text=text),
36
+ ],
37
+ ),
38
+ ]
39
+ generate_content_config = types.GenerateContentConfig(
40
+ temperature=1,
41
+ top_p=0.95,
42
+ top_k=40,
43
+ max_output_tokens=8192,
44
+ response_modalities=[
45
+ "image",
46
+ "text",
47
+ ],
48
+ response_mime_type="text/plain",
49
+ )
50
+
51
+ with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as tmp:
52
+ temp_path = tmp.name
53
+ for chunk in client.models.generate_content_stream(
54
+ model=model,
55
+ contents=contents,
56
+ config=generate_content_config,
57
+ ):
58
+ if not chunk.candidates or not chunk.candidates[0].content or not chunk.candidates[0].content.parts:
59
+ continue
60
+ inline_data = chunk.candidates[0].content.parts[0].inline_data
61
+ if inline_data:
62
+ save_binary_file(temp_path, inline_data.data)
63
+ print(
64
+ "File of mime type "
65
+ f"{inline_data.mime_type} saved to: {temp_path}"
66
+ )
67
+ else:
68
+ print(chunk.text)
69
+
70
+ del files
71
+ return temp_path
72
+
73
+
74
+ def process_image_and_prompt(composite_pil, prompt, gemini_api_key):
75
+ # Save the composite image to a temporary file.
76
+ with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as tmp:
77
+ composite_path = tmp.name
78
+ composite_pil.save(composite_path)
79
+
80
+ file_name = composite_path
81
+ input_text = prompt
82
+ model = "gemini-2.0-flash-exp"
83
+
84
+ gemma_edited_image_path = generate(text=input_text, file_name=file_name, api_key=gemini_api_key, model=model)
85
+ print("image_path ", gemma_edited_image_path)
86
+ result_img = Image.open(gemma_edited_image_path)
87
+ if result_img.mode == "RGBA":
88
+ result_img = result_img.convert("RGB")
89
+ return [result_img]
90
+
91
+ # Build a Blocks-based interface to include the custom HTML header.
92
+ with gr.Blocks() as demo:
93
+ # HTML Header for the application.
94
+ gr.HTML(
95
+ """
96
+ <div style='display: flex; align-items: center; justify-content: center; gap: 20px'>
97
+ <div style="background-color: var(--block-background-fill); border-radius: 8px">
98
+ <img src="https://www.gstatic.com/lamda/images/gemini_favicon_f069958c85030456e93de685481c559f160ea06b.png" style="width: 100px; height: 100px;">
99
+ </div>
100
+ <div>
101
+ <h1>Gen AI Image Editing</h1>
102
+ <p>Gemini using for Image Editing</p>
103
+ <p>Powered by <a href="https://gradio.app/">Gradio</a> ⚡️</p>
104
+ <p>Get an API Key <a href="https://aistudio.google.com/apikey">here</a></p>
105
+ <p>Follow me on Twitter: <a href="https://x.com/Ameerazam18">Ameerazam18</a></p>
106
+ </div>
107
+ </div>
108
+ """
109
+ )
110
+
111
+ # Title and description.
112
+
113
+ # Define examples to be shown within the Gradio interface
114
+ examples = [
115
+ # Each example is a list corresponding to the inputs:
116
+ # [Input Image, Prompt, Guidance Scale, Number of Steps, LoRA Name]
117
+ ["data/1.webp", 'change text to "AMEER"'],
118
+ ["data/2.webp", "remove the spoon from hand only"],
119
+ ["data/3.webp", 'change text to "Make it "'],
120
+ ["data/1.jpg", "add joker style only on face"],
121
+ ["data/1777043.jpg", "add joker style only on face"],
122
+ ["data/2807615.jpg","add lipstick on lip only "],
123
+
124
+ ["data/76860.jpg", "add lipstick on lip only "],
125
+ ["data/2807615.jpg", "make it happy looking face only"],
126
+
127
+
128
+ ]
129
+
130
+ gr.Markdown("Upload an image and enter a prompt to generate outputs in the gallery. Do not Use NFSW Images")
131
+
132
+ with gr.Row():
133
+ with gr.Column():
134
+ image_input = gr.Image(
135
+ type="pil",
136
+ label="Upload Image",
137
+ image_mode="RGBA"
138
+ )
139
+ gemini_api_key = gr.Textbox(
140
+ lines=1,
141
+ placeholder="Enter Gemini API Key (optional)",
142
+ label="Gemini API Key (optional) Generate and fill here"
143
+ )
144
+ prompt_input = gr.Textbox(
145
+ lines=2,
146
+ placeholder="Enter prompt here...",
147
+ label="Prompt"
148
+ )
149
+ submit_btn = gr.Button("Generate")
150
+ with gr.Column():
151
+ output_gallery = gr.Gallery(label="Generated Outputs")
152
+
153
+ # Set up the interaction.
154
+ submit_btn.click(
155
+ fn=process_image_and_prompt,
156
+ inputs=[image_input, prompt_input, gemini_api_key],
157
+ outputs=output_gallery,
158
+
159
+ )
160
+ gr.Examples(
161
+ examples=examples,
162
+ inputs=[image_input, prompt_input, gemini_api_key],
163
+ label="Try these examples"
164
+ )
165
+
166
+ demo.launch(share=True)