TiberiuCristianLeon commited on
Commit
0a92611
·
verified ·
1 Parent(s): d5c671b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -4
app.py CHANGED
@@ -55,23 +55,41 @@ def translate(text: str, model: Optional[str] = MODELS['enro']):
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: Optional[str] = 'bergamot'):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59
  try:
60
  import bergamot
61
  # print(type(input_text))
62
  # input_text = [input_text] if isinstance(input_text, str) else input_text
63
  config = bergamot.ServiceConfig(numWorkers=4)
64
  service = bergamot.Service(config)
65
- models = service.modelFromConfigPath("./deen/bergamot.config.yml")
66
  options = bergamot.ResponseOptions(alignment=False, qualityScores=False, HTML=False)
67
- rawresponse = service.translate(models, bergamot.VectorString(input_text), options)
68
  response: list|str = [r.target.text for r in rawresponse] if len(rawresponse) > 1 else next(iter(rawresponse)).target.text
69
  print(type(input_text), len(input_text), len(rawresponse), type(response), response)
70
  # response = [r.target.text for r in model_response][0] if isinstance(response, bergamot._bergamot.VectorResponse) else next(iter(response)).target.text
71
  # response is of type bergamot._bergamot.VectorResponse, an iterable of bergamot._bergamot.Response
 
72
  except Exception as error:
73
  response = error
74
- return {"input": input_text, "result": response, "model": model}
75
 
76
  # Create an MCP server based on this app
77
  mcp = FastApiMCP(
 
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'):
59
+ """
60
+ Translates the input text from the source language to the target language using a specified model.
61
+ Parameters:
62
+ input_text (str): The source text to be translated
63
+ sl (str): The source language of the input text
64
+ tl (str): The target language into which the input text is translated
65
+ model_name (str): The selected translation model name
66
+ Returns:
67
+ dict:
68
+ input_text(str): The input text in the source language
69
+ translated_text(str): The input text translated to the selected target language
70
+ message_text(str): A descriptive message summarizing the translation process. Example: "Translated from English to German with ende."
71
+
72
+ Example:
73
+ >>> bergamot("Hello world", "en", "de", "ende")
74
+ {"input_text": "Hello world", "translated_text": "Hallo Welt", "message_text": "Translated from English to German with ende."}
75
+ """
76
  try:
77
  import bergamot
78
  # print(type(input_text))
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
+ model = service.modelFromConfigPath(f"./{model_name}/bergamot.config.yml")
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
86
  print(type(input_text), len(input_text), len(rawresponse), type(response), response)
87
  # response = [r.target.text for r in model_response][0] if isinstance(response, bergamot._bergamot.VectorResponse) else next(iter(response)).target.text
88
  # response is of type bergamot._bergamot.VectorResponse, an iterable of bergamot._bergamot.Response
89
+ message_text = f"Translated from {sl} to {tl} with {model_name}."
90
  except Exception as error:
91
  response = error
92
+ return {"input": input_text, "translated_text": response, "message_text": message_text}
93
 
94
  # Create an MCP server based on this app
95
  mcp = FastApiMCP(