leadingbridge commited on
Commit
012a8ca
1 Parent(s): f6650b3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +51 -45
app.py CHANGED
@@ -19,62 +19,70 @@ def sentiment_analysis(text):
19
  # Open AI Chatbot Model
20
  openai.api_key = os.environ['openai_api']
21
 
22
- start_sequence = "\nAI:"
23
- restart_sequence = "\nHuman: "
 
 
 
 
 
 
 
 
 
 
 
 
 
24
 
25
- prompt = "You can discuss any topic with the Chinese Chatbot assistant by typing Chinese in here"
26
 
27
- def openai_create(prompt):
28
 
29
- response = openai.Completion.create(
30
- model="text-davinci-003",
31
- prompt=prompt,
32
- temperature=0.9,
33
- max_tokens=2048,
 
 
 
34
  top_p=1,
35
  frequency_penalty=0,
36
- presence_penalty=0.6,
37
- stop=[" Human:", " AI:"]
38
  )
39
 
40
- return response.choices[0].text
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41
 
42
  def chatgpt_clone(input, history):
43
  history = history or []
44
  s = list(sum(history, ()))
45
  s.append(input)
46
  inp = ' '.join(s)
47
- output = openai_create(inp)
48
  history.append((input, output))
49
  return history, history
50
 
51
-
52
- # Open AI Chinese Translation Model
53
- def translate_to_chinese(text_to_translate):
54
- response = openai.Completion.create(
55
- model="text-davinci-003",
56
- prompt=f"Translate this short English sentence into Chinese:\n\n{text_to_translate}\n\n1.",
57
- temperature=0.3,
58
- max_tokens=2048,
59
- top_p=1.0,
60
- frequency_penalty=0.0,
61
- presence_penalty=0.0
62
- )
63
- return response.choices[0].text.strip()
64
-
65
- # Open AI English Translation Model
66
- def translate_to_english(text_to_translate):
67
- response = openai.Completion.create(
68
- model="text-davinci-003",
69
- prompt=f"Translate this short Chinese sentence into English:\n\n{text_to_translate}\n\n1.",
70
- temperature=0.3,
71
- max_tokens=2048,
72
- top_p=1.0,
73
- frequency_penalty=0.0,
74
- presence_penalty=0.0
75
- )
76
- return response.choices[0].text.strip()
77
-
78
 
79
  # Gradio Output Model
80
  with gr.Blocks() as demo:
@@ -82,7 +90,7 @@ with gr.Blocks() as demo:
82
  with gr.Tab("🗣️Chatbot"):
83
  gr.Markdown("This is a Chinese chatbot powered by the OpenAI language model. Enter your message below in Chinese and the chatbot will respond.")
84
  chatbot = gr.Chatbot()
85
- message = gr.Textbox(placeholder=prompt)
86
  state = gr.State()
87
  submit = gr.Button("Send")
88
  submit.click(chatgpt_clone, inputs=[message, state], outputs=[chatbot, state])
@@ -97,18 +105,16 @@ with gr.Blocks() as demo:
97
  inputs = gr.Textbox(placeholder="Enter a short English sentence to translate to Chinese here.")
98
  outputs = gr.Textbox(label="Translation Result")
99
  proceed_button = gr.Button("Translate")
100
- proceed_button.click(fn=translate_to_chinese, inputs=inputs, outputs=outputs)
101
  with gr.Tab("🔤English Translation"):
102
  gr.Markdown("This model translate a Chinese sentence to English using the OpenAI engine. Enter a Chinese short sentence in the input box and click the 'Translate' button to get the translation result in English.")
103
  inputs = gr.Textbox(placeholder="Enter a short Chinese sentence to translate to English here.")
104
  outputs = gr.Textbox(label="Translation Result")
105
  proceed_button = gr.Button("Translate")
106
- proceed_button.click(fn=translate_to_english, inputs=inputs, outputs=outputs)
107
  gr.Markdown('''
108
  We are happy to share with you some Chinese language models that we've made using NLP. When we looked online, we noticed that there weren't many resources available for Chinese NLP, so we hope that our models can be useful to you.
109
-
110
  We want to mention that these models aren't perfect and there is still room for improvement. Because of limited resources, there might be some mistakes or limitations in the models.
111
-
112
  However, We hope that you find them helpful and that you can help make them even better.
113
  ''')
