Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -53,6 +53,9 @@ def translate(text: str, model: Optional[str] = MODELS['enro']):
|
|
| 53 |
translation: str = Translate.gemma_direct(text, model)
|
| 54 |
return {"input": text, "result": translation, "model": model}
|
| 55 |
|
|
|
|
|
|
|
|
|
|
| 56 |
# https://tiberiucristianleon-fastapimt.hf.space/bergamot?input_text=das%20ist%20keine%20gute%20Frau&input_text=das%20ist%20eine%20gute%20Nachricht&sl=de&tl=en&model=bergamot
|
| 57 |
@app.get("/bergamot", operation_id="get_bergamot", description="Translate text with Bergamot", tags=["bergamot"], summary="Translate text with Bergamot")
|
| 58 |
def bergamot(input_text: list[str] = Query(description="Input list of strings"), sl: str = 'de', tl: str = 'en', model_name: Optional[str] = 'deen'):
|
|
@@ -79,7 +82,21 @@ def bergamot(input_text: list[str] = Query(description="Input list of strings"),
|
|
| 79 |
# input_text = [input_text] if isinstance(input_text, str) else input_text
|
| 80 |
config = bergamot.ServiceConfig(numWorkers=4)
|
| 81 |
service = bergamot.Service(config)
|
| 82 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
options = bergamot.ResponseOptions(alignment=False, qualityScores=False, HTML=False)
|
| 84 |
rawresponse = service.translate(model, bergamot.VectorString(input_text), options)
|
| 85 |
response: list|str = [r.target.text for r in rawresponse] if len(rawresponse) > 1 else next(iter(rawresponse)).target.text
|
|
|
|
| 53 |
translation: str = Translate.gemma_direct(text, model)
|
| 54 |
return {"input": text, "result": translation, "model": model}
|
| 55 |
|
| 56 |
+
# Keep track of installed (src, tgt) pairs
|
| 57 |
+
installed_pairs = set()
|
| 58 |
+
|
| 59 |
# https://tiberiucristianleon-fastapimt.hf.space/bergamot?input_text=das%20ist%20keine%20gute%20Frau&input_text=das%20ist%20eine%20gute%20Nachricht&sl=de&tl=en&model=bergamot
|
| 60 |
@app.get("/bergamot", operation_id="get_bergamot", description="Translate text with Bergamot", tags=["bergamot"], summary="Translate text with Bergamot")
|
| 61 |
def bergamot(input_text: list[str] = Query(description="Input list of strings"), sl: str = 'de', tl: str = 'en', model_name: Optional[str] = 'deen'):
|
|
|
|
| 82 |
# input_text = [input_text] if isinstance(input_text, str) else input_text
|
| 83 |
config = bergamot.ServiceConfig(numWorkers=4)
|
| 84 |
service = bergamot.Service(config)
|
| 85 |
+
repo_id="TiberiuCristianLeon/Bergamot"
|
| 86 |
+
branches = ['base', 'base-memory', 'tiny']
|
| 87 |
+
subfolder = f"{src_code}{tgt_code}"
|
| 88 |
+
# List all files in the repo
|
| 89 |
+
all_files = list_repo_files(repo_id, repo_type='model')
|
| 90 |
+
for branch in branches:
|
| 91 |
+
branch_files = [f for f in all_files if f.startswith(branch)]
|
| 92 |
+
model_files = [f for f in branch_files if f.startswith(model_name)]
|
| 93 |
+
print(model_files)
|
| 94 |
+
for file_path in model_files:
|
| 95 |
+
if subfolder not in file_path:
|
| 96 |
+
local_path = hf_hub_download(repo_id=repo_id, filename=file_path)
|
| 97 |
+
print(f"Downloaded to: {local_path}")
|
| 98 |
+
installed_pairs.add(subfolder)
|
| 99 |
+
model = service.modelFromConfigPath(f"./{model_name}/config.yml")
|
| 100 |
options = bergamot.ResponseOptions(alignment=False, qualityScores=False, HTML=False)
|
| 101 |
rawresponse = service.translate(model, bergamot.VectorString(input_text), options)
|
| 102 |
response: list|str = [r.target.text for r in rawresponse] if len(rawresponse) > 1 else next(iter(rawresponse)).target.text
|