Athspi commited on
Commit
a5f2ac3
·
verified ·
1 Parent(s): 25ebc67

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +69 -152
app.py CHANGED
@@ -1,166 +1,83 @@
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} and prompt input :{text}"
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)
 
 
 
1
+ from flask import Flask, render_template, request, jsonify
2
+ import google.generativeai as genai
3
  import os
 
 
4
  import tempfile
 
 
5
  import base64
6
+ from dotenv import load_dotenv
7
 
8
+ # Load environment variables
9
+ load_dotenv()
10
 
11
+ # Configure Flask app
12
+ app = Flask(__name__)
 
13
 
14
+ # Configure Gemini API
15
+ genai.configure(api_key=os.getenv("GEMINI_API_KEY"))
 
 
 
 
 
 
16
 
17
+ @app.route("/")
18
+ def home():
19
+ return render_template("index.html")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
 
21
+ @app.route("/process", methods=["POST"])
22
+ def process_image():
23
+ try:
24
+ # Get data from request
25
+ data = request.json
26
+ image_data = data.get("image")
27
+ object_type = data.get("objectType")
28
 
29
+ if not image_data or not object_type:
30
+ return jsonify({"success": False, "message": "Invalid input data"})
 
 
 
 
31
 
32
+ # Decode base64 image data
33
+ image_bytes = base64.b64decode(image_data.split(",")[1])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34
 
35
+ # Create temporary directory
36
+ temp_dir = tempfile.mkdtemp()
37
+ input_path = os.path.join(temp_dir, "input.png")
38
+ with open(input_path, "wb") as f:
39
+ f.write(image_bytes)
40
 
41
+ # Create the model
42
+ model = genai.GenerativeModel('gemini-2.0-flash-exp-image-generation')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
43
 
44
+ # Build the prompt
45
+ prompt = f"Remove the {object_type} from the image and fill the area naturally."
46
 
47
+ # Generate content
48
+ response = model.generate_content(
49
+ [
50
+ prompt,
51
+ genai.upload_file(input_path)
52
+ ],
53
+ generation_config={
54
+ "temperature": 1,
55
+ "top_p": 0.95,
56
+ "top_k": 40,
57
+ "max_output_tokens": 8192,
58
+ },
59
+ safety_settings={
60
+ "HARM_CATEGORY_CIVIC_INTEGRITY": "BLOCK_NONE"
61
+ }
62
+ )
63
+
64
+ # Process response
65
+ output_path = os.path.join(temp_dir, "result.png")
66
+ for chunk in response:
67
+ if chunk.candidates:
68
+ for part in chunk.candidates[0].content.parts:
69
+ if hasattr(part, 'inline_data'):
70
+ with open(output_path, "wb") as f:
71
+ f.write(part.inline_data.data)
72
+ return jsonify({
73
+ "success": True,
74
+ "resultPath": output_path
75
+ })
76
+
77
+ return jsonify({"success": False, "message": "No valid image data found in response"})
78
+
79
+ except Exception as e:
80
+ return jsonify({"success": False, "message": str(e)})
81
+
82
+ if __name__ == "__main__":
83
+ app.run(host="0.0.0.0", port=5000)