Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -82,8 +82,69 @@ def translate(input_text: str, model_name: str = MODELS['mttcbig'], sl: str = 'e
|
|
| 82 |
translation: str = src.Translate.gemma_direct(input_text, model_name)
|
| 83 |
return {"input_text": input_text, "translation": translation, "model_name": model_name, "message": message}
|
| 84 |
|
| 85 |
-
|
| 86 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
|
| 88 |
# 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
|
| 89 |
@app.get("/bergamot", operation_id="get_bergamot", description="Translate text with Bergamot", tags=["bergamot"], summary="Translate text with Bergamot")
|
|
@@ -101,17 +162,42 @@ def bergamot(input_text: list[str] = Query(description="Input string or list of
|
|
| 101 |
translated_text(str): The input text translated into the selected target language
|
| 102 |
message_text(str): A descriptive message summarizing the translation process. Example: "Translated from English to German with base/ende."
|
| 103 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 104 |
Example:
|
| 105 |
>>> bergamot("Hello world", "en", "de", "base/ende")
|
| 106 |
{"input_text": "Hello world", "translated_text": "Hallo Welt", "message_text": "Translated from English to German with base/ende."}
|
| 107 |
"""
|
| 108 |
try:
|
| 109 |
import bergamot
|
|
|
|
|
|
|
| 110 |
# input_text = [input_text] if isinstance(input_text, str) else input_text
|
| 111 |
config = bergamot.ServiceConfig(numWorkers=4)
|
| 112 |
service = bergamot.Service(config)
|
| 113 |
-
repo_id="TiberiuCristianLeon/Bergamot"
|
| 114 |
-
branches = ['base', 'base-memory', 'tiny']
|
| 115 |
subfolder = f"{sl}{tl}"
|
| 116 |
localfolder = f"{subfolder}/{model_name}"
|
| 117 |
# List all files in the repo
|
|
|
|
| 82 |
translation: str = src.Translate.gemma_direct(input_text, model_name)
|
| 83 |
return {"input_text": input_text, "translation": translation, "model_name": model_name, "message": message}
|
| 84 |
|
| 85 |
+
class Bergamot:
|
| 86 |
+
import bergamot
|
| 87 |
+
config = bergamot.ServiceConfig(numWorkers=4)
|
| 88 |
+
service = bergamot.Service(config)
|
| 89 |
+
|
| 90 |
+
def __init__(self, input_text, sl, tl, model_name):
|
| 91 |
+
# Keep track of installed (src, tgt) pairs
|
| 92 |
+
self.installed_pairs = set()
|
| 93 |
+
self.input_text, self.sl, self.tl, self.model_name = input_text, sl, tl, model_name
|
| 94 |
+
self.repo_id = "TiberiuCristianLeon/Bergamot"
|
| 95 |
+
self.branches = ['base', 'base-memory', 'tiny']
|
| 96 |
+
self.subfolder = f"{sl}{tl}"
|
| 97 |
+
self.localfolder = f"{subfolder}/{model_name}"
|
| 98 |
+
# List all files in the repo
|
| 99 |
+
self.all_files = list_repo_files(repo_id, repo_type='model')
|
| 100 |
+
|
| 101 |
+
def downloadbergamotfiles(self):
|
| 102 |
+
print('input text type:', type(self.input_text), len(self.all_files), 'installed_pairs', self.installed_pairs, 'defaultlocalfolder', self.localfolder)
|
| 103 |
+
try:
|
| 104 |
+
for branch in self.branches:
|
| 105 |
+
branch_files = [f for f in self.all_files if f.startswith(branch)]
|
| 106 |
+
fullmodel_files = [f for f in branch_files if f.startswith(self.model_name)]
|
| 107 |
+
print('branch_files', len(branch_files), 'fullmodel_files', fullmodel_files)
|
| 108 |
+
model_files = [f.split(f'{self.model_name}/')[1] for f in fullmodel_files]
|
| 109 |
+
print('branch_files', len(branch_files), 'model_files', model_files)
|
| 110 |
+
for file_path in model_files:
|
| 111 |
+
if localfolder not in installed_pairs:
|
| 112 |
+
# local_files_only (bool, optional, defaults to False) — If True, avoid downloading the file and return the path to the local cached file if it exists.
|
| 113 |
+
# dry_run (bool, optional, defaults to False) — If True, perform a dry run without actually downloading the file. Returns a DryRunFileInfo object containing information about what would be downloaded.
|
| 114 |
+
local_path = hf_hub_download(repo_id=self.repo_id, subfolder=self.model_name, filename=file_path, local_dir=self.subfolder)
|
| 115 |
+
print(f"Downloaded to: {local_path}") # Downloaded to: deen/base/deen/config.yml
|
| 116 |
+
## Check if model/localfolder in repo files, add to set if exists
|
| 117 |
+
modelcheck = [i for i in all_files if self.model_name in i]
|
| 118 |
+
print('Modelcheck', modelcheck)
|
| 119 |
+
if modelcheck:
|
| 120 |
+
print('Add to set after modelcheck', modelcheck)
|
| 121 |
+
self.installed_pairs.add(self.localfolder)
|
| 122 |
+
try:
|
| 123 |
+
dry_run = hf_hub_download(repo_id=self.repo_id, subfolder=self.model_name, filename='config.yml', local_dir=self.subfolder)
|
| 124 |
+
print('installed_pairs', self.installed_pairs, 'localfolder', self.localfolder, 'dry_run', type(dry_run), dry_run)
|
| 125 |
+
if isinstance(dry_run, str):
|
| 126 |
+
print('Add to set after dryrun', dry_run)
|
| 127 |
+
self.installed_pairs.add(localfolder)
|
| 128 |
+
except Exception as dryrunerror:
|
| 129 |
+
print('installed_pairs', self.installed_pairs, 'localfolder', self.localfolder, 'dry_runerror', dryrunerror)
|
| 130 |
+
except Exception as downloaderror:
|
| 131 |
+
response, message_text = str(downloaderror), f"Error downloading {self.model_name}: {downloaderror}."
|
| 132 |
+
print(downloaderror)
|
| 133 |
+
def translate(self):
|
| 134 |
+
try:
|
| 135 |
+
model = service.modelFromConfigPath(f"{self.localfolder}/config.yml")
|
| 136 |
+
# model = service.modelFromConfig(localfolder)
|
| 137 |
+
options = bergamot.ResponseOptions(alignment=False, sentenceMappings=False, qualityScores=False, HTML=False)
|
| 138 |
+
rawresponse = service.translate(model, bergamot.VectorString(self.input_text), options)
|
| 139 |
+
response: list|str = [r.target.text for r in rawresponse] if len(rawresponse) > 1 else next(iter(rawresponse)).target.text
|
| 140 |
+
print(type(self.input_text), len(self.input_text), len(rawresponse), type(response), response)
|
| 141 |
+
# response = [r.target.text for r in model_response][0] if isinstance(response, bergamot._bergamot.VectorResponse) else next(iter(response)).target.text
|
| 142 |
+
# response is of type bergamot._bergamot.VectorResponse, an iterable of bergamot._bergamot.Response
|
| 143 |
+
message_text = f"Translated from {self.sl} to {self.tl} with {self.model_name}."
|
| 144 |
+
except Exception as translateerror:
|
| 145 |
+
response, message_text = str(translateerror), f"Error translating from {self.sl} to {self.tl} with {self.model_name}: {translateerror}."
|
| 146 |
+
print(translateerror)
|
| 147 |
+
return {"input": self.input_text, "translated_text": response, "message_text": message_text}
|
| 148 |
|
| 149 |
# 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
|
| 150 |
@app.get("/bergamot", operation_id="get_bergamot", description="Translate text with Bergamot", tags=["bergamot"], summary="Translate text with Bergamot")
|
|
|
|
| 162 |
translated_text(str): The input text translated into the selected target language
|
| 163 |
message_text(str): A descriptive message summarizing the translation process. Example: "Translated from English to German with base/ende."
|
| 164 |
|
| 165 |
+
Example:
|
| 166 |
+
>>> bergamot("Hello world", "en", "de", "base/ende")
|
| 167 |
+
{"input_text": "Hello world", "translated_text": "Hallo Welt", "message_text": "Translated from English to German with base/ende."}
|
| 168 |
+
"""
|
| 169 |
+
try:
|
| 170 |
+
bergamotinstance = Bergamot(input_text, sl, tl, model_name)
|
| 171 |
+
bergamotinstance.downloadbergamotfiles()
|
| 172 |
+
return bergamotinstance.translate()
|
| 173 |
+
|
| 174 |
+
# 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
|
| 175 |
+
@app.get("/bergamots", operation_id="get_bergamot", description="Translate text with Bergamot", tags=["bergamot"], summary="Translate text with Bergamot")
|
| 176 |
+
def bergamots(input_text: list[str] = Query(description="Input string or list of strings"), sl: str = 'de', tl: str = 'en', model_name: Optional[str] = 'base/deen'):
|
| 177 |
+
"""
|
| 178 |
+
Translates the input text from the source language to the target language using a specified model.
|
| 179 |
+
Parameters:
|
| 180 |
+
input_text (str | list[str]): The source text to be translated, can be either a string or a list of strings
|
| 181 |
+
sl (str): The source language of the input text
|
| 182 |
+
tl (str): The target language into which the input text is translated
|
| 183 |
+
model_name (str): The selected translation model name
|
| 184 |
+
Returns:
|
| 185 |
+
dict:
|
| 186 |
+
input_text(str): The input text in the source language
|
| 187 |
+
translated_text(str): The input text translated into the selected target language
|
| 188 |
+
message_text(str): A descriptive message summarizing the translation process. Example: "Translated from English to German with base/ende."
|
| 189 |
+
|
| 190 |
Example:
|
| 191 |
>>> bergamot("Hello world", "en", "de", "base/ende")
|
| 192 |
{"input_text": "Hello world", "translated_text": "Hallo Welt", "message_text": "Translated from English to German with base/ende."}
|
| 193 |
"""
|
| 194 |
try:
|
| 195 |
import bergamot
|
| 196 |
+
repo_id="TiberiuCristianLeon/Bergamot"
|
| 197 |
+
branches = ['base', 'base-memory', 'tiny']
|
| 198 |
# input_text = [input_text] if isinstance(input_text, str) else input_text
|
| 199 |
config = bergamot.ServiceConfig(numWorkers=4)
|
| 200 |
service = bergamot.Service(config)
|
|
|
|
|
|
|
| 201 |
subfolder = f"{sl}{tl}"
|
| 202 |
localfolder = f"{subfolder}/{model_name}"
|
| 203 |
# List all files in the repo
|