Update app.py
Browse files
app.py
CHANGED
|
@@ -24,6 +24,7 @@ MAX_CHUNK_SIZE = 350
|
|
| 24 |
MAX_WORKERS = 3
|
| 25 |
CLEANUP_INTERVAL = 300 # 5 دقیقه
|
| 26 |
|
|
|
|
| 27 |
|
| 28 |
os.environ["HF_HOME"] = "/tmp/huggingface"
|
| 29 |
os.environ["TRANSFORMERS_CACHE"] = "/tmp/huggingface"
|
|
@@ -991,22 +992,26 @@ async def check_auto_charge_status(request: AutoChargeStatusRequest):
|
|
| 991 |
"status": req_data.get('status', 'unknown')
|
| 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())
|
| 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,
|
|
@@ -1014,7 +1019,6 @@ async def heavy_translate(request: Request):
|
|
| 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."}
|
|
@@ -1022,13 +1026,11 @@ async def heavy_translate(request: Request):
|
|
| 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",
|
|
@@ -1038,14 +1040,13 @@ async def run_translation_job(request_id, text, source_lang, target_lang, notifi
|
|
| 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]}")
|
|
|
|
| 24 |
MAX_WORKERS = 3
|
| 25 |
CLEANUP_INTERVAL = 300 # 5 دقیقه
|
| 26 |
|
| 27 |
+
translations = {}
|
| 28 |
|
| 29 |
os.environ["HF_HOME"] = "/tmp/huggingface"
|
| 30 |
os.environ["TRANSFORMERS_CACHE"] = "/tmp/huggingface"
|
|
|
|
| 992 |
"status": req_data.get('status', 'unknown')
|
| 993 |
}
|
| 994 |
|
| 995 |
+
app = FastAPI()
|
| 996 |
+
|
| 997 |
+
# دیکشنری سراسری برای وضعیت ترجمهها
|
| 998 |
+
translations = {}
|
| 999 |
+
|
| 1000 |
@app.post("/api/translate/heavy")
|
| 1001 |
async def heavy_translate(request: Request):
|
| 1002 |
data = await request.json()
|
| 1003 |
+
|
| 1004 |
+
# اگر وردپرس request_id فرستاده، همونو استفاده کن
|
| 1005 |
request_id = data.get("request_id")
|
| 1006 |
if not request_id:
|
| 1007 |
+
request_id = str(uuid.uuid4())
|
| 1008 |
+
|
| 1009 |
text = data.get("text")
|
| 1010 |
source_lang = data.get("source_lang")
|
| 1011 |
target_lang = data.get("target_lang")
|
| 1012 |
auto_charge = data.get("auto_charge", False)
|
| 1013 |
notification_url = data.get("notification_url")
|
| 1014 |
|
|
|
|
| 1015 |
translations[request_id] = {
|
| 1016 |
"status": "processing",
|
| 1017 |
"progress": 0,
|
|
|
|
| 1019 |
"message": "Translation in progress..."
|
| 1020 |
}
|
| 1021 |
|
|
|
|
| 1022 |
asyncio.create_task(run_translation_job(request_id, text, source_lang, target_lang, notification_url))
|
| 1023 |
|
| 1024 |
return {"success": True, "request_id": request_id, "message": "Background translation started."}
|
|
|
|
| 1026 |
|
| 1027 |
async def run_translation_job(request_id, text, source_lang, target_lang, notification_url):
|
| 1028 |
try:
|
|
|
|
| 1029 |
for i in range(1, 10):
|
| 1030 |
+
await asyncio.sleep(5)
|
| 1031 |
translations[request_id]["progress"] = i * 10
|
| 1032 |
translations[request_id]["elapsed_time"] += 5
|
| 1033 |
+
|
|
|
|
| 1034 |
translated_text = f"[{target_lang}] {text}"
|
| 1035 |
translations[request_id] = {
|
| 1036 |
"status": "completed",
|
|
|
|
| 1040 |
"result": translated_text
|
| 1041 |
}
|
| 1042 |
|
|
|
|
| 1043 |
if notification_url:
|
| 1044 |
+
import requests
|
| 1045 |
payload = {
|
| 1046 |
"request_id": request_id,
|
| 1047 |
"status": "completed",
|
| 1048 |
"result": translated_text
|
| 1049 |
}
|
|
|
|
| 1050 |
try:
|
| 1051 |
r = requests.post(notification_url, json=payload, verify=False, timeout=10)
|
| 1052 |
print(f"📡 Sent notify for {request_id}, status={r.status_code}, body={r.text[:200]}")
|