Athspi commited on
Commit
6d771df
·
verified ·
1 Parent(s): cf06554

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -15
app.py CHANGED
@@ -10,15 +10,12 @@ from PIL import Image
10
  # Configure Gemini API key using an environment variable
11
  genai.configure(api_key=os.getenv("GEMINI_API_KEY"))
12
 
13
- # Initialize Gemini client
14
- client = genai.Client()
15
-
16
  # Initialize Flask app
17
  app = Flask(__name__)
18
 
19
  def save_image(image_data):
20
  """Save the image from a base64 string to a temporary file and return its path."""
21
- # image_data is expected to be in the format "data:image/png;base64,...."
22
  header, encoded = image_data.split(',', 1)
23
  image_bytes = base64.b64decode(encoded)
24
  image = Image.open(io.BytesIO(image_bytes))
@@ -28,13 +25,13 @@ def save_image(image_data):
28
 
29
  def remove_object_from_image(image_path, object_type):
30
  """Use Gemini API to remove a specified object from the image."""
31
- # Upload the image file to Gemini
32
- uploaded_file = client.files.upload(file=image_path)
33
 
34
  # Prepare the input parts:
35
- # 1. The image file
36
  parts = [types.Part.from_uri(file_uri=uploaded_file.uri, mime_type="image/png")]
37
- # 2. The Gemini magic text instructing removal
38
  if object_type:
39
  parts.append(types.Part.from_text(text=f"Remove {object_type} from the image"))
40
 
@@ -51,16 +48,16 @@ def remove_object_from_image(image_path, object_type):
51
 
52
  result_image = None
53
 
54
- # Process the generation stream from Gemini
55
- for chunk in client.models.generate_content_stream(
56
  model="gemini-2.0-flash-exp-image-generation",
57
  contents=contents,
58
  config=generate_content_config,
59
  ):
60
- if chunk.candidates and chunk.candidates[0].content and chunk.candidates[0].content.parts:
 
61
  part = chunk.candidates[0].content.parts[0]
62
  if part.inline_data:
63
- # Save the generated binary image data
64
  file_name = "generated_output.png"
65
  with open(file_name, "wb") as f:
66
  f.write(part.inline_data.data)
@@ -80,11 +77,11 @@ def process_image():
80
  image_data = data['image']
81
  object_type = data['objectType']
82
 
83
- # Save the uploaded image locally
84
  image_path = save_image(image_data)
85
 
86
  try:
87
- # Use Gemini to remove the object from the image
88
  result_image = remove_object_from_image(image_path, object_type)
89
  return jsonify({'success': True, 'resultPath': result_image})
90
  except Exception as e:
@@ -92,4 +89,4 @@ def process_image():
92
 
93
  if __name__ == '__main__':
94
  # For local testing; in production, your hosting provider will manage the server.
95
- app.run(host="0.0.0.0", port=7860)
 
10
  # Configure Gemini API key using an environment variable
11
  genai.configure(api_key=os.getenv("GEMINI_API_KEY"))
12
 
 
 
 
13
  # Initialize Flask app
14
  app = Flask(__name__)
15
 
16
  def save_image(image_data):
17
  """Save the image from a base64 string to a temporary file and return its path."""
18
+ # Expected format: "data:image/png;base64,...."
19
  header, encoded = image_data.split(',', 1)
20
  image_bytes = base64.b64decode(encoded)
21
  image = Image.open(io.BytesIO(image_bytes))
 
25
 
26
  def remove_object_from_image(image_path, object_type):
27
  """Use Gemini API to remove a specified object from the image."""
28
+ # Upload the image file to Gemini using the module-level function.
29
+ uploaded_file = genai.files.upload(file=image_path)
30
 
31
  # Prepare the input parts:
32
+ # 1. The image file.
33
  parts = [types.Part.from_uri(file_uri=uploaded_file.uri, mime_type="image/png")]
34
+ # 2. The Gemini magic text instructing removal.
35
  if object_type:
36
  parts.append(types.Part.from_text(text=f"Remove {object_type} from the image"))
37
 
 
48
 
49
  result_image = None
50
 
51
+ # Use the module-level function to generate content.
52
+ for chunk in genai.models.generate_content_stream(
53
  model="gemini-2.0-flash-exp-image-generation",
54
  contents=contents,
55
  config=generate_content_config,
56
  ):
57
+ if (chunk.candidates and chunk.candidates[0].content and
58
+ chunk.candidates[0].content.parts):
59
  part = chunk.candidates[0].content.parts[0]
60
  if part.inline_data:
 
61
  file_name = "generated_output.png"
62
  with open(file_name, "wb") as f:
63
  f.write(part.inline_data.data)
 
77
  image_data = data['image']
78
  object_type = data['objectType']
79
 
80
+ # Save the uploaded image locally.
81
  image_path = save_image(image_data)
82
 
83
  try:
84
+ # Use Gemini to remove the object from the image.
85
  result_image = remove_object_from_image(image_path, object_type)
86
  return jsonify({'success': True, 'resultPath': result_image})
87
  except Exception as e:
 
89
 
90
  if __name__ == '__main__':
91
  # For local testing; in production, your hosting provider will manage the server.
92
+ app.run(debug=True)