Sada8888 commited on
Commit
1e4ebab
·
verified ·
1 Parent(s): c56b580

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +59 -10
app.py CHANGED
@@ -64,7 +64,6 @@ def generate():
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,
@@ -74,12 +73,12 @@ def generate():
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
 
@@ -110,30 +109,80 @@ def edit_image():
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__':
 
64
  r = requests.post(url, headers=headers, json=payload)
65
 
66
  if r.status_code == 204:
 
67
  return jsonify({
68
  "status": "success",
69
  "run_id": run_id,
 
73
  else:
74
  return jsonify({"status": "error", "message": r.text}), 400
75
 
76
+ # --- API مخصوص ویرایش عکس (AI Photoshop) ---
77
  @app.route('/api/edit', methods=['POST'])
78
  def edit_image():
79
  if not GITHUB_TOKEN: return jsonify({"status": "error", "message": "Token missing"}), 500
 
80
  if 'image' not in request.files: return jsonify({"status": "error", "message": "Image required"}), 400
81
+
82
  file = request.files['image']
83
  persian_prompt = request.form.get('prompt', '')
84
 
 
109
  else:
110
  return jsonify({"status": "error", "message": r.text}), 400
111
 
112
+ # --- API جدید مخصوص تبدیل عکس به ویدیو (Image to Video) ---
113
+ @app.route('/api/generate-video', methods=['POST'])
114
+ def generate_video():
115
+ if not GITHUB_TOKEN: return jsonify({"status": "error", "message": "Token missing"}), 500
116
+ if 'image' not in request.files: return jsonify({"status": "error", "message": "Image required"}), 400
117
+
118
+ file = request.files['image']
119
+ persian_prompt = request.form.get('prompt', '')
120
+ duration = request.form.get('duration', 5.0)
121
+
122
+ try:
123
+ if persian_prompt.strip():
124
+ english_prompt = GoogleTranslator(source='fa', target='en').translate(persian_prompt)
125
+ else:
126
+ english_prompt = ""
127
+ except:
128
+ english_prompt = persian_prompt
129
+
130
+ run_id = str(uuid.uuid4())
131
+
132
+ input_filename = f"{run_id}_video_input.png"
133
+ file.save(f"static/images/{input_filename}")
134
+ image_public_url = f"{SPACE_URL}/static/images/{input_filename}"
135
+
136
+ url = f"https://api.github.com/repos/{GITHUB_USER}/{GITHUB_REPO}/dispatches"
137
+ headers = {"Accept": "application/vnd.github.v3+json", "Authorization": f"token {GITHUB_TOKEN}"}
138
+ payload = {
139
+ "event_type": "generate-video",
140
+ "client_payload": {
141
+ "prompt": english_prompt,
142
+ "duration": duration,
143
+ "image_url": image_public_url,
144
+ "run_id": run_id,
145
+ "space_url": SPACE_URL
146
+ }
147
+ }
148
+ r = requests.post(url, headers=headers, json=payload)
149
+
150
+ if r.status_code == 204:
151
+ return jsonify({"status": "success", "run_id": run_id, "translated": english_prompt})
152
+ else:
153
+ return jsonify({"status": "error", "message": r.text}), 400
154
+
155
+ # وب‌هوک برای دریافت فایل نهایی از گیت‌هاب (با پشتیبانی از فرمت‌های مختلف)
156
  @app.route('/api/webhook/upload', methods=['POST'])
157
  def webhook_upload():
158
  run_id = request.form.get('run_id')
159
  gh_run_id = request.form.get('github_run_id')
160
+ ext = request.form.get('ext', 'webp') # پسوند پیش‌فرض webp برای عکس و mp4 برای ویدیو
161
+
162
  if 'file' not in request.files or not run_id:
163
  return "Invalid request", 400
164
 
165
  file = request.files['file']
166
+ file.save(f"static/images/{run_id}.{ext}")
167
+
168
  if gh_run_id: threading.Thread(target=delete_github_run, args=(gh_run_id,)).start()
169
  return "OK", 200
170
 
171
  # چک کردن وضعیت توسط مرورگر
172
  @app.route('/api/status/<run_id>')
173
  def check_status(run_id):
174
+ # چک کردن فایل ویدیویی
175
+ if os.path.exists(f"static/images/{run_id}.mp4"):
176
+ return jsonify({"status": "ready", "url": f"/static/images/{run_id}.mp4"})
177
+ # چک کردن فایل تصویری
178
+ if os.path.exists(f"static/images/{run_id}.webp"):
179
+ return jsonify({"status": "ready", "url": f"/static/images/{run_id}.webp"})
180
+
181
  return jsonify({"status": "processing"})
182
 
183
+ # دسترسی دادن به پوشه تصاویر و ویدیوها
184
  @app.route('/static/images/<filename>')
185
+ def serve_file(filename):
186
  return send_from_directory('static/images', filename)
187
 
188
  if __name__ == '__main__':