Athspi commited on
Commit
a97d8ed
·
verified ·
1 Parent(s): 063cc44

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -28
app.py CHANGED
@@ -2,8 +2,8 @@ from flask import Flask, render_template, request, jsonify
2
  import google.generativeai as genai
3
  import os
4
  import tempfile
 
5
  from dotenv import load_dotenv
6
- from google.generativeai.types import Content, Part
7
 
8
  # Load environment variables
9
  load_dotenv()
@@ -14,12 +14,10 @@ app = Flask(__name__)
14
  # Configure Gemini API
15
  genai.configure(api_key=os.getenv("GEMINI_API_KEY"))
16
 
17
- # Route for home page
18
  @app.route("/")
19
  def home():
20
  return render_template("index.html")
21
 
22
- # Route for processing images
23
  @app.route("/process", methods=["POST"])
24
  def process_image():
25
  try:
@@ -32,33 +30,26 @@ def process_image():
32
  return jsonify({"success": False, "message": "Invalid input data"})
33
 
34
  # Decode base64 image data
35
- import base64
36
  image_bytes = base64.b64decode(image_data.split(",")[1])
37
 
38
  # Create temporary directory
39
  temp_dir = tempfile.mkdtemp()
40
- upload_path = os.path.join(temp_dir, "input_image.png")
41
- with open(upload_path, "wb") as f:
42
  f.write(image_bytes)
43
 
44
- # Construct Gemini prompt
45
- gemini_prompt = f"Remove the {object_type} from the image and fill the area naturally."
46
 
47
- # Create request content
48
- contents = [
49
- Content(
50
- role="user",
51
- parts=[
52
- Part.from_text(gemini_prompt),
53
- Part.from_data(mime_type="image/png", data=image_bytes)
54
- ]
55
- )
56
- ]
57
 
58
- # Generate content with Gemini
59
- model = genai.GenerativeModel('gemini-2.0-flash-exp-image-generation')
60
  response = model.generate_content(
61
- contents=contents,
 
 
 
62
  generation_config={
63
  "temperature": 1,
64
  "top_p": 0.95,
@@ -71,16 +62,16 @@ def process_image():
71
  )
72
 
73
  # Process response
74
- if response.candidates:
75
- for candidate in response.candidates:
76
- for part in candidate.content.parts:
 
77
  if hasattr(part, 'inline_data'):
78
- output_path = os.path.join(temp_dir, "result.png")
79
  with open(output_path, "wb") as f:
80
  f.write(part.inline_data.data)
81
  return jsonify({
82
  "success": True,
83
- "resultPath": f"/static/results/result.png"
84
  })
85
 
86
  return jsonify({"success": False, "message": "No valid image data found in response"})
@@ -88,6 +79,5 @@ def process_image():
88
  except Exception as e:
89
  return jsonify({"success": False, "message": str(e)})
90
 
91
- # Run the app
92
  if __name__ == "__main__":
93
- app.run(debug=True)
 
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()
 
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:
 
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,
 
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"})
 
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=7860)