Update app.py
Browse files
app.py
CHANGED
|
@@ -8,15 +8,31 @@ from datetime import datetime, timedelta
|
|
| 8 |
import threading
|
| 9 |
from queue import Queue
|
| 10 |
import logging
|
| 11 |
-
from typing import Dict, List, Tuple
|
| 12 |
-
from fastapi import FastAPI, HTTPException
|
| 13 |
from fastapi.middleware.cors import CORSMiddleware
|
|
|
|
| 14 |
import uvicorn
|
| 15 |
|
| 16 |
# Set up logging
|
| 17 |
logging.basicConfig(level=logging.INFO)
|
| 18 |
logger = logging.getLogger(__name__)
|
| 19 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
class TranslationCache:
|
| 21 |
def __init__(self, cache_duration_minutes: int = 60):
|
| 22 |
self.cache = {}
|
|
@@ -236,8 +252,52 @@ async def root():
|
|
| 236 |
return {"message": "Multilingual Translation API", "status": "active"}
|
| 237 |
|
| 238 |
@app.post("/api/translate")
|
| 239 |
-
async def api_translate(
|
| 240 |
"""API endpoint for translation"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 241 |
if not text.strip():
|
| 242 |
raise HTTPException(status_code=400, detail="No text provided")
|
| 243 |
|
|
|
|
| 8 |
import threading
|
| 9 |
from queue import Queue
|
| 10 |
import logging
|
| 11 |
+
from typing import Dict, List, Tuple, Optional
|
| 12 |
+
from fastapi import FastAPI, HTTPException, Request
|
| 13 |
from fastapi.middleware.cors import CORSMiddleware
|
| 14 |
+
from pydantic import BaseModel
|
| 15 |
import uvicorn
|
| 16 |
|
| 17 |
# Set up logging
|
| 18 |
logging.basicConfig(level=logging.INFO)
|
| 19 |
logger = logging.getLogger(__name__)
|
| 20 |
|
| 21 |
+
# Pydantic models for request/response
|
| 22 |
+
class TranslationRequest(BaseModel):
|
| 23 |
+
text: str
|
| 24 |
+
source_lang: str
|
| 25 |
+
target_lang: str
|
| 26 |
+
api_key: Optional[str] = None
|
| 27 |
+
|
| 28 |
+
class TranslationResponse(BaseModel):
|
| 29 |
+
translation: str
|
| 30 |
+
source_language: str
|
| 31 |
+
target_language: str
|
| 32 |
+
processing_time: float
|
| 33 |
+
character_count: int
|
| 34 |
+
status: str
|
| 35 |
+
|
| 36 |
class TranslationCache:
|
| 37 |
def __init__(self, cache_duration_minutes: int = 60):
|
| 38 |
self.cache = {}
|
|
|
|
| 252 |
return {"message": "Multilingual Translation API", "status": "active"}
|
| 253 |
|
| 254 |
@app.post("/api/translate")
|
| 255 |
+
async def api_translate(request: TranslationRequest):
|
| 256 |
"""API endpoint for translation"""
|
| 257 |
+
if not request.text.strip():
|
| 258 |
+
raise HTTPException(status_code=400, detail="No text provided")
|
| 259 |
+
|
| 260 |
+
source_code = LANGUAGE_MAP.get(request.source_lang)
|
| 261 |
+
target_code = LANGUAGE_MAP.get(request.target_lang)
|
| 262 |
+
|
| 263 |
+
if not source_code or not target_code:
|
| 264 |
+
raise HTTPException(status_code=400, detail="Invalid language codes")
|
| 265 |
+
|
| 266 |
+
try:
|
| 267 |
+
translation, processing_time = translator.translate_text(request.text, source_code, target_code)
|
| 268 |
+
|
| 269 |
+
return TranslationResponse(
|
| 270 |
+
translation=translation,
|
| 271 |
+
source_language=request.source_lang,
|
| 272 |
+
target_language=request.target_lang,
|
| 273 |
+
processing_time=processing_time,
|
| 274 |
+
character_count=len(request.text),
|
| 275 |
+
status="success"
|
| 276 |
+
)
|
| 277 |
+
except Exception as e:
|
| 278 |
+
raise HTTPException(status_code=500, detail=f"Translation error: {str(e)}")
|
| 279 |
+
|
| 280 |
+
# Alternative endpoint for form data (compatibility with WordPress)
|
| 281 |
+
@app.post("/api/translate/form")
|
| 282 |
+
async def api_translate_form(request: Request):
|
| 283 |
+
"""Alternative endpoint that accepts form data"""
|
| 284 |
+
try:
|
| 285 |
+
form_data = await request.form()
|
| 286 |
+
text = form_data.get("text", "")
|
| 287 |
+
source_lang = form_data.get("source_lang", "")
|
| 288 |
+
target_lang = form_data.get("target_lang", "")
|
| 289 |
+
api_key = form_data.get("api_key", None)
|
| 290 |
+
except:
|
| 291 |
+
try:
|
| 292 |
+
# Try to get JSON data if form data fails
|
| 293 |
+
json_data = await request.json()
|
| 294 |
+
text = json_data.get("text", "")
|
| 295 |
+
source_lang = json_data.get("source_lang", "")
|
| 296 |
+
target_lang = json_data.get("target_lang", "")
|
| 297 |
+
api_key = json_data.get("api_key", None)
|
| 298 |
+
except:
|
| 299 |
+
raise HTTPException(status_code=400, detail="Invalid request format")
|
| 300 |
+
|
| 301 |
if not text.strip():
|
| 302 |
raise HTTPException(status_code=400, detail="No text provided")
|
| 303 |
|