Spaces:
Runtime error
Runtime error
Update app.py
Browse files
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
|
8 |
|
9 |
-
|
10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
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 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
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)
|