Spaces:
Sleeping
Sleeping
Update modules/translator.py
Browse files- modules/translator.py +4 -4
modules/translator.py
CHANGED
|
@@ -2,7 +2,7 @@ import os
|
|
| 2 |
import gradio as gr
|
| 3 |
from pydantic import BaseModel, Field
|
| 4 |
from langchain.prompts import HumanMessagePromptTemplate, ChatPromptTemplate
|
| 5 |
-
from langchain.output_parsers import
|
| 6 |
from langchain_openai import ChatOpenAI
|
| 7 |
|
| 8 |
chat = ChatOpenAI()
|
|
@@ -11,8 +11,8 @@ chat = ChatOpenAI()
|
|
| 11 |
class TextTranslator(BaseModel):
|
| 12 |
output: str = Field(description="Python string containing the output text translated in the desired language")
|
| 13 |
|
| 14 |
-
#
|
| 15 |
-
output_parser =
|
| 16 |
|
| 17 |
def text_translator(input_text: str, language: str) -> str:
|
| 18 |
human_template = """Enter the text that you want to translate:
|
|
@@ -23,7 +23,7 @@ def text_translator(input_text: str, language: str) -> str:
|
|
| 23 |
messages = prompt.to_messages()
|
| 24 |
response = chat(messages=messages)
|
| 25 |
|
| 26 |
-
# Use
|
| 27 |
output = output_parser.parse(response.content)
|
| 28 |
return output.output
|
| 29 |
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
from pydantic import BaseModel, Field
|
| 4 |
from langchain.prompts import HumanMessagePromptTemplate, ChatPromptTemplate
|
| 5 |
+
from langchain.output_parsers import PydanticOutputParser
|
| 6 |
from langchain_openai import ChatOpenAI
|
| 7 |
|
| 8 |
chat = ChatOpenAI()
|
|
|
|
| 11 |
class TextTranslator(BaseModel):
|
| 12 |
output: str = Field(description="Python string containing the output text translated in the desired language")
|
| 13 |
|
| 14 |
+
# Use PydanticOutputParser (no need for response_schemas)
|
| 15 |
+
output_parser = PydanticOutputParser(pydantic_object=TextTranslator)
|
| 16 |
|
| 17 |
def text_translator(input_text: str, language: str) -> str:
|
| 18 |
human_template = """Enter the text that you want to translate:
|
|
|
|
| 23 |
messages = prompt.to_messages()
|
| 24 |
response = chat(messages=messages)
|
| 25 |
|
| 26 |
+
# Use output_parser to parse the response
|
| 27 |
output = output_parser.parse(response.content)
|
| 28 |
return output.output
|
| 29 |
|