EssayChecker / app.py
jvictoria's picture
Update app.py
0bd1db7
import openai
import gradio
import os
import re
openai.api_key = os.environ.get("API_TOKEN")
messages = [{"role": "system", "content": "You are an education expert who can correct essays. You are bilingual in English and Japanese"}]
MAX_TOKENS = 4096
MAX_HISTORY = 1
def CustomChatGPT(user_input):
global messages
essay_keywords = ["essay", "エッセイ", "論文"]
action_keywords = ["write", "make", "create", "生成", "作成", "書く"]
if any(re.search(f"{action_kw}.*{essay_kw}", user_input.lower()) for action_kw in action_keywords for essay_kw in essay_keywords):
return "I'm sorry, I cannot write an essay for you."
# Clear the messages list before adding new messages
messages = [{"role": "system", "content": "You are an education expert who can correct essays. You are bilingual in English and Japanese"}]
user_message = {"role": "user", "content": f"Step 1, find make grammar and spelling and corrections. 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}]"}
messages.append(user_message)
while True:
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=messages
)
total_tokens = response['usage']['total_tokens']
if total_tokens < MAX_TOKENS:
break
ChatGPT_reply = response["choices"][0]["message"]["content"]
messages.append({"role": "assistant", "content": ChatGPT_reply})
return ChatGPT_reply
# Add text instructions on top of the input and output boxes
input_text = "ここに訂正してほしい英語の作文を置いてください。そして「Submit」を押してください:"
output_text = "訂正と説明はここに表示されます:"
instructions = "このアプリケーションは、文法と綴りをチェックするために使用できます。アプリは、修正した作文を提供し、それに続いて修正の説明を示します。アプリは、1つのパラグラフずつ入力する場合に最適に機能します。例えば、3つのパラグラフから成る作文をチェックしたい場合は、それぞれのパラグラフを「Submit」してください。つまり、プログラムを3回実行し、各パラグラフごとに1回ずつ実行してください。"
# Modify the Gradio interface to include the text instructions and image
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)
demo.launch(share=False, debug=True)