Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,93 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
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()
|
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 |
+
# 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:
|
26 |
+
# Get data from request
|
27 |
+
data = request.json
|
28 |
+
image_data = data.get("image")
|
29 |
+
object_type = data.get("objectType")
|
30 |
+
|
31 |
+
if not image_data or not object_type:
|
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,
|
65 |
+
"top_k": 40,
|
66 |
+
"max_output_tokens": 8192,
|
67 |
+
},
|
68 |
+
safety_settings={
|
69 |
+
"HARM_CATEGORY_CIVIC_INTEGRITY": "BLOCK_NONE"
|
70 |
+
}
|
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"})
|
87 |
+
|
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)
|