Spaces:
Sleeping
Sleeping
from fastapi import FastAPI, HTTPException | |
import requests | |
from pydantic import BaseModel | |
from typing import Optional | |
app = FastAPI() | |
# Fonction de traduction du français vers le Lingala en utilisant l'API MyMemory | |
def translate_fr_to_ln(text: str) -> str: | |
url = "https://api.mymemory.translated.net/get" | |
params = { | |
'q': text, | |
'langpair': 'fr|ln' # Traduction du français vers le Lingala | |
} | |
response = requests.get(url, params=params) | |
if response.status_code == 200: | |
result = response.json() | |
return result['responseData']['translatedText'] | |
else: | |
raise Exception(f"Erreur: {response.status_code}, {response.text}") | |
# Fonction de traduction du Lingala vers le français en utilisant l'API MyMemory | |
def translate_ln_to_fr(text: str) -> str: | |
url = "https://api.mymemory.translated.net/get" | |
params = { | |
'q': text, | |
'langpair': 'ln|fr' # Traduction du Lingala vers le français | |
} | |
response = requests.get(url, params=params) | |
if response.status_code == 200: | |
result = response.json() | |
return result['responseData']['translatedText'] | |
else: | |
raise Exception(f"Erreur: {response.status_code}, {response.text}") | |
# Classe pour la réponse de traduction | |
class TranslationResponse(BaseModel): | |
translated_text: str | |
def translate_fr_to_ln_api(text: Optional[str] = None): | |
if text: | |
try: | |
# Appel de la fonction de traduction français -> Lingala | |
translated_text = translate_fr_to_ln(text) | |
return TranslationResponse(translated_text=translated_text) | |
except Exception as e: | |
raise HTTPException(status_code=500, detail=str(e)) | |
else: | |
raise HTTPException(status_code=400, detail="Le texte est requis pour la traduction.") | |
def translate_ln_to_fr_api(text: Optional[str] = None): | |
if text: | |
try: | |
# Appel de la fonction de traduction Lingala -> français | |
translated_text = translate_ln_to_fr(text) | |
return TranslationResponse(translated_text=translated_text) | |
except Exception as e: | |
raise HTTPException(status_code=500, detail=str(e)) | |
else: | |
raise HTTPException(status_code=400, detail="Le texte est requis pour la traduction.") | |
if __name__ == "__main__": | |
import uvicorn | |
uvicorn.run(app, host="0.0.0.0", port=7860) | |