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