Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,141 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import uuid
|
| 3 |
+
import requests
|
| 4 |
+
import threading
|
| 5 |
+
import time
|
| 6 |
+
from flask import Flask, request, jsonify, render_template, send_from_directory
|
| 7 |
+
from deep_translator import GoogleTranslator
|
| 8 |
+
|
| 9 |
+
app = Flask(__name__)
|
| 10 |
+
|
| 11 |
+
GITHUB_TOKEN = os.environ.get("GITHUB_TOKEN")
|
| 12 |
+
GITHUB_USER = os.environ.get("GITHUB_USER", "Hamed744")
|
| 13 |
+
GITHUB_REPO = os.environ.get("GITHUB_REPO", "vid-test")
|
| 14 |
+
SPACE_HOST = os.environ.get("SPACE_HOST", "")
|
| 15 |
+
SPACE_URL = f"https://{SPACE_HOST}"
|
| 16 |
+
|
| 17 |
+
def delete_github_run(gh_run_id):
|
| 18 |
+
time.sleep(20)
|
| 19 |
+
url = f"https://api.github.com/repos/{GITHUB_USER}/{GITHUB_REPO}/actions/runs/{gh_run_id}"
|
| 20 |
+
headers = {"Accept": "application/vnd.github.v3+json", "Authorization": f"token {GITHUB_TOKEN}"}
|
| 21 |
+
requests.delete(url, headers=headers)
|
| 22 |
+
|
| 23 |
+
@app.route('/')
|
| 24 |
+
def index():
|
| 25 |
+
return render_template('fluxpro.html')
|
| 26 |
+
|
| 27 |
+
@app.route('/<page_name>')
|
| 28 |
+
def serve_html(page_name):
|
| 29 |
+
if not page_name.endswith('.html'):
|
| 30 |
+
page_name += '.html'
|
| 31 |
+
try:
|
| 32 |
+
return render_template(page_name)
|
| 33 |
+
except:
|
| 34 |
+
return "Page not found", 404
|
| 35 |
+
|
| 36 |
+
# --- API برای ساخت عکس (Flux و Cartoon) ---
|
| 37 |
+
@app.route('/api/generate', methods=['POST'])
|
| 38 |
+
def generate():
|
| 39 |
+
if not GITHUB_TOKEN:
|
| 40 |
+
return jsonify({"status": "error", "message": "Secret GITHUB_TOKEN is missing!"}), 500
|
| 41 |
+
|
| 42 |
+
data = request.json
|
| 43 |
+
persian_prompt = data.get('prompt', '')
|
| 44 |
+
|
| 45 |
+
try:
|
| 46 |
+
english_prompt = GoogleTranslator(source='fa', target='en').translate(persian_prompt)
|
| 47 |
+
except:
|
| 48 |
+
english_prompt = persian_prompt
|
| 49 |
+
|
| 50 |
+
run_id = str(uuid.uuid4())
|
| 51 |
+
url = f"https://api.github.com/repos/{GITHUB_USER}/{GITHUB_REPO}/dispatches"
|
| 52 |
+
headers = {"Accept": "application/vnd.github.v3+json", "Authorization": f"token {GITHUB_TOKEN}"}
|
| 53 |
+
|
| 54 |
+
payload = {
|
| 55 |
+
"event_type": data.get('action_name', 'generate-flux'),
|
| 56 |
+
"client_payload": {
|
| 57 |
+
"prompt": english_prompt,
|
| 58 |
+
"width": data.get('width', 1024),
|
| 59 |
+
"height": data.get('height', 1024),
|
| 60 |
+
"run_id": run_id,
|
| 61 |
+
"space_url": SPACE_URL
|
| 62 |
+
}
|
| 63 |
+
}
|
| 64 |
+
r = requests.post(url, headers=headers, json=payload)
|
| 65 |
+
|
| 66 |
+
if r.status_code == 204:
|
| 67 |
+
# کلمه کلیدی translated_prompt برگردانده شد تا با fluxpro.html و cartoon.html کاملا سازگار باشد
|
| 68 |
+
return jsonify({
|
| 69 |
+
"status": "success",
|
| 70 |
+
"run_id": run_id,
|
| 71 |
+
"translated_prompt": english_prompt,
|
| 72 |
+
"translated": english_prompt
|
| 73 |
+
})
|
| 74 |
+
else:
|
| 75 |
+
return jsonify({"status": "error", "message": r.text}), 400
|
| 76 |
+
|
| 77 |
+
# --- API جدید مخصوص ویرایش عکس (AI Photoshop) ---
|
| 78 |
+
@app.route('/api/edit', methods=['POST'])
|
| 79 |
+
def edit_image():
|
| 80 |
+
if not GITHUB_TOKEN: return jsonify({"status": "error", "message": "Token missing"}), 500
|
| 81 |
+
|
| 82 |
+
if 'image' not in request.files: return jsonify({"status": "error", "message": "Image required"}), 400
|
| 83 |
+
file = request.files['image']
|
| 84 |
+
persian_prompt = request.form.get('prompt', '')
|
| 85 |
+
|
| 86 |
+
try: english_prompt = GoogleTranslator(source='fa', target='en').translate(persian_prompt)
|
| 87 |
+
except: english_prompt = persian_prompt
|
| 88 |
+
|
| 89 |
+
run_id = str(uuid.uuid4())
|
| 90 |
+
|
| 91 |
+
input_filename = f"{run_id}_input.png"
|
| 92 |
+
file.save(f"static/images/{input_filename}")
|
| 93 |
+
image_public_url = f"{SPACE_URL}/static/images/{input_filename}"
|
| 94 |
+
|
| 95 |
+
url = f"https://api.github.com/repos/{GITHUB_USER}/{GITHUB_REPO}/dispatches"
|
| 96 |
+
headers = {"Accept": "application/vnd.github.v3+json", "Authorization": f"token {GITHUB_TOKEN}"}
|
| 97 |
+
payload = {
|
| 98 |
+
"event_type": "generate-editor",
|
| 99 |
+
"client_payload": {
|
| 100 |
+
"prompt": english_prompt,
|
| 101 |
+
"image_url": image_public_url,
|
| 102 |
+
"run_id": run_id,
|
| 103 |
+
"space_url": SPACE_URL
|
| 104 |
+
}
|
| 105 |
+
}
|
| 106 |
+
r = requests.post(url, headers=headers, json=payload)
|
| 107 |
+
|
| 108 |
+
if r.status_code == 204:
|
| 109 |
+
return jsonify({"status": "success", "run_id": run_id, "translated": english_prompt})
|
| 110 |
+
else:
|
| 111 |
+
return jsonify({"status": "error", "message": r.text}), 400
|
| 112 |
+
|
| 113 |
+
# وبهوک برای دریافت فایل نهایی از گیتهاب
|
| 114 |
+
@app.route('/api/webhook/upload', methods=['POST'])
|
| 115 |
+
def webhook_upload():
|
| 116 |
+
run_id = request.form.get('run_id')
|
| 117 |
+
gh_run_id = request.form.get('github_run_id')
|
| 118 |
+
if 'file' not in request.files or not run_id:
|
| 119 |
+
return "Invalid request", 400
|
| 120 |
+
|
| 121 |
+
file = request.files['file']
|
| 122 |
+
file.save(f"static/images/{run_id}.webp")
|
| 123 |
+
if gh_run_id: threading.Thread(target=delete_github_run, args=(gh_run_id,)).start()
|
| 124 |
+
return "OK", 200
|
| 125 |
+
|
| 126 |
+
# چک کردن وضعیت توسط مرورگر
|
| 127 |
+
@app.route('/api/status/<run_id>')
|
| 128 |
+
def check_status(run_id):
|
| 129 |
+
path = f"static/images/{run_id}.webp"
|
| 130 |
+
if os.path.exists(path):
|
| 131 |
+
return jsonify({"status": "ready", "url": f"/{path}"})
|
| 132 |
+
return jsonify({"status": "processing"})
|
| 133 |
+
|
| 134 |
+
# دسترسی دادن به پوشه تصاویر
|
| 135 |
+
@app.route('/static/images/<filename>')
|
| 136 |
+
def serve_image(filename):
|
| 137 |
+
return send_from_directory('static/images', filename)
|
| 138 |
+
|
| 139 |
+
if __name__ == '__main__':
|
| 140 |
+
os.makedirs("static/images", exist_ok=True)
|
| 141 |
+
app.run(host='0.0.0.0', port=7860)
|