Deadmon commited on
Commit
ff6bd0c
·
verified ·
1 Parent(s): 844ed4d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +129 -38
app.py CHANGED
@@ -1,4 +1,109 @@
1
- # Alternative layout using a separate gr.Image component
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
  with gr.Blocks(title="Image Editing Chatbot") as demo:
3
  gr.Markdown("# Image Editing Chatbot")
4
  gr.Markdown("Upload an image and/or type a prompt to generate or edit an image using Google's Gemini model")
@@ -18,54 +123,40 @@ with gr.Blocks(title="Image Editing Chatbot") as demo:
18
 
19
  # Input area
20
  with gr.Row():
21
- image_input = gr.Image(
22
- label="Upload Image",
23
- type="pil",
24
- scale=1,
25
- height=100
26
- )
27
- prompt_input = gr.Textbox(
28
- label="",
29
- placeholder="Type something",
30
- show_label=False,
31
- container=False,
32
- scale=3
33
- )
34
- run_btn = gr.Button("Run", scale=1)
 
 
 
 
35
 
36
  # State to maintain chat history
37
  chat_state = gr.State([])
38
 
39
- def chat_handler(user_input, user_image, chat_history):
40
- # Add user message to chat history
41
- if user_input:
42
- chat_history.append({"role": "user", "content": user_input})
43
-
44
- # If no input, return early
45
- if not user_input and not user_image:
46
- chat_history.append({"role": "assistant", "content": "Please provide a prompt or an image."})
47
- return chat_history, None, user_image, None, ""
48
-
49
- # Generate image based on user input
50
- img, status = generate_image(user_input or "Generate an image", user_image)
51
-
52
- # Add AI response to chat history
53
- chat_history.append({"role": "assistant", "content": status})
54
-
55
- return chat_history, None, user_image, img, ""
56
-
57
  # Connect the button to the chat handler
58
- run_btn.click(
59
  fn=chat_handler,
60
- inputs=[prompt_input, image_input, chat_state],
61
- outputs=[chatbot, image_input, uploaded_image_output, generated_image_output, prompt_input]
62
  )
63
 
64
  # Also allow Enter key to submit
65
  prompt_input.submit(
66
  fn=chat_handler,
67
- inputs=[prompt_input, image_input, chat_state],
68
- outputs=[chatbot, image_input, uploaded_image_output, generated_image_output, prompt_input]
69
  )
70
 
71
  if __name__ == "__main__":
 
