moslem commited on
Commit
aa0ae99
·
verified ·
1 Parent(s): 4f26bd6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -40
app.py CHANGED
@@ -1,40 +1,21 @@
1
- from fastapi import FastAPI
2
- from transformers import pipeline
3
-
4
- # ساخت اپ FastAPI
5
- app = FastAPI(
6
- title="English–Persian Translator API",
7
- description="A lightweight translation API using Helsinki-NLP models.",
8
- version="1.0.0"
9
- )
10
-
11
- # لود مدل ترجمه سبک
12
- # مدل‌های Helsinki-NLP بسیار سبک و سازگار با اسپیس‌های رایگان هستند
13
- translator_en_fa = pipeline("translation", model="Helsinki-NLP/opus-mt-en-fa")
14
- translator_fa_en = pipeline("translation", model="Helsinki-NLP/opus-mt-fa-en")
15
-
16
- @app.get("/")
17
- def home():
18
- return {
19
- "message": "Welcome to the English–Persian Translator API!",
20
- "usage": {
21
- "English Persian": "/translate/en2fa?text=Hello world",
22
- "Persian → English": "/translate/fa2en?text=سلام دنیا"
23
- }
24
- }
25
-
26
- @app.get("/translate/en2fa")
27
- def translate_en_to_fa(text: str):
28
- """
29
- ترجمه از انگلیسی به فارسی
30
- """
31
- output = translator_en_fa(text)
32
- return {"input": text, "translation": output[0]["translation_text"]}
33
-
34
- @app.get("/translate/fa2en")
35
- def translate_fa_to_en(text: str):
36
- """
37
- ترجمه از فارسی به انگلیسی
38
- """
39
- output = translator_fa_en(text)
40
- return {"input": text, "translation": output[0]["translation_text"]}
 
1
+ from fastapi import FastAPI, Query
2
+ from transformers import pipeline
3
+
4
+ # ساخت اپ FastAPI
5
+ app = FastAPI(title="English–Persian Translator")
6
+
7
+ # لود مدل سبک ترجمه (عمومی و بدون نیاز به لاگین)
8
+ translator_en_fa = pipeline(
9
+ "translation_en_to_fa",
10
+ model="Helsinki-NLP/opus-mt-en-fa",
11
+ trust_remote_code=True
12
+ )
13
+
14
+ @app.get("/")
15
+ def home():
16
+ return {"message": "Welcome to English–Persian Translator!"}
17
+
18
+ @app.get("/translate")
19
+ def translate(text: str = Query(..., description="English text to translate to Persian")):
20
+ result = translator_en_fa(text)
21
+ return {"input": text, "translation": result[0]["translation_text"]}