IS361Group4 commited on
Commit
47b84e2
·
verified ·
1 Parent(s): fb51035

Update translator.py

Browse files
Files changed (1) hide show
  1. translator.py +20 -18
translator.py CHANGED
@@ -15,24 +15,26 @@ class TextTranslator(BaseModel):
15
  output_parser = PydanticOutputParser(pydantic_object=TextTranslator)
16
  format_instructions = output_parser.get_format_instructions()
17
 
18
- def text_translator(input_text : str, language : str) -> str:
19
- human_template = """Enter the text that you want to translate:
20
- {input_text}, and enter the language that you want it to translate to {language}. {format_instructions}"""
21
- human_message_prompt = HumanMessagePromptTemplate.from_template(human_template)
22
-
23
- chat_prompt = ChatPromptTemplate.from_messages([human_message_prompt])
24
-
25
- prompt = chat_prompt.format_prompt(input_text = input_text, language = language, format_instructions = format_instructions)
26
-
27
- messages = prompt.to_messages()
28
-
29
- response = chat(messages = messages)
30
-
31
- output = output_parser.parse(response.content)
32
-
33
- output_text = output.output
34
-
35
- return output_text
 
 
36
 
37
  def text_translator_ui():
38
  with gr.Column() as translator_ui:
 
15
  output_parser = PydanticOutputParser(pydantic_object=TextTranslator)
16
  format_instructions = output_parser.get_format_instructions()
17
 
18
+ def text_translator(input_text: str, language: str) -> str:
19
+ try:
20
+ human_template = """Enter the text that you want to translate:
21
+ {input_text}, and enter the language that you want it to translate to {language}. {format_instructions}"""
22
+ human_message_prompt = HumanMessagePromptTemplate.from_template(human_template)
23
+
24
+ chat_prompt = ChatPromptTemplate.from_messages([human_message_prompt])
25
+ prompt = chat_prompt.format_prompt(
26
+ input_text=input_text,
27
+ language=language,
28
+ format_instructions=format_instructions
29
+ )
30
+ messages = prompt.to_messages()
31
+
32
+ response = chat(messages=messages)
33
+ output = output_parser.parse(response.content)
34
+ return output.output
35
+ except Exception as e:
36
+ return f"❌ Error: {str(e)}"
37
+
38
 
39
  def text_translator_ui():
40
  with gr.Column() as translator_ui: