Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -87,23 +87,23 @@ installed_pairs = set()
|
|
| 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")
|
| 90 |
-
def bergamot(input_text: list[str] = Query(description="Input list of strings"), sl: str = 'de', tl: str = 'en', model_name: Optional[str] = 'base/deen'):
|
| 91 |
"""
|
| 92 |
Translates the input text from the source language to the target language using a specified model.
|
| 93 |
Parameters:
|
| 94 |
-
input_text (str): The source text to be translated
|
| 95 |
sl (str): The source language of the input text
|
| 96 |
tl (str): The target language into which the input text is translated
|
| 97 |
model_name (str): The selected translation model name
|
| 98 |
Returns:
|
| 99 |
dict:
|
| 100 |
input_text(str): The input text in the source language
|
| 101 |
-
translated_text(str): The input text translated
|
| 102 |
-
message_text(str): A descriptive message summarizing the translation process. Example: "Translated from English to German with ende."
|
| 103 |
|
| 104 |
Example:
|
| 105 |
-
>>> bergamot("Hello world", "en", "de", "ende")
|
| 106 |
-
{"input_text": "Hello world", "translated_text": "Hallo Welt", "message_text": "Translated from English to German with ende."}
|
| 107 |
"""
|
| 108 |
try:
|
| 109 |
import bergamot
|
|
@@ -117,9 +117,7 @@ def bergamot(input_text: list[str] = Query(description="Input list of strings"),
|
|
| 117 |
localfolder = f"{subfolder}/{model_name}"
|
| 118 |
# List all files in the repo
|
| 119 |
all_files = list_repo_files(repo_id, repo_type='model')
|
| 120 |
-
print(len(all_files), 'installed_pairs', installed_pairs, 'defaultlocalfolder', localfolder)
|
| 121 |
-
# 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.
|
| 122 |
-
# 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.
|
| 123 |
for branch in branches:
|
| 124 |
branch_files = [f for f in all_files if f.startswith(branch)]
|
| 125 |
fullmodel_files = [f for f in branch_files if f.startswith(model_name)]
|
|
@@ -128,6 +126,8 @@ def bergamot(input_text: list[str] = Query(description="Input list of strings"),
|
|
| 128 |
print('branch_files', len(branch_files), 'model_files', model_files)
|
| 129 |
for file_path in model_files:
|
| 130 |
if localfolder not in installed_pairs:
|
|
|
|
|
|
|
| 131 |
local_path = hf_hub_download(repo_id=repo_id, subfolder=model_name, filename=file_path, local_dir=subfolder)
|
| 132 |
print(f"Downloaded to: {local_path}") # Downloaded to: deen/base/deen/config.yml
|
| 133 |
# localfolder = local_path.rsplit('/', 1)[0]
|
|
@@ -139,15 +139,15 @@ def bergamot(input_text: list[str] = Query(description="Input list of strings"),
|
|
| 139 |
print('installed_pairs', installed_pairs, 'localfolder', localfolder, 'dry_run', dryrunerror)
|
| 140 |
model = service.modelFromConfigPath(f"{localfolder}/config.yml")
|
| 141 |
# model = service.modelFromConfig(localfolder)
|
| 142 |
-
options = bergamot.ResponseOptions(alignment=False, qualityScores=False, HTML=False)
|
| 143 |
rawresponse = service.translate(model, bergamot.VectorString(input_text), options)
|
| 144 |
response: list|str = [r.target.text for r in rawresponse] if len(rawresponse) > 1 else next(iter(rawresponse)).target.text
|
| 145 |
print(type(input_text), len(input_text), len(rawresponse), type(response), response)
|
| 146 |
# response = [r.target.text for r in model_response][0] if isinstance(response, bergamot._bergamot.VectorResponse) else next(iter(response)).target.text
|
| 147 |
# response is of type bergamot._bergamot.VectorResponse, an iterable of bergamot._bergamot.Response
|
| 148 |
message_text = f"Translated from {sl} to {tl} with {model_name}."
|
| 149 |
-
except Exception as
|
| 150 |
-
response, message_text = str(
|
| 151 |
print(error)
|
| 152 |
return {"input": input_text, "translated_text": response, "message_text": message_text}
|
| 153 |
|
|
|
|
| 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")
|
| 90 |
+
def bergamot(input_text: list[str] = Query(description="Input string or list of strings"), sl: str = 'de', tl: str = 'en', model_name: Optional[str] = 'base/deen'):
|
| 91 |
"""
|
| 92 |
Translates the input text from the source language to the target language using a specified model.
|
| 93 |
Parameters:
|
| 94 |
+
input_text (str | list[str]): The source text to be translated, can be either a string or a list of strings
|
| 95 |
sl (str): The source language of the input text
|
| 96 |
tl (str): The target language into which the input text is translated
|
| 97 |
model_name (str): The selected translation model name
|
| 98 |
Returns:
|
| 99 |
dict:
|
| 100 |
input_text(str): The input text in the source language
|
| 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
|
|
|
|
| 117 |
localfolder = f"{subfolder}/{model_name}"
|
| 118 |
# List all files in the repo
|
| 119 |
all_files = list_repo_files(repo_id, repo_type='model')
|
| 120 |
+
print('input text type:', type(input_text), len(all_files), 'installed_pairs', installed_pairs, 'defaultlocalfolder', localfolder)
|
|
|
|
|
|
|
| 121 |
for branch in branches:
|
| 122 |
branch_files = [f for f in all_files if f.startswith(branch)]
|
| 123 |
fullmodel_files = [f for f in branch_files if f.startswith(model_name)]
|
|
|
|
| 126 |
print('branch_files', len(branch_files), 'model_files', model_files)
|
| 127 |
for file_path in model_files:
|
| 128 |
if localfolder not in installed_pairs:
|
| 129 |
+
# 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.
|
| 130 |
+
# 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.
|
| 131 |
local_path = hf_hub_download(repo_id=repo_id, subfolder=model_name, filename=file_path, local_dir=subfolder)
|
| 132 |
print(f"Downloaded to: {local_path}") # Downloaded to: deen/base/deen/config.yml
|
| 133 |
# localfolder = local_path.rsplit('/', 1)[0]
|
|
|
|
| 139 |
print('installed_pairs', installed_pairs, 'localfolder', localfolder, 'dry_run', dryrunerror)
|
| 140 |
model = service.modelFromConfigPath(f"{localfolder}/config.yml")
|
| 141 |
# model = service.modelFromConfig(localfolder)
|
| 142 |
+
options = bergamot.ResponseOptions(alignment=False, sentenceMappings-False, qualityScores=False, HTML=False)
|
| 143 |
rawresponse = service.translate(model, bergamot.VectorString(input_text), options)
|
| 144 |
response: list|str = [r.target.text for r in rawresponse] if len(rawresponse) > 1 else next(iter(rawresponse)).target.text
|
| 145 |
print(type(input_text), len(input_text), len(rawresponse), type(response), response)
|
| 146 |
# response = [r.target.text for r in model_response][0] if isinstance(response, bergamot._bergamot.VectorResponse) else next(iter(response)).target.text
|
| 147 |
# response is of type bergamot._bergamot.VectorResponse, an iterable of bergamot._bergamot.Response
|
| 148 |
message_text = f"Translated from {sl} to {tl} with {model_name}."
|
| 149 |
+
except Exception as generalerror:
|
| 150 |
+
response, message_text = str(generalerror), f"Error translating from {sl} to {tl} with {model_name}: {generalerror}."
|
| 151 |
print(error)
|
| 152 |
return {"input": input_text, "translated_text": response, "message_text": message_text}
|
| 153 |
|