Spaces:
Sleeping
Sleeping
import openai | |
import gradio as gr | |
import os | |
openai.api_key = os.environ.get("API_TOKEN") | |
def analyze_prompt(prompt, user_answer, points): | |
response = openai.ChatCompletion.create( | |
model="gpt-3.5-turbo", | |
messages=[ | |
{"role": "system", "content": "You are an academic writing teacher, correct grammar and spelling, and provide recommendations on how to improve the essay."}, | |
{"role": "user", "content": f"Analyze this writing prompt: {prompt}"}, | |
{"role": "user", "content": f"User's answer: {user_answer}"} | |
] | |
) | |
ChatGPT_reply = response["choices"][0]["message"]["content"] | |
return ChatGPT_reply | |
def correct_and_recommend(user_answer, points): | |
response = openai.ChatCompletion.create( | |
model="gpt-3.5-turbo", | |
messages=[ | |
{"role": "system", "content": "You are an academic writing teacher. Follow this rubric: 1) Give three reasons to support the answer. 2) It should follow this structure: 1 Introductory Paragraph, 3 Body Paragraphs, and 1 Concluding Paragraph. 3) The length should be 200-240 words. 4) The introductory paragraph should include a status quo and relate it to the author's thesis statement. 5) The paragraphs should make use of complex sentences. 6) The paragraphs should make use of discourse markers."}, | |
{"role": "user", "content": f"Correct the grammar and spelling of this answer: {user_answer}"}, | |
{"role": "user", "content": "Give recommendations on how to improve this answer according to the rubric, and provide specific examples from the essay for each recommendation. Make grammatical, spelling, punctuation, and spelling corrections where necessary."} | |
] | |
) | |
ChatGPT_reply = response["choices"][0]["message"]["content"] | |
return ChatGPT_reply | |
def process_input(prompt, user_answer, points): | |
analysis = analyze_prompt(prompt, user_answer, points) | |
corrections_and_recommendations = correct_and_recommend(user_answer, points) | |
return f"Prompt Analysis:\n{analysis}\n\nCorrections & Recommendations:\n{corrections_and_recommendations}" | |
demo = gr.Interface( | |
fn=process_input, | |
inputs=[ | |
gr.Textbox(lines=3, label="Writing Prompt"), | |
gr.Textbox(lines=5, label="Your Answer") | |
], | |
outputs=gr.Textbox(label="Analysis & Recommendations"), | |
title="Teacher Jihan's 英検1級 Essay Checker" | |
) | |
demo.launch(share=False, debug=True) |