Rawiwan1912 commited on
Commit
0bcdb5f
·
verified ·
1 Parent(s): 830f999

Update modules/translator.py

Browse files
Files changed (1) hide show
  1. modules/translator.py +14 -25
modules/translator.py CHANGED
@@ -1,7 +1,6 @@
1
  import os
2
  import gradio as gr
3
-
4
- from 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
@@ -15,18 +14,18 @@ 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
 
@@ -34,23 +33,13 @@ def text_translator(input_text : str, language : str) -> str:
34
 
35
  return output_text
36
 
37
- # Interface
38
- def translator_interface():
39
- with gr.Blocks() as demo:
40
- gr.HTML("<h1 align = 'center'> Text Translator </h1>")
41
- gr.HTML("<h4 align = 'center'> Translate to any language </h4>")
42
-
43
- with gr.Row():
44
- input_text = gr.Textbox(label="Enter the text that you want to translate")
45
- language = gr.Textbox(label="Enter the language that you want it to translate to", placeholder="Example: Hindi, French, Bengali, etc.")
46
-
47
- generate_btn = gr.Button(value='Generate')
48
- output_text = gr.Textbox(label="Translated text")
49
-
50
- generate_btn.click(fn=text_translator, inputs=[input_text, language], outputs=output_text)
51
-
52
- return demo
53
-
54
- # สำหรับแสดง Interface ใน app.py
55
- def get_translator_tab():
56
- return translator_interface()
 
1
  import os
2
  import gradio as gr
3
+ from langchain_core.pydantic_v1 import BaseModel, Field
 
4
  from langchain.prompts import HumanMessagePromptTemplate, ChatPromptTemplate
5
  from langchain.output_parsers import PydanticOutputParser
6
  from langchain_openai import ChatOpenAI
 
14
  output_parser = PydanticOutputParser(pydantic_object=TextTranslator)
15
  format_instructions = output_parser.get_format_instructions()
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}. {format_instructions}"""
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
 
 
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)