Update app.py
Browse files
app.py
CHANGED
|
@@ -36,65 +36,90 @@ def serve_html(page_name):
|
|
| 36 |
# --- API برای ساخت عکس (Flux و Cartoon) ---
|
| 37 |
@app.route('/api/generate', methods=['POST'])
|
| 38 |
def generate():
|
| 39 |
-
if not GITHUB_TOKEN:
|
|
|
|
|
|
|
| 40 |
data = request.json
|
| 41 |
persian_prompt = data.get('prompt', '')
|
| 42 |
-
|
| 43 |
-
|
|
|
|
|
|
|
|
|
|
| 44 |
|
| 45 |
run_id = str(uuid.uuid4())
|
| 46 |
url = f"https://api.github.com/repos/{GITHUB_USER}/{GITHUB_REPO}/dispatches"
|
| 47 |
headers = {"Accept": "application/vnd.github.v3+json", "Authorization": f"token {GITHUB_TOKEN}"}
|
|
|
|
| 48 |
payload = {
|
| 49 |
"event_type": data.get('action_name', 'generate-flux'),
|
| 50 |
-
"client_payload": {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
}
|
| 52 |
r = requests.post(url, headers=headers, json=payload)
|
| 53 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
|
| 55 |
# --- API جدید مخصوص ویرایش عکس (AI Photoshop) ---
|
| 56 |
@app.route('/api/edit', methods=['POST'])
|
| 57 |
def edit_image():
|
| 58 |
-
if not GITHUB_TOKEN: return jsonify({"error": "Token missing"}), 500
|
| 59 |
|
| 60 |
-
|
| 61 |
-
if 'image' not in request.files: return jsonify({"error": "Image required"}), 400
|
| 62 |
file = request.files['image']
|
| 63 |
persian_prompt = request.form.get('prompt', '')
|
| 64 |
|
| 65 |
-
# ترجمه متن
|
| 66 |
try: english_prompt = GoogleTranslator(source='fa', target='en').translate(persian_prompt)
|
| 67 |
except: english_prompt = persian_prompt
|
| 68 |
|
| 69 |
run_id = str(uuid.uuid4())
|
| 70 |
|
| 71 |
-
# ذخیره عکس اولیه در داکر تا گیتهاب بتواند آن را دانلود کند
|
| 72 |
input_filename = f"{run_id}_input.png"
|
| 73 |
file.save(f"static/images/{input_filename}")
|
| 74 |
image_public_url = f"{SPACE_URL}/static/images/{input_filename}"
|
| 75 |
|
| 76 |
-
# ارسال دستور به گیتهاب
|
| 77 |
url = f"https://api.github.com/repos/{GITHUB_USER}/{GITHUB_REPO}/dispatches"
|
| 78 |
headers = {"Accept": "application/vnd.github.v3+json", "Authorization": f"token {GITHUB_TOKEN}"}
|
| 79 |
payload = {
|
| 80 |
"event_type": "generate-editor",
|
| 81 |
"client_payload": {
|
| 82 |
"prompt": english_prompt,
|
| 83 |
-
"image_url": image_public_url,
|
| 84 |
"run_id": run_id,
|
| 85 |
"space_url": SPACE_URL
|
| 86 |
}
|
| 87 |
}
|
| 88 |
-
requests.post(url, headers=headers, json=payload)
|
| 89 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
|
| 91 |
# وبهوک برای دریافت فایل نهایی از گیتهاب
|
| 92 |
@app.route('/api/webhook/upload', methods=['POST'])
|
| 93 |
def webhook_upload():
|
| 94 |
run_id = request.form.get('run_id')
|
| 95 |
gh_run_id = request.form.get('github_run_id')
|
|
|
|
|
|
|
|
|
|
| 96 |
file = request.files['file']
|
| 97 |
-
file.save(f"static/images/{run_id}.webp")
|
| 98 |
if gh_run_id: threading.Thread(target=delete_github_run, args=(gh_run_id,)).start()
|
| 99 |
return "OK", 200
|
| 100 |
|
|
|
|
| 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 |
|