Ronaldodev commited on
Commit
850aaf6
·
1 Parent(s): 435c64e

first commit

Browse files
Files changed (2) hide show
  1. app.py +71 -0
  2. requirements.txt +40 -0
app.py ADDED
@@ -0,0 +1,71 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI, HTTPException
2
+ import requests
3
+ from pydantic import BaseModel
4
+ from typing import Optional
5
+ from googletrans import Translator
6
+
7
+ translator = Translator()
8
+
9
+ app = FastAPI()
10
+
11
+ # Fonction de traduction du français vers le Lingala en utilisant l'API MyMemory
12
+ def translate_fr_to_ln(text: str) -> str:
13
+ url = "https://api.mymemory.translated.net/get"
14
+ params = {
15
+ 'q': text,
16
+ 'langpair': 'fr|ln' # Traduction du français vers le Lingala
17
+ }
18
+ response = requests.get(url, params=params)
19
+ if response.status_code == 200:
20
+ result = response.json()
21
+ return result['responseData']['translatedText']
22
+ else:
23
+ raise Exception(f"Erreur: {response.status_code}, {response.text}")
24
+
25
+ # Fonction de traduction du Lingala vers le français en utilisant l'API MyMemory
26
+ def translate_ln_to_fr(text: str) -> str:
27
+ url = "https://api.mymemory.translated.net/get"
28
+ params = {
29
+ 'q': text,
30
+ 'langpair': 'ln|fr' # Traduction du Lingala vers le français
31
+ }
32
+ response = requests.get(url, params=params)
33
+ if response.status_code == 200:
34
+ result = response.json()
35
+ return result['responseData']['translatedText']
36
+ else:
37
+ raise Exception(f"Erreur: {response.status_code}, {response.text}")
38
+
39
+
40
+ # Classe pour la réponse de traduction
41
+ class TranslationResponse(BaseModel):
42
+ translated_text: str
43
+
44
+ @app.get("/translate_fr_to_ln", response_model=TranslationResponse)
45
+ def translate_fr_to_ln_api(text: Optional[str] = None):
46
+ if text:
47
+ try:
48
+ # Appel de la fonction de traduction français -> Lingala
49
+ translated_text = translate_fr_to_ln(text)
50
+ return TranslationResponse(translated_text=translated_text)
51
+ except Exception as e:
52
+ raise HTTPException(status_code=500, detail=str(e))
53
+ else:
54
+ raise HTTPException(status_code=400, detail="Le texte est requis pour la traduction.")
55
+
56
+ @app.get("/translate_ln_to_fr", response_model=TranslationResponse)
57
+ def translate_ln_to_fr_api(text: Optional[str] = None):
58
+ if text:
59
+ try:
60
+ # Appel de la fonction de traduction Lingala -> français
61
+ translated_text = translate_ln_to_fr(text)
62
+ return TranslationResponse(translated_text=translated_text)
63
+ except Exception as e:
64
+ raise HTTPException(status_code=500, detail=str(e))
65
+ else:
66
+ raise HTTPException(status_code=400, detail="Le texte est requis pour la traduction.")
67
+
68
+
69
+ if __name__ == "__main__":
70
+ import uvicorn
71
+ uvicorn.run(app, host="0.0.0.0", port=7860)
requirements.txt ADDED
@@ -0,0 +1,40 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ annotated-types==0.7.0
2
+ anyio==4.7.0
3
+ cachetools==5.5.0
4
+ certifi==2024.12.14
5
+ chardet==3.0.4
6
+ charset-normalizer==3.4.0
7
+ click==8.1.7
8
+ deepl==1.20.0
9
+ fastapi==0.115.6
10
+ google-api-core==2.24.0
11
+ google-auth==2.37.0
12
+ google-cloud-core==2.4.1
13
+ google-cloud-translate==3.19.0
14
+ googleapis-common-protos==1.66.0
15
+ googletrans==3.0.0
16
+ grpc-google-iam-v1==0.13.1
17
+ grpcio==1.68.1
18
+ grpcio-status==1.68.1
19
+ h11==0.9.0
20
+ h2==3.2.0
21
+ hpack==3.0.0
22
+ hstspreload==2024.12.1
23
+ httpcore==0.9.1
24
+ httpx==0.13.3
25
+ hyperframe==5.2.0
26
+ idna==2.10
27
+ proto-plus==1.25.0
28
+ protobuf==5.29.2
29
+ pyasn1==0.6.1
30
+ pyasn1_modules==0.4.1
31
+ pydantic==2.10.4
32
+ pydantic_core==2.27.2
33
+ requests==2.32.3
34
+ rfc3986==1.5.0
35
+ rsa==4.9
36
+ sniffio==1.3.1
37
+ starlette==0.41.3
38
+ typing_extensions==4.12.2
39
+ urllib3==2.2.3
40
+ uvicorn==0.34.0