Update app.py
Browse files
app.py
CHANGED
|
@@ -8,8 +8,8 @@ from datetime import datetime, timedelta
|
|
| 8 |
from concurrent.futures import ThreadPoolExecutor
|
| 9 |
from typing import Dict, List, Optional, Any
|
| 10 |
import json
|
| 11 |
-
|
| 12 |
-
from fastapi import FastAPI, HTTPException, BackgroundTasks
|
| 13 |
from fastapi.middleware.cors import CORSMiddleware
|
| 14 |
from fastapi.responses import JSONResponse
|
| 15 |
from pydantic import BaseModel, Field
|
|
@@ -992,31 +992,72 @@ async def check_auto_charge_status(request: AutoChargeStatusRequest):
|
|
| 992 |
}
|
| 993 |
|
| 994 |
@app.post("/api/translate/heavy")
|
| 995 |
-
async def
|
| 996 |
-
|
| 997 |
-
import uuid
|
| 998 |
-
|
| 999 |
-
# تولید شناسه منحصر به فرد
|
| 1000 |
-
request_id = str(uuid.uuid4())
|
| 1001 |
|
| 1002 |
-
#
|
| 1003 |
-
|
| 1004 |
-
|
| 1005 |
-
|
| 1006 |
-
request.source_lang,
|
| 1007 |
-
request.target_lang,
|
| 1008 |
-
request_id,
|
| 1009 |
-
request.auto_charge
|
| 1010 |
-
)
|
| 1011 |
|
| 1012 |
-
|
| 1013 |
-
|
| 1014 |
-
|
| 1015 |
-
|
| 1016 |
-
|
| 1017 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1018 |
}
|
| 1019 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1020 |
@app.post("/api/translate/session")
|
| 1021 |
async def translate_with_session(request: TranslationRequest):
|
| 1022 |
"""ترجمه با session برای پیگیری پیشرفت"""
|
|
|
|
| 8 |
from concurrent.futures import ThreadPoolExecutor
|
| 9 |
from typing import Dict, List, Optional, Any
|
| 10 |
import json
|
| 11 |
+
import uuid
|
| 12 |
+
from fastapi import FastAPI, HTTPException, BackgroundTasks, Request
|
| 13 |
from fastapi.middleware.cors import CORSMiddleware
|
| 14 |
from fastapi.responses import JSONResponse
|
| 15 |
from pydantic import BaseModel, Field
|
|
|
|
| 992 |
}
|
| 993 |
|
| 994 |
@app.post("/api/translate/heavy")
|
| 995 |
+
async def heavy_translate(request: Request):
|
| 996 |
+
data = await request.json()
|
|
|
|
|
|
|
|
|
|
|
|
|
| 997 |
|
| 998 |
+
# اگر وردپرس request_id فرستاده بود، همونو استفاده کن
|
| 999 |
+
request_id = data.get("request_id")
|
| 1000 |
+
if not request_id:
|
| 1001 |
+
request_id = str(uuid.uuid4()) # فقط در صورت نبودن request_id
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1002 |
|
| 1003 |
+
text = data.get("text")
|
| 1004 |
+
source_lang = data.get("source_lang")
|
| 1005 |
+
target_lang = data.get("target_lang")
|
| 1006 |
+
auto_charge = data.get("auto_charge", False)
|
| 1007 |
+
notification_url = data.get("notification_url")
|
| 1008 |
+
|
| 1009 |
+
# ذخیره وضعیت اولیه (processing)
|
| 1010 |
+
translations[request_id] = {
|
| 1011 |
+
"status": "processing",
|
| 1012 |
+
"progress": 0,
|
| 1013 |
+
"elapsed_time": 0,
|
| 1014 |
+
"message": "Translation in progress..."
|
| 1015 |
}
|
| 1016 |
|
| 1017 |
+
# اجرای async برای شبیهسازی ترجمه
|
| 1018 |
+
asyncio.create_task(run_translation_job(request_id, text, source_lang, target_lang, notification_url))
|
| 1019 |
+
|
| 1020 |
+
return {"success": True, "request_id": request_id, "message": "Background translation started."}
|
| 1021 |
+
|
| 1022 |
+
|
| 1023 |
+
async def run_translation_job(request_id, text, source_lang, target_lang, notification_url):
|
| 1024 |
+
try:
|
| 1025 |
+
# شبیهسازی پیشرفت
|
| 1026 |
+
for i in range(1, 10):
|
| 1027 |
+
await asyncio.sleep(5) # مثلا هر ۵ ثانیه
|
| 1028 |
+
translations[request_id]["progress"] = i * 10
|
| 1029 |
+
translations[request_id]["elapsed_time"] += 5
|
| 1030 |
+
|
| 1031 |
+
# در پایان، متن ترجمهشده
|
| 1032 |
+
translated_text = f"[{target_lang}] {text}"
|
| 1033 |
+
translations[request_id] = {
|
| 1034 |
+
"status": "completed",
|
| 1035 |
+
"progress": 100,
|
| 1036 |
+
"elapsed_time": translations[request_id]["elapsed_time"],
|
| 1037 |
+
"message": "Translation completed successfully.",
|
| 1038 |
+
"result": translated_text
|
| 1039 |
+
}
|
| 1040 |
+
|
| 1041 |
+
# ارسال notify به وردپرس
|
| 1042 |
+
if notification_url:
|
| 1043 |
+
payload = {
|
| 1044 |
+
"request_id": request_id,
|
| 1045 |
+
"status": "completed",
|
| 1046 |
+
"result": translated_text
|
| 1047 |
+
}
|
| 1048 |
+
import requests
|
| 1049 |
+
try:
|
| 1050 |
+
r = requests.post(notification_url, json=payload, verify=False, timeout=10)
|
| 1051 |
+
print(f"📡 Sent notify for {request_id}, status={r.status_code}, body={r.text[:200]}")
|
| 1052 |
+
except Exception as e:
|
| 1053 |
+
print(f"❌ Notify failed for {request_id}: {e}")
|
| 1054 |
+
|
| 1055 |
+
except Exception as e:
|
| 1056 |
+
translations[request_id] = {
|
| 1057 |
+
"status": "failed",
|
| 1058 |
+
"message": f"Error: {e}"
|
| 1059 |
+
}
|
| 1060 |
+
|
| 1061 |
@app.post("/api/translate/session")
|
| 1062 |
async def translate_with_session(request: TranslationRequest):
|
| 1063 |
"""ترجمه با session برای پیگیری پیشرفت"""
|