Spaces:
Running
Running
File size: 3,934 Bytes
53f7a0c 4a672f4 6a97cfb c882742 48e1f5b be112e0 25d7e14 4a672f4 62e7ddb 4a672f4 c882742 5a23c5b c882742 f78b83e b70fed0 f78b83e b70fed0 f78b83e b70fed0 c882742 b035646 c882742 c03350d c882742 9cc018a 46d342a be112e0 9cc018a c882742 55e119e c882742 55e119e c882742 55e119e c882742 55e119e c882742 173e4c6 c882742 25d7e14 5a23c5b e8494d6 |
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 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
from flask import Flask, request, jsonify, send_from_directory
import os
import psutil
import time
import datetime
import json
import subprocess
import string
import random
app = Flask(__name__)
# Define the path to static files
static_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)))
@app.route('/css/<path:filename>')
def css(filename):
return send_from_directory(os.path.join(static_dir, 'css'), filename)
@app.route('/js/<path:filename>')
def js(filename):
return send_from_directory(os.path.join(static_dir, 'js'), filename)
@app.route('/img/<path:filename>')
def img(filename):
return send_from_directory(os.path.join(static_dir, 'img'), filename)
@app.route('/')
def index():
return send_from_directory(static_dir, 'index.html')
@app.route('/ai')
def ai():
return send_from_directory(os.path.join(static_dir, 'views'), 'ai.html')
@app.route('/ai/<path:filename>')
def ai_file(filename):
if filename.endswith('.py'):
with open(os.path.join(static_dir, 'ai', filename), 'r') as f:
code = f.read()
output = subprocess.check_output(["python", "-c", code], shell=True, stderr=subprocess.STDOUT)
return output.decode('utf-8')
else:
return send_from_directory(os.path.join(static_dir, 'ai'), filename)
@app.route('/info')
def info():
ip = request.remote_addr
current_time = datetime.datetime.now().strftime("%H:%M:%S")
return jsonify({'ip': ip, 'current_time': current_time})
# Define the visitor count routes
visitor_count = 0
visitor_today = 0
last_update_date = datetime.datetime.now().date()
visitor_total = 0
@app.before_request
def update_visitor_counts():
global visitor_count, visitor_today, last_update_date, visitor_total
allowed_paths = ['/ai', '/api', '/tool']
if request.path.startswith(tuple(allowed_paths)):
current_date = datetime.datetime.now().date()
if current_date != last_update_date:
visitor_today = 0
last_update_date = current_date
visitor_count += 1
visitor_today += 1
visitor_total += 1
if datetime.datetime.now().hour == 0 and datetime.datetime.now().minute == 0:
reset_visitor_count()
@app.route('/count')
def count():
return jsonify({
'visitor_count': visitor_count,
'visitor_today': visitor_today,
'visitor_total': visitor_total
})
# Define the status route
@app.route('/status')
def status():
uptime_seconds = int(time.time() - psutil.boot_time())
uptime = str(datetime.timedelta(seconds=uptime_seconds))
memory_free = psutil.virtual_memory().available
memory_total = psutil.virtual_memory().total
return jsonify({'runtime': uptime, 'memory': f'{memory_free} / {memory_total}'})
# Handle 404 errors
@app.errorhandler(404)
def page_not_found(e):
return send_from_directory(static_dir, '404.html'), 404
def generate_random_text(length=10):
letters = string.ascii_lowercase
return ''.join(random.choice(letters) for _ in range(length))
@app.route('/upload', methods=['GET'])
def upload_image():
try:
# Get the URL parameter
url = request.args.get('url')
if not url:
return jsonify({'error': 'URL parameter is missing'}), 400
# Download the image
response = requests.get(url)
if response.status_code != 200:
return jsonify({'error': 'Failed to download image'}), 400
# Generate random text for the image name
random_text = generate_random_text()
image_name = f"{random_text}.png"
# Save the image
img = Image.open(BytesIO(response.content))
img.save(image_name, "PNG")
return jsonify({'success': f'Image uploaded as {image_name}'}), 200
except Exception as e:
return jsonify({'error': str(e)}), 500
if __name__ == "__main__":
app.run(host="0.0.0.0", port=7860, debug=True) |