-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
60 lines (49 loc) · 2.19 KB
/
app.py
File metadata and controls
60 lines (49 loc) · 2.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
"""
Flask Web App for Image Summary Generator
Run: python app.py
Then open: http://localhost:5000
NO templates folder needed - HTML is embedded directly.
"""
from flask import Flask, request, jsonify
from PIL import Image
import io
import base64
import os
from model import ImageSummaryGenerator
app = Flask(__name__)
generator = ImageSummaryGenerator()
HTML_PAGE = open(os.path.join(os.path.dirname(os.path.abspath(__file__)), "index.html"), encoding="utf-8").read() if os.path.exists(os.path.join(os.path.dirname(os.path.abspath(__file__)), "index.html")) else "<h1>index.html not found</h1>"
@app.route("/")
def index():
# Try to load from index.html in same folder, fallback to templates/
base = os.path.dirname(os.path.abspath(__file__))
for path in [os.path.join(base, "index.html"),
os.path.join(base, "templates", "index.html")]:
if os.path.exists(path):
with open(path, encoding="utf-8") as f:
return f.read()
return "<h1 style='color:red'>ERROR: index.html not found next to app.py or in templates/</h1>", 500
@app.route("/summarize", methods=["POST"])
def summarize():
try:
if "image" in request.files:
file = request.files["image"]
image = Image.open(file.stream)
elif request.json and "image_base64" in request.json:
image_data = request.json["image_base64"].split(",")[-1]
image_bytes = base64.b64decode(image_data)
image = Image.open(io.BytesIO(image_bytes))
elif request.json and "image_url" in request.json:
import requests as req
response = req.get(request.json["image_url"], timeout=10)
image = Image.open(io.BytesIO(response.content))
else:
return jsonify({"error": "No image provided"}), 400
summary = generator.generate_summary(image)
return jsonify({"summary": summary, "status": "success"})
except Exception as e:
return jsonify({"error": str(e), "status": "error"}), 500
if __name__ == "__main__":
print("Starting Image Summary Generator Web App...")
print("Open http://localhost:5000 in your browser")
app.run(debug=True, host="0.0.0.0", port=5000)