Spaces:
Running
Running
Kastg
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -5,67 +5,10 @@ from PIL import Image
|
|
5 |
from io import BytesIO
|
6 |
import psutil
|
7 |
import time
|
|
|
8 |
|
9 |
app = Flask(__name__)
|
10 |
|
11 |
-
BASE_URL = "https://kastg-cdn.hf.space" # Update this with your actual base URL
|
12 |
-
|
13 |
-
IMAGE_FOLDER = "images"
|
14 |
-
app.config["UPLOAD_FOLDER"] = IMAGE_FOLDER
|
15 |
-
|
16 |
-
@app.route("/images/<filename>")
|
17 |
-
def get_image(filename):
|
18 |
-
return send_from_directory(app.config["UPLOAD_FOLDER"], filename)
|
19 |
-
|
20 |
-
# Create folders to store uploaded images
|
21 |
-
UPLOAD_FOLDER = "uploads"
|
22 |
-
IMAGE_FOLDER = os.path.join(UPLOAD_FOLDER, "images")
|
23 |
-
|
24 |
-
for folder in [UPLOAD_FOLDER, IMAGE_FOLDER]:
|
25 |
-
os.makedirs(folder, exist_ok=True)
|
26 |
-
|
27 |
-
# Create a dictionary to store available images
|
28 |
-
available_images = {}
|
29 |
-
|
30 |
-
@app.route("/upload", methods=["GET"])
|
31 |
-
def upload_image():
|
32 |
-
url = request.args.get("url")
|
33 |
-
|
34 |
-
if not url:
|
35 |
-
return jsonify({"message": "URL parameter is missing"}), 400
|
36 |
-
|
37 |
-
# Check if the URL is valid and get the image content
|
38 |
-
try:
|
39 |
-
response = requests.get(url)
|
40 |
-
response.raise_for_status()
|
41 |
-
except requests.exceptions.RequestException as e:
|
42 |
-
return jsonify({"message": f"Error fetching image from URL: {str(e)}"}), 400
|
43 |
-
|
44 |
-
# Check if the response content is an image
|
45 |
-
content_type = response.headers.get('content-type')
|
46 |
-
if 'image' not in content_type:
|
47 |
-
return jsonify({"message": "The provided URL does not lead to an image"}), 400
|
48 |
-
|
49 |
-
# Open the image using PIL
|
50 |
-
try:
|
51 |
-
img = Image.open(BytesIO(response.content))
|
52 |
-
except Exception as e:
|
53 |
-
return jsonify({"message": f"Error processing image: {str(e)}"}), 400
|
54 |
-
|
55 |
-
# Save the image
|
56 |
-
filename = os.path.basename(url)
|
57 |
-
image_path = os.path.join(IMAGE_FOLDER, filename)
|
58 |
-
img.save(image_path)
|
59 |
-
|
60 |
-
# Update available images dictionary
|
61 |
-
available_images[filename] = {"url": url, "name": filename}
|
62 |
-
|
63 |
-
# Construct the full image URL including the site name
|
64 |
-
full_image_url = f"{BASE_URL}/images/{filename}"
|
65 |
-
|
66 |
-
return jsonify({"message": "File uploaded successfully", "image_url": full_image_url})
|
67 |
-
|
68 |
-
|
69 |
@app.route("/uptime")
|
70 |
def uptime():
|
71 |
# Get system uptime using psutil
|
|
|
5 |
from io import BytesIO
|
6 |
import psutil
|
7 |
import time
|
8 |
+
import datetime # Add this line to import datetime module
|
9 |
|
10 |
app = Flask(__name__)
|
11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
@app.route("/uptime")
|
13 |
def uptime():
|
14 |
# Get system uptime using psutil
|