114
 
 
19
  # Open AI Chatbot Model
20
  openai.api_key = os.environ['openai_api']
21
 
22
+ def openai_chatbot(prompt):
23
+
24
+ response = openai.ChatCompletion.create(
25
+ model="gpt-3.5-turbo",
26
+ messages=[
27
+ {"role":"system","content":"You are a general chatbot that can answer anything"},
28
+ {"role":"user","content":prompt}
29
+ ],
30
+ temperature=0.8,
31
+ max_tokens=5000,
32
+ top_p=1,
33
+ frequency_penalty=0,
34
+ presence_penalty=0.6
35
+
36
+ )
37
 
38
+ return response.choices[0].message.content
39
 
40
+ def openai_translation_ec(prompt):
41
 
42
+ response = openai.ChatCompletion.create(
43
+ model="gpt-3.5-turbo",
44
+ messages=[
45
+ {"role":"system","content":"You are a sentense translator which translate article from English to Chinese. Don't do any task other than translation"},
46
+ {"role":"user","content":prompt}
47
+ ],
48
+ temperature=0.8,
49
+ max_tokens=5000,
50
  top_p=1,
51
  frequency_penalty=0,
52
+ presence_penalty=0.6
53
+
54
  )
55
 
56
+ return response.choices[0].message.content
57
+
58
+ def openai_translation_ce(prompt):
59
+
60
+ response = openai.ChatCompletion.create(
61
+ model="gpt-3.5-turbo",
62
+ messages=[
63
+ {"role":"system","content":"You are a sentense translator which translate article from Chinese to English. Don't do any task other than translation"},
64
+ {"role":"user","content":prompt}
65
+ ],
66
+ temperature=0.8,
67
+ max_tokens=5000,
68
+ top_p=1,
69
+ frequency_penalty=0,
70
+ presence_penalty=0.6
71
+
72
+ )
73
+
74
+ return response.choices[0].message.content
75
 
76
  def chatgpt_clone(input, history):
77
  history = history or []
78
  s = list(sum(history, ()))
79
  s.append(input)
80
  inp = ' '.join(s)
81
+ output = openai_chatbot(inp)
82
  history.append((input, output))
83
  return history, history
84
 
85
+ """# **Gradio Model**"""
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
86
 
87
  # Gradio Output Model
88
  with gr.Blocks() as demo:
 
90
  with gr.Tab("🗣️Chatbot"):
91
  gr.Markdown("This is a Chinese chatbot powered by the OpenAI language model. Enter your message below in Chinese and the chatbot will respond.")
92
  chatbot = gr.Chatbot()
93
+ message = gr.Textbox(placeholder="You can discuss any topic with the Chinese Chatbot assistant by typing Chinese in here")
94
  state = gr.State()
95
  submit = gr.Button("Send")
96
  submit.click(chatgpt_clone, inputs=[message, state], outputs=[chatbot, state])
 
105
  inputs = gr.Textbox(placeholder="Enter a short English sentence to translate to Chinese here.")
106
  outputs = gr.Textbox(label="Translation Result")
107
  proceed_button = gr.Button("Translate")
108
+ proceed_button.click(fn=openai_translation_ec, inputs=inputs, outputs=outputs)
109
  with gr.Tab("🔤English Translation"):
110
  gr.Markdown("This model translate a Chinese sentence to English using the OpenAI engine. Enter a Chinese short sentence in the input box and click the 'Translate' button to get the translation result in English.")
111
  inputs = gr.Textbox(placeholder="Enter a short Chinese sentence to translate to English here.")
112
  outputs = gr.Textbox(label="Translation Result")
113
  proceed_button = gr.Button("Translate")
114
+ proceed_button.click(fn=openai_translation_ce, inputs=inputs, outputs=outputs)
115
  gr.Markdown('''
116
  We are happy to share with you some Chinese language models that we've made using NLP. When we looked online, we noticed that there weren't many resources available for Chinese NLP, so we hope that our models can be useful to you.
 
117
  We want to mention that these models aren't perfect and there is still room for improvement. Because of limited resources, there might be some mistakes or limitations in the models.
 
118
  However, We hope that you find them helpful and that you can help make them even better.
119
  ''')
120