Spaces:
Sleeping
Sleeping
Update modules/translator.py
Browse files- modules/translator.py +11 -20
modules/translator.py
CHANGED
|
@@ -1,45 +1,36 @@
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
-
from pydantic
|
| 4 |
from langchain.prompts import HumanMessagePromptTemplate, ChatPromptTemplate
|
| 5 |
-
from langchain.output_parsers import
|
| 6 |
from langchain_openai import ChatOpenAI
|
| 7 |
|
| 8 |
chat = ChatOpenAI()
|
| 9 |
|
| 10 |
-
# Define the Pydantic Model
|
| 11 |
class TextTranslator(BaseModel):
|
| 12 |
output: str = Field(description="Python string containing the output text translated in the desired language")
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
|
| 17 |
def text_translator(input_text: str, language: str) -> str:
|
| 18 |
human_template = """Enter the text that you want to translate:
|
| 19 |
-
{input_text}, and enter the language that you want it to translate to {language}.
|
| 20 |
human_message_prompt = HumanMessagePromptTemplate.from_template(human_template)
|
| 21 |
-
|
| 22 |
chat_prompt = ChatPromptTemplate.from_messages([human_message_prompt])
|
| 23 |
-
|
| 24 |
-
prompt = chat_prompt.format_prompt(input_text=input_text, language=language, format_instructions=format_instructions)
|
| 25 |
-
|
| 26 |
messages = prompt.to_messages()
|
| 27 |
-
|
| 28 |
response = chat(messages=messages)
|
| 29 |
|
|
|
|
| 30 |
output = output_parser.parse(response.content)
|
| 31 |
-
|
| 32 |
-
output_text = output.output
|
| 33 |
-
|
| 34 |
-
return output_text
|
| 35 |
|
| 36 |
-
# แยก UI เป็นฟังก์ชัน เพื่อนำไปใส่ใน Gradio Tab ได้
|
| 37 |
def text_translator_ui():
|
| 38 |
gr.Markdown("### Text Translator\nTranslate text into any language using AI.")
|
| 39 |
-
|
| 40 |
input_text = gr.Textbox(label="Enter the text that you want to translate")
|
| 41 |
input_lang = gr.Textbox(label="Enter the language that you want it to translate to", placeholder="Example: Hindi, French, Bengali, etc.")
|
| 42 |
output_text = gr.Textbox(label="Translated text")
|
| 43 |
-
|
| 44 |
translate_button = gr.Button("Translate")
|
| 45 |
-
translate_button.click(fn=text_translator, inputs=[input_text, input_lang], outputs=output_text)
|
|
|
|
| 1 |
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 StructuredOutputParser
|
| 6 |
from langchain_openai import ChatOpenAI
|
| 7 |
|
| 8 |
chat = ChatOpenAI()
|
| 9 |
|
| 10 |
+
# Define the Pydantic Model (updated for Pydantic v2)
|
| 11 |
class TextTranslator(BaseModel):
|
| 12 |
output: str = Field(description="Python string containing the output text translated in the desired language")
|
| 13 |
+
|
| 14 |
+
# Updated: Use StructuredOutputParser with Pydantic v2
|
| 15 |
+
output_parser = StructuredOutputParser(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:
|
| 19 |
+
{input_text}, and enter the language that you want it to translate to {language}."""
|
| 20 |
human_message_prompt = HumanMessagePromptTemplate.from_template(human_template)
|
|
|
|
| 21 |
chat_prompt = ChatPromptTemplate.from_messages([human_message_prompt])
|
| 22 |
+
prompt = chat_prompt.format_prompt(input_text=input_text, language=language)
|
|
|
|
|
|
|
| 23 |
messages = prompt.to_messages()
|
|
|
|
| 24 |
response = chat(messages=messages)
|
| 25 |
|
| 26 |
+
# Use model_dump() for Pydantic v2
|
| 27 |
output = output_parser.parse(response.content)
|
| 28 |
+
return output.output
|
|
|
|
|
|
|
|
|
|
| 29 |
|
|
|
|
| 30 |
def text_translator_ui():
|
| 31 |
gr.Markdown("### Text Translator\nTranslate text into any language using AI.")
|
|
|
|
| 32 |
input_text = gr.Textbox(label="Enter the text that you want to translate")
|
| 33 |
input_lang = gr.Textbox(label="Enter the language that you want it to translate to", placeholder="Example: Hindi, French, Bengali, etc.")
|
| 34 |
output_text = gr.Textbox(label="Translated text")
|
|
|
|
| 35 |
translate_button = gr.Button("Translate")
|
| 36 |
+
translate_button.click(fn=text_translator, inputs=[input_text, input_lang], outputs=output_text)
|