1
+ import base64
2
+ import os
3
+ import mimetypes
4
+ from google import genai
5
+ from google.genai import types
6
+ import gradio as gr
7
+ import io
8
+ from PIL import Image
9
+
10
+ def save_binary_file(file_name, data):
11
+ f = open(file_name, "wb")
12
+ f.write(data)
13
+ f.close()
14
+
15
+ def generate_image(prompt, image=None, output_filename="generated_image"):
16
+ # Initialize client with the API key
17
+ client = genai.Client(
18
+ api_key="AIzaSyAQcy3LfrkMy6DqS_8MqftAXu1Bx_ov_E8",
19
+ )
20
+
21
+ model = "gemini-2.0-flash-exp-image-generation"
22
+ parts = [types.Part.from_text(text=prompt)]
23
+
24
+ # If an image is provided, add it to the content
25
+ if image:
26
+ # Convert PIL Image to bytes
27
+ img_byte_arr = io.BytesIO()
28
+ image.save(img_byte_arr, format="PNG")
29
+ img_bytes = img_byte_arr.getvalue()
30
+ # Add the image as a Part with inline_data
31
+ parts.append({
32
+ "inline_data": {
33
+ "mime_type": "image/png",
34
+ "data": img_bytes
35
+ }
36
+ })
37
+
38
+ contents = [
39
+ types.Content(
40
+ role="user",
41
+ parts=parts,
42
+ ),
43
+ ]
44
+ generate_content_config = types.GenerateContentConfig(
45
+ temperature=1,
46
+ top_p=0.95,
47
+ top_k=40,
48
+ max_output_tokens=8192,
49
+ response_modalities=[
50
+ "image",
51
+ "text",
52
+ ],
53
+ safety_settings=[
54
+ types.SafetySetting(
55
+ category="HARM_CATEGORY_CIVIC_INTEGRITY",
56
+ threshold="OFF",
57
+ ),
58
+ ],
59
+ response_mime_type="text/plain",
60
+ )
61
+
62
+ # Generate the content
63
+ response = client.models.generate_content_stream(
64
+ model=model,
65
+ contents=contents,
66
+ config=generate_content_config,
67
+ )
68
+
69
+ # Process the response
70
+ for chunk in response:
71
+ if not chunk.candidates or not chunk.candidates[0].content or not chunk.candidates[0].content.parts:
72
+ continue
73
+ if chunk.candidates[0].content.parts[0].inline_data:
74
+ inline_data = chunk.candidates[0].content.parts[0].inline_data
75
+ file_extension = mimetypes.guess_extension(inline_data.mime_type)
76
+ filename = f"{output_filename}{file_extension}"
77
+ save_binary_file(filename, inline_data.data)
78
+
79
+ # Convert binary data to PIL Image for Gradio display
80
+ img = Image.open(io.BytesIO(inline_data.data))
81
+ return img, f"Image saved as {filename}"
82
+ else:
83
+ return None, chunk.text
84
+
85
+ return None, "No image generated"
86
+
87
+ # Function to handle chat interaction
88
+ def chat_handler(prompt, user_image, chat_history, output_filename="generated_image"):
89
+ # Add the prompt to the chat history
90
+ if prompt:
91
+ chat_history.append({"role": "user", "content": prompt})
92
+
93
+ # If no input, return early
94
+ if not prompt and not user_image:
95
+ chat_history.append({"role": "assistant", "content": "Please provide a prompt or an image."})
96
+ return chat_history, user_image, None, ""
97
+
98
+ # Generate image based on user input
99
+ img, status = generate_image(prompt or "Generate an image", user_image, output_filename)
100
+
101
+ # Add the status message to the chat history
102
+ chat_history.append({"role": "assistant", "content": status})
103
+
104
+ return chat_history, user_image, img, ""
105
+
106
+ # Create Gradio interface
107
  with gr.Blocks(title="Image Editing Chatbot") as demo:
108
  gr.Markdown("# Image Editing Chatbot")
109
  gr.Markdown("Upload an image and/or type a prompt to generate or edit an image using Google's Gemini model")
 
123
 
124
  # Input area
125
  with gr.Row():
126
+ with gr.Column():
127
+ image_input = gr.Image(
128
+ label="Upload Image",
129
+ type="pil",
130
+ scale=1,
131
+ height=100
132
+ )
133
+ prompt_input = gr.Textbox(
134
+ label="Prompt",
135
+ placeholder="Enter your image description here...",
136
+ lines=3
137
+ )
138
+ filename_input = gr.Textbox(
139
+ label="Output Filename",
140
+ value="generated_image",
141
+ placeholder="Enter desired filename (without extension)"
142
+ )
143
+ generate_btn = gr.Button("Generate Image")
144
 
145
  # State to maintain chat history
146
  chat_state = gr.State([])
147
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
148
  # Connect the button to the chat handler
149
+ generate_btn.click(
150
  fn=chat_handler,
151
+ inputs=[prompt_input, image_input, chat_state, filename_input],
152
+ outputs=[chatbot, uploaded_image_output, generated_image_output, prompt_input]
153
  )
154
 
155
  # Also allow Enter key to submit
156
  prompt_input.submit(
157
  fn=chat_handler,
158
+ inputs=[prompt_input, image_input, chat_state, filename_input],
159
+ outputs=[chatbot, uploaded_image_output, generated_image_output, prompt_input]
160
  )
161
 
162
  if __name__ == "__main__":