Spaces:
Sleeping
Sleeping
Update translator.py
Browse files- 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
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
|
|
|
|
|
|
| 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:
|