Update app.py
Browse files
app.py
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
|
|
| 1 |
import os
|
| 2 |
import asyncio
|
| 3 |
import requests
|
|
@@ -234,7 +235,7 @@ async def serve_ttspro():
|
|
| 234 |
for path in ["ttspro.html", "templates/ttspro.html"]:
|
| 235 |
if os.path.exists(path):
|
| 236 |
return FileResponse(path)
|
| 237 |
-
return HTMLResponse("<h1>خطا: فایل ttspro.html پیدا نشد!
|
| 238 |
|
| 239 |
|
| 240 |
# =================================================================
|
|
@@ -304,15 +305,16 @@ async def chat_proxy_bridge(request: Request):
|
|
| 304 |
except Exception as e:
|
| 305 |
return JSONResponse({"status": "error", "message": "سرور موقتا در دسترس نیست."}, status_code=500)
|
| 306 |
|
| 307 |
-
# 🟢 ا
|
| 308 |
@app.post("/api/create_file")
|
| 309 |
-
async def
|
| 310 |
try:
|
| 311 |
body_bytes = await request.body()
|
| 312 |
|
| 313 |
async def stream_generator():
|
| 314 |
try:
|
| 315 |
-
|
|
|
|
| 316 |
async with client.stream(
|
| 317 |
"POST",
|
| 318 |
f"{CHAT_SPACE_URL}/api/create_file",
|
|
@@ -323,13 +325,14 @@ async def create_file_bridge(request: Request):
|
|
| 323 |
if chunk:
|
| 324 |
yield chunk
|
| 325 |
except Exception as e:
|
| 326 |
-
err =
|
| 327 |
-
yield
|
| 328 |
|
| 329 |
headers = {
|
| 330 |
"Cache-Control": "no-cache, no-transform",
|
| 331 |
"X-Accel-Buffering": "no",
|
| 332 |
-
"Connection": "keep-alive"
|
|
|
|
| 333 |
}
|
| 334 |
return StreamingResponse(stream_generator(), media_type="text/event-stream", headers=headers)
|
| 335 |
except Exception as e:
|
|
@@ -448,7 +451,7 @@ def get_hf_headers(request: Request):
|
|
| 448 |
"Accept": "application/json"
|
| 449 |
}
|
| 450 |
|
| 451 |
-
# کپی کردن هدر Content-Type برای پارس شدن صحیح فایلهای ارسالی کاربران در فلاسک
|
| 452 |
if "content-type" in request.headers:
|
| 453 |
headers["Content-Type"] = request.headers["content-type"]
|
| 454 |
|
|
@@ -459,17 +462,20 @@ def get_hf_headers(request: Request):
|
|
| 459 |
headers["X-Forwarded-For"] = request.client.host
|
| 460 |
return headers
|
| 461 |
|
| 462 |
-
|
|
|
|
|
|
|
|
|
|
| 463 |
|
| 464 |
@app.post("/api/generate")
|
| 465 |
async def submit_job(request: Request, background_tasks: BackgroundTasks):
|
| 466 |
payload = await request.json()
|
| 467 |
headers = get_hf_headers(request)
|
| 468 |
|
| 469 |
-
# تشخیص هوشمند درخواست تولید عکس (فراخوانی همگام و ایمن فلاسکِ اکشن)
|
| 470 |
if "action_name" in payload and "prompt" in payload:
|
| 471 |
try:
|
| 472 |
-
# استفاده از ترنسپورت همگامِ WSGITransport به همراه Client معمولی جهت پیشگیری از خطای ناهمگامی
|
| 473 |
transport = httpx.WSGITransport(app=action.app)
|
| 474 |
with httpx.Client(transport=transport, base_url="http://local", timeout=120.0) as client:
|
| 475 |
resp = client.post("/api/generate", json=payload, headers=headers)
|
|
@@ -581,7 +587,7 @@ async def smart_check_credit(request: Request):
|
|
| 581 |
resp = await client.post(url, json=payload, headers=headers)
|
| 582 |
return JSONResponse(resp.json(), status_code=resp.status_code)
|
| 583 |
else:
|
| 584 |
-
# فراخوانی مستقیم و داخلی فلاسک رانفلر با WSGITransport و کلاینت همگام
|
| 585 |
transport = httpx.WSGITransport(app=action.app)
|
| 586 |
with httpx.Client(transport=transport, base_url="http://local", timeout=30.0) as client:
|
| 587 |
resp = client.post("/api/check-credit", json=payload, headers=headers)
|
|
@@ -602,7 +608,7 @@ async def smart_use_credit(request: Request):
|
|
| 602 |
resp = await client.post(url, json=payload, headers=headers)
|
| 603 |
return JSONResponse(resp.json(), status_code=resp.status_code)
|
| 604 |
else:
|
| 605 |
-
# فراخوانی مستقیم و داخلی فلاسک رانفلر با WSGITransport و کلاینت همگام
|
| 606 |
transport = httpx.WSGITransport(app=action.app)
|
| 607 |
with httpx.Client(transport=transport, base_url="http://local", timeout=30.0) as client:
|
| 608 |
resp = client.post("/api/use-credit", json=payload, headers=headers)
|
|
@@ -694,6 +700,14 @@ async def serve_edit_studio():
|
|
| 694 |
return FileResponse(path)
|
| 695 |
return HTMLResponse("<h1>خطا: فایل edit.html پیدا نشد!</h1>", status_code=404)
|
| 696 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 697 |
# --- مسیرهای اعتبار اختصاصی تصاویر ---
|
| 698 |
@app.post("/api/check-image-credit")
|
| 699 |
async def proxy_check_image_credit(request: Request):
|
|
|
|
| 1 |
+
# app.py (Runflare Combined Server Core)
|
| 2 |
import os
|
| 3 |
import asyncio
|
| 4 |
import requests
|
|
|
|
| 235 |
for path in ["ttspro.html", "templates/ttspro.html"]:
|
| 236 |
if os.path.exists(path):
|
| 237 |
return FileResponse(path)
|
| 238 |
+
return HTMLResponse("<h1>خطا: فایل ttspro.html پیدا نشد!</h1>", status_code=404)
|
| 239 |
|
| 240 |
|
| 241 |
# =================================================================
|
|
|
|
| 305 |
except Exception as e:
|
| 306 |
return JSONResponse({"status": "error", "message": "سرور موقتا در دسترس نیست."}, status_code=500)
|
| 307 |
|
| 308 |
+
# 🟢 اندپوینت جدید: پروکسی استریمینگ و فوقالعاده پایدار ساخت فایل (مقاله) از سرور چت اصلی
|
| 309 |
@app.post("/api/create_file")
|
| 310 |
+
async def create_file_proxy_bridge(request: Request):
|
| 311 |
try:
|
| 312 |
body_bytes = await request.body()
|
| 313 |
|
| 314 |
async def stream_generator():
|
| 315 |
try:
|
| 316 |
+
# اتصال مستقیم به سرور چت با تنظیم تایماوت ۵ دقیقهای برای مقالات طولانی
|
| 317 |
+
async with httpx.AsyncClient(timeout=httpx.Timeout(120.0, read=300.0)) as client:
|
| 318 |
async with client.stream(
|
| 319 |
"POST",
|
| 320 |
f"{CHAT_SPACE_URL}/api/create_file",
|
|
|
|
| 325 |
if chunk:
|
| 326 |
yield chunk
|
| 327 |
except Exception as e:
|
| 328 |
+
err = f"data: {json.dumps({'type': 'error', 'message': f'خطا در ارتباط رانفلر با سرور اصلی: {str(e)}'})}\n\n"
|
| 329 |
+
yield err.encode('utf-8')
|
| 330 |
|
| 331 |
headers = {
|
| 332 |
"Cache-Control": "no-cache, no-transform",
|
| 333 |
"X-Accel-Buffering": "no",
|
| 334 |
+
"Connection": "keep-alive",
|
| 335 |
+
"Transfer-Encoding": "chunked"
|
| 336 |
}
|
| 337 |
return StreamingResponse(stream_generator(), media_type="text/event-stream", headers=headers)
|
| 338 |
except Exception as e:
|
|
|
|
| 451 |
"Accept": "application/json"
|
| 452 |
}
|
| 453 |
|
| 454 |
+
# 🟢 کپی کردن هدر Content-Type برای پارس شدن صحیح فایلهای ارسالی کاربران در فلاسک
|
| 455 |
if "content-type" in request.headers:
|
| 456 |
headers["Content-Type"] = request.headers["content-type"]
|
| 457 |
|
|
|
|
| 462 |
headers["X-Forwarded-For"] = request.client.host
|
| 463 |
return headers
|
| 464 |
|
| 465 |
+
|
| 466 |
+
# =================================================================
|
| 467 |
+
# 4. بازنشانی آدرسها و وبهوکهای محلی اکشنز (جایگزین sada8888-tts3)
|
| 468 |
+
# =================================================================
|
| 469 |
|
| 470 |
@app.post("/api/generate")
|
| 471 |
async def submit_job(request: Request, background_tasks: BackgroundTasks):
|
| 472 |
payload = await request.json()
|
| 473 |
headers = get_hf_headers(request)
|
| 474 |
|
| 475 |
+
# 🟢 تشخیص هوشمند درخواست تولید عکس (فراخوانی همگام و ایمن فلاسکِ اکشن)
|
| 476 |
if "action_name" in payload and "prompt" in payload:
|
| 477 |
try:
|
| 478 |
+
# 🟢 استفاده از ترنسپورت همگامِ WSGITransport به همراه Client معمولی جهت پیشگیری از خطای ناهمگامی
|
| 479 |
transport = httpx.WSGITransport(app=action.app)
|
| 480 |
with httpx.Client(transport=transport, base_url="http://local", timeout=120.0) as client:
|
| 481 |
resp = client.post("/api/generate", json=payload, headers=headers)
|
|
|
|
| 587 |
resp = await client.post(url, json=payload, headers=headers)
|
| 588 |
return JSONResponse(resp.json(), status_code=resp.status_code)
|
| 589 |
else:
|
| 590 |
+
# 🟢 فراخوانی مستقیم و داخلی فلاسک رانفلر با WSGITransport و کلاینت همگام
|
| 591 |
transport = httpx.WSGITransport(app=action.app)
|
| 592 |
with httpx.Client(transport=transport, base_url="http://local", timeout=30.0) as client:
|
| 593 |
resp = client.post("/api/check-credit", json=payload, headers=headers)
|
|
|
|
| 608 |
resp = await client.post(url, json=payload, headers=headers)
|
| 609 |
return JSONResponse(resp.json(), status_code=resp.status_code)
|
| 610 |
else:
|
| 611 |
+
# 🟢 فراخوانی مستقیم و داخلی فلاسک رانفلر با WSGITransport و کلاینت همگام
|
| 612 |
transport = httpx.WSGITransport(app=action.app)
|
| 613 |
with httpx.Client(transport=transport, base_url="http://local", timeout=30.0) as client:
|
| 614 |
resp = client.post("/api/use-credit", json=payload, headers=headers)
|
|
|
|
| 700 |
return FileResponse(path)
|
| 701 |
return HTMLResponse("<h1>خطا: فایل edit.html پیدا نشد!</h1>", status_code=404)
|
| 702 |
|
| 703 |
+
@app.get("/ttspro", response_class=HTMLResponse)
|
| 704 |
+
@app.get("/ttspro/", response_class=HTMLResponse)
|
| 705 |
+
async def serve_ttspro():
|
| 706 |
+
for path in ["ttspro.html", "templates/ttspro.html"]:
|
| 707 |
+
if os.path.exists(path):
|
| 708 |
+
return FileResponse(path)
|
| 709 |
+
return HTMLResponse("<h1>خطا: فایل ttspro.html پیدا نشد!</h1>", status_code=404)
|
| 710 |
+
|
| 711 |
# --- مسیرهای اعتبار اختصاصی تصاویر ---
|
| 712 |
@app.post("/api/check-image-credit")
|
| 713 |
async def proxy_check_image_credit(request: Request):
|