AlphaGrammar / app.py
jvictoria's picture
Update app.py
3302bbd
import openai
import gradio
import os
import re
openai.api_key = os.environ.get("API_TOKEN")
messages = [{"role": "system", "content": "You are an academic english writing expert who can explain grammar mistakes."}]
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 academic english writing expert who can explain grammar mistakes."}]
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 = "Write the text you want to check here."
output_text = "This is the corrected version"
instructions = "This app will correct and explain grammar mistakes."
# 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)