Spico commited on
Commit
9ac5adf
·
1 Parent(s): 885d7a0

add translation

Browse files
Files changed (2) hide show
  1. README.md +5 -4
  2. app.py +24 -11
README.md CHANGED
@@ -28,7 +28,8 @@ $ python app.py
28
  ## ⚔️🥊Abilities
29
 
30
  - Completion (文本补全): Let the model complete a story, or any texts
31
- - Correction(文本纠错): Correcting grammar errors
32
- - Polishing(文本润色): Polishing texts
33
- - Paraphrase(文本改写): Text rewriting
34
- - Freestyle(直接调用ChatGPT): This will call raw ChatGPT API without leading instruction prefixes, so you may want to use it as you've done on [the official ChatGPT website](https://chat.openai.com/)
 
 
28
  ## ⚔️🥊Abilities
29
 
30
  - Completion (文本补全): Let the model complete a story, or any texts
31
+ - Correction (文本纠错): Correcting grammar errors
32
+ - Polishing (文本润色): Polishing texts
33
+ - Paraphrase (文本改写): Text rewriting
34
+ - Translation (机器翻译,需要提供目标语言): Text translation to target language
35
+ - Freestyle (直接调用ChatGPT): This will call raw ChatGPT API without leading instruction prefixes, so you may want to use it as you've done on [the official ChatGPT website](https://chat.openai.com/)
app.py CHANGED
@@ -7,13 +7,14 @@ instructions = {
7
  "correction": "Please help me correct mistakes in the text",
8
  "polishing": "Please help me polish the language and improve my writing",
9
  "paraphrase": "Please help me paraphrase the text",
 
10
  "freestyle": "",
11
  }
12
 
13
  template = "{instruction}:\n\nText: {text}"
14
 
15
 
16
- def chat(task_type: str, text: str, api_key: str) -> str:
17
  openai.api_key = api_key
18
 
19
  prompt = ""
@@ -21,12 +22,18 @@ def chat(task_type: str, text: str, api_key: str) -> str:
21
  if task_type == "freestyle":
22
  prompt = text
23
  else:
24
- prompt = template.format(instruction=instructions[task_type], text=text)
 
 
 
 
 
 
25
 
26
  messages = [
27
  {
28
  "role": "system",
29
- "content": "You are a helpful writing assistant to help correct grammar mistakes and polish or paraphrase texts.",
30
  },
31
  {"role": "user", "content": prompt},
32
  ]
@@ -59,19 +66,25 @@ with gr.Blocks(css="") as demo:
59
  )
60
 
61
  with gr.Row():
62
- api_key = gr.Textbox(label='OpenAI API Key')
63
 
64
- with gr.Row():
65
- emojis = "🪄🥊💎🫧🎤"
66
- task_type = gr.Radio([f"{emojis[i]}{k.title()}" for i, k in enumerate(instructions.keys())], label="Task")
67
- text_button = gr.Button("Can~ do!")
 
 
 
 
68
 
69
  with gr.Row():
70
- text_input = gr.TextArea(lines=15, label="Input")
71
- text_output = gr.TextArea(lines=15, label="Output")
 
 
72
 
73
  text_button.click(
74
- chat, inputs=[task_type, text_input, api_key], outputs=text_output
75
  )
76
 
77
  demo.launch()
 
7
  "correction": "Please help me correct mistakes in the text",
8
  "polishing": "Please help me polish the language and improve my writing",
9
  "paraphrase": "Please help me paraphrase the text",
10
+ "translation": "Please help me translate the text",
11
  "freestyle": "",
12
  }
13
 
14
  template = "{instruction}:\n\nText: {text}"
15
 
16
 
17
+ def chat(task_type: str, text: str, api_key: str, tgt_lang: str = "") -> str:
18
  openai.api_key = api_key
19
 
20
  prompt = ""
 
22
  if task_type == "freestyle":
23
  prompt = text
24
  else:
25
+ instruction = instructions[task_type]
26
+ if task_type == "translation":
27
+ if tgt_lang:
28
+ instruction += f" into {tgt_lang.strip()}"
29
+ else:
30
+ raise ValueError("Target language cannot be empty when translating")
31
+ prompt = template.format(instruction=instruction, text=text)
32
 
33
  messages = [
34
  {
35
  "role": "system",
36
+ "content": "You are a helpful writing assistant to help correct grammar mistakes, polish and paraphrase texts.",
37
  },
38
  {"role": "user", "content": prompt},
39
  ]
 
66
  )
67
 
68
  with gr.Row():
69
+ api_key = gr.Textbox(label='OpenAI API Key', type="password")
70
 
71
+ with gr.Row().style(equal_height=True):
72
+ with gr.Column(scale=3):
73
+ emojis = "🪄🥊💎🫧🚌🎤"
74
+ task_type = gr.Radio([f"{emojis[i]}{k.title()}" for i, k in enumerate(instructions.keys())], label="Task")
75
+ with gr.Column(min_width=100):
76
+ tgt_lang = gr.Textbox(label="Target language in translation")
77
+ with gr.Column():
78
+ text_button = gr.Button("Can~ do!", variant="primary")
79
 
80
  with gr.Row():
81
+ with gr.Column():
82
+ text_input = gr.TextArea(lines=15, label="Input")
83
+ with gr.Column():
84
+ text_output = gr.TextArea(lines=15, label="Output")
85
 
86
  text_button.click(
87
+ chat, inputs=[task_type, text_input, api_key, tgt_lang], outputs=text_output
88
  )
89
 
90
  demo.launch()