jvictoria commited on
Commit
1816373
1 Parent(s): 5be3748

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -22
app.py CHANGED
@@ -1,37 +1,51 @@
1
  import openai
2
  import gradio
3
  import os
 
4
 
5
  openai.api_key = os.environ.get("API_TOKEN")
6
 
7
- messages = [{"role": "system", "content": "You are an education expert who can explain everything, even complicated subjects, so well that a child can understand"}]
8
 
9
- def CheckAssist(user_input):
10
- messages.append({"role": "user", "content": user_input})
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
 
12
- # Save the user question to a file
13
- with open("user_questions.txt", "a") as f:
14
- f.write(user_input + "\n")
15
-
16
- response = openai.ChatCompletion.create(
17
- model="gpt-3.5-turbo",
18
- messages=messages[-5:], # Change this line
19
- )
 
 
20
  ChatGPT_reply = response["choices"][0]["message"]["content"]
21
  messages.append({"role": "assistant", "content": ChatGPT_reply})
22
 
23
- # Keep only the last five messages in the list
24
- if len(messages) > 10: # Change this line
25
- messages.pop(0)
26
- messages.pop(0)
27
-
28
  return ChatGPT_reply
29
 
30
- demo = gradio.Interface(
31
- fn=CheckAssist,
32
- inputs=gradio.inputs.Textbox(label="Your Question"),
33
- outputs=gradio.outputs.Textbox(label="Assistant's Answer"),
34
- title="Teacher Jihan's Teaching Assistant"
35
- )
 
36
 
37
  demo.launch(share=False, debug=True)
 
1
  import openai
2
  import gradio
3
  import os
4
+ import re
5
 
6
  openai.api_key = os.environ.get("API_TOKEN")
7
 
8
+ messages = [{"role": "system", "content": "You are an education expert who can correct essays. You are bilingual in English and Japanese"}]
9
 
10
+ MAX_TOKENS = 4096
11
+ MAX_HISTORY = 1
12
+
13
+ def CustomChatGPT(user_input):
14
+ global messages
15
+ essay_keywords = ["essay", "エッセイ", "論文"]
16
+ action_keywords = ["write", "make", "create", "生成", "作成", "書く"]
17
+
18
+ if any(re.search(f"{action_kw}.*{essay_kw}", user_input.lower()) for action_kw in action_keywords for essay_kw in essay_keywords):
19
+ return "I'm sorry, I cannot write an essay for you."
20
+
21
+ # Clear the messages list before adding new messages
22
+ messages = [{"role": "system", "content": "You are an education expert who can correct essays. You are bilingual in English and Japanese"}]
23
+
24
+ user_message = {"role": "user", "content": f"Step 1, find make corrections on the essay's logic and reasoning. You do not need to worry about grammatical mistakes. Step 2, show the corrected version. Step 3, Explain in detail all the errors from what I originally wrote. Put each explanation in a bullet point: [{user_input}]"}
25
+
26
+ messages.append(user_message)
27
 
28
+ while True:
29
+ response = openai.ChatCompletion.create(
30
+ model="gpt-3.5-turbo",
31
+ messages=messages
32
+ )
33
+
34
+ total_tokens = response['usage']['total_tokens']
35
+ if total_tokens < MAX_TOKENS:
36
+ break
37
+
38
  ChatGPT_reply = response["choices"][0]["message"]["content"]
39
  messages.append({"role": "assistant", "content": ChatGPT_reply})
40
 
 
 
 
 
 
41
  return ChatGPT_reply
42
 
43
+ # Add text instructions on top of the input and output boxes
44
+ input_text = "ここに訂正してほしい英語の作文を置いてください。そして「Submit」を押してください:"
45
+ output_text = "訂正と説明はここに表示されます:"
46
+ instructions = "このアプリケーションは、文法と綴りをチェックするために使用できます。アプリは、修正した作文を提供し、それに続いて修正の説明を示します。アプリは、1つのパラグラフずつ入力する場合に最適に機能します。例えば、3つのパラグラフから成る作文をチェックしたい場合は、それぞれのパラグラフを「Submit」してください。つまり、プログラムを3回実行し、各パラグラフごとに1回ずつ実行してください。"
47
+
48
+ # Modify the Gradio interface to include the text instructions and image
49
+ demo = gradio.Interface(fn=CustomChatGPT, inputs=gradio.inputs.Textbox(lines=5, label=input_text), outputs=gradio.outputs.Textbox(label=output_text), title="Teacher Jihan's Checking Assistant", description=instructions)
50
 
51
  demo.launch(share=False, debug=True)