File size: 2,675 Bytes
f02087b
 
 
 
 
 
 
028fef4
f02087b
e0ef94e
f02087b
 
 
 
 
 
 
 
 
 
 
3d50db6
f02087b
b9b1055
f02087b
 
 
 
 
e0ef94e
f02087b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
293b8ca
f02087b
 
293b8ca
f02087b
 
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 explain grammar mistakes. You are bilingual in English and Japanese"}]

MAX_TOKENS = 8192
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 explain grammar mistakes. You are bilingual in English and Japanese"}]

    user_message = {"role": "user", "content": f"Step 1: Correct the text I give. Be especially strict when it comes to sentence fragments. Step 2: Explain all the grammar and spelling mistakes for an EFL student. Put each explanation in a bullet point: [{user_input}]"}
    
    messages.append(user_message)
    
    while True:
        response = openai.ChatCompletion.create(
            model="gpt-3.5-turbo-16k",
            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 Grammar Checker", description=instructions)

demo.launch(share=False, debug=True)