Wiuhh commited on
Commit
41a4a34
·
verified ·
1 Parent(s): e27da0c

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +163 -0
app.py ADDED
@@ -0,0 +1,163 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ # 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 to include the custom HTML header.
94
+ with gr.Blocks() as demo:
95
+ # HTML Header for the application.
96
+ gr.HTML(
97
+ """
98
+ <div style='display: flex; align-items: center; justify-content: center; gap: 20px'>
99
+ <div style="background-color: var(--block-background-fill); border-radius: 8px">
100
+ <img src="https://www.gstatic.com/lamda/images/gemini_favicon_f069958c85030456e93de685481c559f160ea06b.png" style="width: 100px; height: 100px;">
101
+ </div>
102
+ <div>
103
+ <h1>Gen AI Image Editing</h1>
104
+ <p>Gemini using for Image Editing</p>
105
+ <p>Powered by <a href="https://gradio.app/">Gradio</a> ⚡️</p>
106
+ <p>Get an API Key <a href="https://aistudio.google.com/apikey">here</a></p>
107
+ <p>Follow me on Instagram: <a href="https://www.instagram.com/nihal_gazi_io/">nihal_gazi_io</a></p>
108
+ <p>Or contact me through Discord: <b><i>@nihal_gazi_io</i></b></p>
109
+ </div>
110
+ </div>
111
+ """
112
+ )
113
+
114
+ # Define examples to be shown within the Gradio interface
115
+ examples = [
116
+ # Each example is a list corresponding to the inputs:
117
+ # [Input Image, Prompt]
118
+ ["data/1.webp", 'change text to "AMEER"'],
119
+ ["data/2.webp", "remove the spoon from hand only"],
120
+ ["data/3.webp", 'change text to "Make it "'],
121
+ ["data/1.jpg", "add joker style only on face"],
122
+ ["data/1777043.jpg", "add joker style only on face"],
123
+ ["data/2807615.jpg","add lipstick on lip only "],
124
+
125
+ ["data/76860.jpg", "add lipstick on lip only "],
126
+ ["data/2807615.jpg", "make it happy looking face only"],
127
+
128
+
129
+ ]
130
+
131
+ gr.Markdown("Upload an image and enter a prompt to generate outputs in the gallery. Do not Use NFSW Images")
132
+
133
+ with gr.Row():
134
+ with gr.Column():
135
+ image_input = gr.Image(
136
+ type="pil",
137
+ label="Upload Image",
138
+ image_mode="RGBA"
139
+ )
140
+ # API Key Textbox ko yahan se hata diya gaya hai
141
+ prompt_input = gr.Textbox(
142
+ lines=2,
143
+ placeholder="Enter prompt here...",
144
+ label="Prompt"
145
+ )
146
+ submit_btn = gr.Button("Generate")
147
+ with gr.Column():
148
+ output_gallery = gr.Gallery(label="Generated Outputs")
149
+
150
+ # Set up the interaction.
151
+ submit_btn.click(
152
+ fn=process_image_and_prompt,
153
+ inputs=[image_input, prompt_input], # Inputs se API key hata di gayi hai
154
+ outputs=output_gallery,
155
+
156
+ )
157
+ gr.Examples(
158
+ examples=examples,
159
+ inputs=[image_input, prompt_input], # Inputs se API key hata di gayi hai
160
+ label="Try these examples"
161
+ )
162
+
163
+ demo.launch(share=True)