File size: 2,751 Bytes
ede2531
4a9ccf4
 
 
 
 
 
 
 
 
 
 
575890c
48f8992
811137d
 
 
 
 
 
08bd3d6
 
 
4e34e58
104d8c9
48f8992
104d8c9
48f8992
5313308
 
 
104d8c9
48f8992
5313308
fe29d0b
 
48f8992
5313308
104d8c9
fe29d0b
104d8c9
4a9ccf4
 
3473f5a
4a9ccf4
0bd1db7
5313308
fe29d0b
5313308
6baffb8
ea4c189
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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)