Spaces:
Runtime error
Runtime error
Upload 2 files
Browse files- app.py +69 -0
- requirements.txt +1 -0
app.py
ADDED
@@ -0,0 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import openai
|
2 |
+
import os
|
3 |
+
import gradio as gr
|
4 |
+
|
5 |
+
openai.api_key = os.getenv("OPENAI_API_KEY")
|
6 |
+
|
7 |
+
class Conversation:
|
8 |
+
def __init__(self, prompt):
|
9 |
+
self.prompt = prompt
|
10 |
+
self.messages = []
|
11 |
+
self.messages.append({"role": "system", "content": self.prompt})
|
12 |
+
|
13 |
+
def ask(self, question):
|
14 |
+
try:
|
15 |
+
self.messages.append({"role": "user", "content": question})
|
16 |
+
response = openai.ChatCompletion.create(
|
17 |
+
model="gpt-3.5-turbo",
|
18 |
+
messages=self.messages,
|
19 |
+
temperature=0.5,
|
20 |
+
max_tokens=2048,
|
21 |
+
top_p=1,
|
22 |
+
)
|
23 |
+
except Exception as e:
|
24 |
+
print(e)
|
25 |
+
return e
|
26 |
+
|
27 |
+
message = response["choices"][0]["message"]["content"]
|
28 |
+
self.messages.append({"role": "assistant", "content": message})
|
29 |
+
|
30 |
+
return message
|
31 |
+
|
32 |
+
|
33 |
+
prompt = """你是一个英语老师,请批改英语作文。用中文回答,回答限制在100个字以内,你的回答按照以下格式:
|
34 |
+
1. 文章主要内容:
|
35 |
+
2. 语法错误:
|
36 |
+
3. 拼写错误:
|
37 |
+
4. 百分制评分:
|
38 |
+
5. 给出评语:
|
39 |
+
如果输入不是英文作文,则提醒输入英文作文"""
|
40 |
+
|
41 |
+
conv = Conversation(prompt)
|
42 |
+
|
43 |
+
def moderation(text):
|
44 |
+
response = openai.Moderation.create(
|
45 |
+
input=text
|
46 |
+
)
|
47 |
+
output = response["results"][0]['flagged']
|
48 |
+
return output
|
49 |
+
|
50 |
+
def answer(question):
|
51 |
+
#调用moderation函数,对输入的作文进行审核,如果有不良内容,返回提示信息
|
52 |
+
moderation_flag = moderation(question)
|
53 |
+
if moderation_flag == True:
|
54 |
+
response = "输入文字有不良用语,请重新输入"
|
55 |
+
else:
|
56 |
+
response = conv.ask(question)
|
57 |
+
return response
|
58 |
+
|
59 |
+
|
60 |
+
|
61 |
+
# 使用gradio画出修改英语作文的界面,左边是输入框和提交按钮,右边是输出评语框,并调用gpt进行作文修改
|
62 |
+
with gr.Interface(
|
63 |
+
answer,
|
64 |
+
gr.inputs.Textbox(lines=5,placeholder="输入英语作文",label="作文"),
|
65 |
+
gr.outputs.Textbox(label="评语"),
|
66 |
+
title="批改英语作文",
|
67 |
+
allow_flagging='never'
|
68 |
+
) as demo:
|
69 |
+
demo.launch(share=True)
|
requirements.txt
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
openai
|