Spaces:
Running
Running
Update translator.py
Browse files- translator.py +30 -16
translator.py
CHANGED
@@ -1,35 +1,49 @@
|
|
|
|
1 |
import gradio as gr
|
2 |
-
|
|
|
3 |
from langchain.prompts import HumanMessagePromptTemplate, ChatPromptTemplate
|
4 |
from langchain.output_parsers import PydanticOutputParser
|
5 |
from langchain_openai import ChatOpenAI
|
6 |
|
7 |
chat = ChatOpenAI()
|
8 |
|
9 |
-
# Define the Pydantic Model
|
10 |
class TextTranslator(BaseModel):
|
11 |
output: str = Field(description="Python string containing the output text translated in the desired language")
|
12 |
-
|
13 |
-
# Use PydanticOutputParser (no need for response_schemas)
|
14 |
output_parser = PydanticOutputParser(pydantic_object=TextTranslator)
|
|
|
15 |
|
16 |
-
def text_translator(input_text: str, language: str) -> str:
|
17 |
human_template = """Enter the text that you want to translate:
|
18 |
-
{input_text}, and enter the language that you want it to translate to {language}."""
|
19 |
human_message_prompt = HumanMessagePromptTemplate.from_template(human_template)
|
|
|
20 |
chat_prompt = ChatPromptTemplate.from_messages([human_message_prompt])
|
21 |
-
|
|
|
|
|
22 |
messages = prompt.to_messages()
|
23 |
-
response = chat(messages=messages)
|
24 |
|
25 |
-
|
|
|
26 |
output = output_parser.parse(response.content)
|
27 |
-
|
|
|
|
|
|
|
28 |
|
29 |
def text_translator_ui():
|
30 |
-
gr.
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
import gradio as gr
|
3 |
+
|
4 |
+
from langchain_core.pydantic_v1 import BaseModel, Field
|
5 |
from langchain.prompts import HumanMessagePromptTemplate, ChatPromptTemplate
|
6 |
from langchain.output_parsers import PydanticOutputParser
|
7 |
from langchain_openai import ChatOpenAI
|
8 |
|
9 |
chat = ChatOpenAI()
|
10 |
|
11 |
+
# Define the Pydantic Model
|
12 |
class TextTranslator(BaseModel):
|
13 |
output: str = Field(description="Python string containing the output text translated in the desired language")
|
14 |
+
|
|
|
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:
|
39 |
+
gr.HTML("<h1 align='center'>Text Translator</h1>")
|
40 |
+
gr.HTML("<h4 align='center'>Translate to any language</h4>")
|
41 |
+
|
42 |
+
input_text = gr.Textbox(label="Enter the text that you want to translate")
|
43 |
+
target_lang = gr.Textbox(label="Enter the language that you want it to translate to", placeholder="Example: Hindi, French, Bengali, etc")
|
44 |
+
generate_btn = gr.Button(value='Generate')
|
45 |
+
output_text = gr.Textbox(label="Translated text")
|
46 |
+
|
47 |
+
generate_btn.click(fn=text_translator, inputs=[input_text, target_lang], outputs=[output_text])
|
48 |
+
|
49 |
+
return translator_ui
|