TiberiuCristianLeon commited on
Commit
261cbe8
·
verified ·
1 Parent(s): 3aaf079

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +5 -7
app.py CHANGED
@@ -5,6 +5,7 @@ from typing import Optional
5
  from fastapi_mcp import FastApiMCP
6
  from huggingface_hub import hf_hub_download, list_repo_files
7
  from sentence_transformers import SentenceTransformer
 
8
 
9
  app = FastAPI()
10
  # app = FastAPI(docs_url="/docs")
@@ -83,7 +84,6 @@ def translate(input_text: str, model_name: str = MODELS['mttcbig'], sl: str = 'e
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
 
@@ -124,17 +124,15 @@ class Bergamot:
124
  except Exception as downloaderror:
125
  response, message_text = str(downloaderror), f"Error downloading {self.model_name}: {downloaderror}."
126
  print(downloaderror)
 
127
  def translate(self):
128
  self.downloadbergamotfiles()
129
  try:
130
  model = self.service.modelFromConfigPath(f"{self.localfolder}/config.yml")
131
- # model = self.service.modelFromConfig(self.localfolder)
132
  options = bergamot.ResponseOptions(alignment=False, sentenceMappings=False, qualityScores=False, HTML=False)
133
  rawresponse = self.service.translate(model, bergamot.VectorString(self.input_text), options)
134
  response: list|str = [r.target.text for r in rawresponse] if len(rawresponse) > 1 else next(iter(rawresponse)).target.text
135
  print(type(self.input_text), len(self.input_text), len(rawresponse), type(response), response)
136
- # response = [r.target.text for r in model_response][0] if isinstance(response, bergamot._bergamot.VectorResponse) else next(iter(response)).target.text
137
- # response is of type bergamot._bergamot.VectorResponse, an iterable of bergamot._bergamot.Response
138
  message_text = f"Translated from {self.sl} to {self.tl} with {self.model_name}."
139
  except Exception as translateerror:
140
  response, message_text = str(translateerror), f"Error translating from {self.sl} to {self.tl} with {self.model_name}: {translateerror}."
@@ -142,7 +140,7 @@ class Bergamot:
142
  return {"input": self.input_text, "translated_text": response, "message_text": message_text}
143
 
144
  # 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
145
- @app.get("/bergamot", operation_id="get_bergamot", description="Translate text with Bergamot", tags=["bergamot"], summary="Translate text with Bergamot")
146
  def bergamott(input_text: list[str] = Query(description="Input string or list of strings"), sl: str = 'de', tl: str = 'en', model_name: Optional[str] = 'base/deen'):
147
  """
148
  Translates the input text from the source language to the target language using a specified model.
@@ -169,7 +167,7 @@ def bergamott(input_text: list[str] = Query(description="Input string or list of
169
  print(mainerror)
170
 
171
  # 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
172
- @app.get("/bergamots", operation_id="get_bergamot", description="Translate text with Bergamot", tags=["bergamot"], summary="Translate text with Bergamot")
173
  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'):
174
  """
175
  Translates the input text from the source language to the target language using a specified model.
@@ -256,7 +254,7 @@ mcp = FastApiMCP(
256
  describe_all_responses=True,
257
  describe_full_response_schema=True,
258
  include_operations=["get_translate", "get_paraphrase"],
259
- include_tags=["paraphrase", "translate", "bergamot"]
260
  )
261
  # Mount the MCP server directly to the FASTAPI app
262
  mcp.mount()
 
5
  from fastapi_mcp import FastApiMCP
6
  from huggingface_hub import hf_hub_download, list_repo_files
7
  from sentence_transformers import SentenceTransformer
8
+ import bergamot
9
 
10
  app = FastAPI()
11
  # app = FastAPI(docs_url="/docs")
 
84
  return {"input_text": input_text, "translation": translation, "model_name": model_name, "message": message}
85
 
86
  class Bergamot:
 
87
  config = bergamot.ServiceConfig(numWorkers=4)
88
  service = bergamot.Service(config)
89
 
 
124
  except Exception as downloaderror:
125
  response, message_text = str(downloaderror), f"Error downloading {self.model_name}: {downloaderror}."
126
  print(downloaderror)
127
+
128
  def translate(self):
129
  self.downloadbergamotfiles()
130
  try:
131
  model = self.service.modelFromConfigPath(f"{self.localfolder}/config.yml")
 
132
  options = bergamot.ResponseOptions(alignment=False, sentenceMappings=False, qualityScores=False, HTML=False)
133
  rawresponse = self.service.translate(model, bergamot.VectorString(self.input_text), options)
134
  response: list|str = [r.target.text for r in rawresponse] if len(rawresponse) > 1 else next(iter(rawresponse)).target.text
135
  print(type(self.input_text), len(self.input_text), len(rawresponse), type(response), response)
 
 
136
  message_text = f"Translated from {self.sl} to {self.tl} with {self.model_name}."
137
  except Exception as translateerror:
138
  response, message_text = str(translateerror), f"Error translating from {self.sl} to {self.tl} with {self.model_name}: {translateerror}."
 
140
  return {"input": self.input_text, "translated_text": response, "message_text": message_text}
141
 
142
  # 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
143
+ @app.get("/bergamott", operation_id="get_bergamott", description="Translate text with Bergamot", tags=["bergamott"], summary="Translate text with Bergamot")
144
  def bergamott(input_text: list[str] = Query(description="Input string or list of strings"), sl: str = 'de', tl: str = 'en', model_name: Optional[str] = 'base/deen'):
145
  """
146
  Translates the input text from the source language to the target language using a specified model.
 
167
  print(mainerror)
168
 
169
  # 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
170
+ @app.get("/bergamots", operation_id="get_bergamot", description="Translate text with Bergamot", tags=["bergamots"], summary="Translate text with Bergamot")
171
  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'):
172
  """
173
  Translates the input text from the source language to the target language using a specified model.
 
254
  describe_all_responses=True,
255
  describe_full_response_schema=True,
256
  include_operations=["get_translate", "get_paraphrase"],
257
+ include_tags=["paraphrase", "translate", "bergamott"]
258
  )
259
  # Mount the MCP server directly to the FASTAPI app
260
  mcp.mount()