xuyingliKepler commited on
Commit
06b4380
1 Parent(s): f316f56

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +341 -0
app.py ADDED
@@ -0,0 +1,341 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import random
3
+ import openai
4
+ import os
5
+ from PIL import Image
6
+ from transformers import pipeline
7
+
8
+
9
+ # 初始化图片描述生成模型
10
+ image_to_text = pipeline("image-to-text", model="Salesforce/blip-image-captioning-large")
11
+
12
+
13
+ # Set OpenAI API key
14
+ OPENAI_API_KEY = st.secrets["OPENAI_API_KEY"]
15
+ if not OPENAI_API_KEY:
16
+ st.error("OPENAI_API_KEY not set in environment variables!")
17
+ raise SystemExit
18
+ openai.api_key = OPENAI_API_KEY
19
+
20
+
21
+ st.set_page_config(layout="wide", initial_sidebar_state="collapsed")
22
+
23
+
24
+ def split_image(image, output_dir='.'):
25
+ # 获取图片尺寸
26
+ width, height = image.size
27
+
28
+ # 计算每一部分的宽度
29
+ part_width = width // 3
30
+
31
+ # 存储分割后图片的路径和描述
32
+ results = []
33
+
34
+ # 分割图片并保存
35
+ for i in range(3):
36
+ left = i * part_width
37
+ right = left + part_width if i != 2 else width
38
+ part_img = image.crop((left, 0, right, height))
39
+ file_path = os.path.join(output_dir, f'part_{i + 1}.png')
40
+ part_img.save(file_path)
41
+
42
+ # 生成图片描述
43
+ caption = image_to_text(part_img)
44
+ results.append((file_path, caption))
45
+
46
+ return results
47
+
48
+ def add_numbering_after_dot(input_string):
49
+ sentences = input_string.split(".")
50
+ numbered_sentences = []
51
+ sentence_count = 1
52
+
53
+ for sentence in sentences:
54
+ sentence = sentence.strip()
55
+ if sentence:
56
+ numbered_sentences.append(f"{sentence}.【{sentence_count}】")
57
+ sentence_count += 1
58
+
59
+ return " ".join(numbered_sentences)
60
+
61
+ def add_paragraph_numbering(input_text):
62
+ paragraphs = input_text.split("\n\n")
63
+ numbered_text = ""
64
+
65
+ for i, paragraph in enumerate(paragraphs, start=1):
66
+ numbered_text += f"{i}. {paragraph}\n\n"
67
+
68
+ return numbered_text
69
+
70
+ def get_completion_from_messages(messages,
71
+ model="gpt-3.5-turbo-16k",
72
+ temperature=0, max_tokens=1000):
73
+ response = openai.ChatCompletion.create(
74
+ model=model,
75
+ messages=messages,
76
+ temperature=temperature,
77
+ max_tokens=max_tokens,
78
+ )
79
+ return response.choices[0].message["content"]
80
+
81
+ student_essay = '''One Saturday, Jack’s parents were sitting in the sofa and reading a magazic. “
82
+ I want to go Sanya on holiday!” said Jack in a cheerful manner. “Let’s go!”
83
+ A few minutes later they arrived to the air plane station.
84
+ Suddenly, the sky turned black and it stared to rain. “Oh, it’s a bad weather to go travelling!’ said Jack.
85
+ One hours later, they arrived the Sanya,
86
+ the sky was sunny and they played on the beach. “What a wonderful day!” said Jack.'''
87
+
88
+ example = """
89
+ student essay: One day, dad was reading a magazine where there have lots of information in it.
90
+ “Would you like to go on holiday in San Ya?” asked dad. He thinks San Ya is a warm city in China,
91
+ which is the perfect place to spend holiday. Then, he bought three tickets.
92
+ It rains dogs and cats when they were going to the plane. “What a bad weather!” said Tom.
93
+ One days ago, they arrived at there on Saturday.
94
+ A local person, who is the photographer, helped they took a photo. They had a happy holiday.
95
+
96
+ you should return:
97
+
98
+ Total mark: 15 points
99
+ ------------------------------------------------------------------------------------
100
+ Content
101
+
102
+ 5 points
103
+
104
+ - Each picture involves content: ✔️
105
+ - Each image contains 2-3 sentences; if sub-clauses are used, description can be reduced to 1-2 sentences for each image: ✔️
106
+ - Writing is relevant with sufficient information conveyed: ✔️
107
+
108
+ ------------------------------------------------------------------------------------
109
+ Organization
110
+
111
+ 5 points
112
+
113
+ - Structured writing with clear introduction, body, and conclusion. The first two sentences provide clear context regarding time, place, individuals, and actions: ✔️
114
+ - Sentences are 90% logically connected: ✔️
115
+ - Incorporates 2-3 different linkage techniques such as conjunctions (because, but, so) and adverbs (unexpectedly, fortunately): ✔️
116
+ - Uses at least 5 linkage techniques, with a mixture of 2-3 different methods: ✔️
117
+
118
+ ------------------------------------------------------------------------------------
119
+ Language
120
+
121
+ 5 points
122
+
123
+ Dimension 1: Complexity
124
+
125
+ - About 20% (15-20%) of words are bi-syllabic or multi-syllabic: ✔️
126
+ - Includes 3 words of B1 level or above: ✔️
127
+ - Covers 2-3 types of sub-clauses: adverbial and relative: ✔️
128
+ - At least 3 compound sentence structures: ✔️
129
+
130
+ Dimension 2: Diversity
131
+
132
+ - Diverse word usage without obvious repetitive grammar structures: ✔️
133
+
134
+ Language Dimension 3: Accuracy
135
+
136
+ - Word spelling errors (e.g., "the-they") and grammatical errors (e.g., "...Where there were lots of pleasant beaches...; I wanted to travelling to Sanya, Because"): ❌ (Vocabulary richness compensates for this shortcoming)
137
+ """
138
+
139
+ example_2 = """
140
+ student essay:In the first picture the family is looking for a maps and magazine of Hawai. In the second picture is raining and the family is took a plane. In the thirt picture the family is in the beach and have sunglasses.
141
+ you should return:
142
+
143
+ Total mark: 6 points
144
+ ------------------------------------------------------------------------------------
145
+ Content
146
+
147
+ 3 points
148
+
149
+ - Each picture involves content: ✔️
150
+ - Each image should have 2-3 sentences; if sub-clauses are used, description can be reduced to 1-2 sentences for each image: ❌
151
+ - Writing is partially relevant and some information is not sufficiently conveyed: -
152
+
153
+ ------------------------------------------------------------------------------------
154
+ Organization
155
+
156
+ 1 point
157
+
158
+ - Structured writing with a clear introduction, body, and conclusion. The first two sentences provide clear context regarding time, place, individuals, and actions: ✔️
159
+ - Less than 90% logical connection between sentences: ❌
160
+ - Insufficient use of linkage techniques: ❌
161
+
162
+ ------------------------------------------------------------------------------------
163
+ Language
164
+
165
+ 2 points
166
+
167
+ *Dimension 1: Complexity*
168
+ - About 30% of words are bi-syllabic or multi-syllabic: ✔️
169
+ - Lacks B1 level or above vocabulary: ❌
170
+ - Does not incorporate sub-clauses: ❌
171
+ - Only one coordinated sentence using "and": -
172
+
173
+ *Dimension 2: Diversity*
174
+ - Repetitive use of words such as "picture" (3 times), "family" (3 times), and present continuous tense (2 times): ❌
175
+
176
+ *Dimension 3: Accuracy*
177
+ - Two word spelling errors: "Hawai", "thirt". Grammatical errors in 2 places: "is took a plane", "is have sunglasses": ❌
178
+ - Can form complete sentences and uses basic words, so the examiner might award an additional 1 point for effort.
179
+ """
180
+
181
+ system_message = f'''
182
+ 你是一个剑桥KET作文老师。
183
+ 请你认真批改学生在【】符号中的作文
184
+ 首先你会给出你的评分,在评分文章时,你会从以下方面考虑:
185
+ Content
186
+ Organization
187
+ Language
188
+ 记住KET的Band Score都是整数
189
+ total = Content Band Score + Organization Band Score + Language Band Score。
190
+
191
+ 以下是评分的要求:
192
+ Band 5:Content:一个高质量的5级作文要确保每一幅图的内容都被涉及,并且每幅图都至少有2-3个句子描述(如果使用从句,描述可以缩减到1-2句)。整体的写作内容需要与主题紧密相关,并确保信息得到充分的传达。
193
+ Organization:作文需要有一个完整的结构,包括明确的开头、经过和结尾,且第一句应清晰地交代时间、地点、人物和行为。句子之间的逻辑连接需要达到90%以上,使用的衔接手段需要多样,涵盖两到三种不同类型(如连词、指代和特定的副词)。而且,衔接手段的使用不能少于5次,且不能都是同一种手段。
194
+ Language:复杂度:这要求作文中双音节和多音节词的占比为15-20%,至少有3-5个非日常生活词汇,包括2-3种不同类型的从句(如宾语从句、状语从句和定语从句),以及不少于3句的复合句式。
195
+ 丰富度:作文中的单词和句子结构应该变化丰富,相同的单词和句式最多只能出现1次(人名除外)。
196
+ 准确度:作文中的单词拼写和句式使用错误不能超过3个,并特别注意描述语言应使用过去时态,而对话部分则没有时态限制。
197
+
198
+ Band 4:Content:1.每幅图的内容都涉及 2.每幅图至少有1个句子 3.写作内容基本相关,1幅图细节不充分
199
+ Organization:1. 结构完整(明确的开头、经过和结尾)2.句与句之间有70%的衔接逻辑 3.包含两种衔接手段:连词、指代或副词 4.衔接手段数量为3-5个(涵盖两个种类)
200
+ Language:维度1:复杂度 1.双音节、多音节词占比为10-15% 2.有1-2个非日常生活词汇 3.1-2种从句:宾从、状从或定从 4.复合句式2-3句
201
+ 维度2:丰富度 相同单词、句式结构最多复现2次
202
+ 维度3:准确度 单词拼写错误、句式错误:3-5个(描述语言使用过去时态,对话语言不限时态)
203
+
204
+ Band 3:Content:1.每幅图内容基本涉及 2.每幅图至少有1个句子 3.写作内容稍偏离主题,至少有1幅图细节不充分
205
+ Organization:1.结构完整(明确的开头、经过和结尾)2.句与句之间有50%的衔接逻辑 3.包含一到两种衔接手段:连词、指代或副词4.衔接手段数量为2-4个(一个种类)
206
+ Language:维度1:复杂度 1. 双音节、多音节词占比为5-10% 2.单词均为日常生活词汇 3.1种从句:宾从、状从或定从 4.复合句式1句
207
+ 维度2:丰富度 相同单词、句式复现2次以上
208
+ 维度3:准确度 单词拼写错误、句式错误:6-8个(语言错误少于6个,可不写从句)
209
+
210
+ Band 2:Content:1.图片内容没有完全覆盖或图片信息有误 2.某一幅图片1个句子的描述都没有 3.写作内容稍偏离主题,至少2幅图片细节不充分
211
+ Organization:1. 结构不完整(有开头没经过或者有开头没结尾)2.句与句之间有一些逻辑(30%) 3.包含一种衔接手段:连词、指代或副词 4.衔接手段数量为1-2个
212
+ Language:维度1:复杂度 1.双音节、多音节词占比少于5% 2.单词均为日常生活词汇 3.无从句或复合句
213
+ 维度2:准确度 单词拼写错误、句式错误:5-8个
214
+
215
+ Band 1:Content:1.图片信息缺漏、有误 2.两幅图片1个句子的描述都没有 3.写作内容与图片不相干
216
+ Organization:1.故事结构不完整 2.句与句之间几乎无逻辑(10%) 3.包含一种衔接手段:连词、指代或副词 4.衔接手段数量为1个
217
+ Language:维度1:复杂度 1.双音节、多音节词占比少于3% 2.单词均为日常生活词汇 3.无从句或复合句
218
+ 维度2:准确度 单词拼写错误、句式错误:10个以上
219
+
220
+ Band 0:Content:写作内容与图片毫无关联
221
+ Organization:1. 缺乏故事结构 2.句与句之间毫无逻辑(0%) 3.无衔接手段
222
+ Language:1. 无双音节、多音节单词 2.无完整句型
223
+
224
+ 把回答都整理成markdown格式。不要给出具体建议和修改文章。
225
+
226
+ 这是一个具体的例子:{example}
227
+ 这是另一个例子:{example_2}
228
+
229
+ '''
230
+
231
+ st.markdown("""
232
+ <h2 style='color: black;'>
233
+ 阅思乐KET作文批改 demo 0.0.1
234
+ </h2>
235
+ """,
236
+ unsafe_allow_html=True
237
+ )
238
+
239
+ st.markdown(
240
+ """
241
+ <style>
242
+ /* 修改标题的大小 */
243
+ .streamlit-container h1 {
244
+ font-size: 2.5em !important;
245
+ }
246
+ </style>
247
+ """,
248
+ unsafe_allow_html=True,
249
+ )
250
+
251
+ col1, col2 = st.columns([3,3])
252
+
253
+ with col1:
254
+
255
+ uploaded_image = st.file_uploader("上传作文图片", type=["jpg", "png", "jpeg"])
256
+ if uploaded_image:
257
+ with st.spinner('处理图片...'):
258
+ st.image(uploaded_image, caption="已上传的图片。", use_column_width=True)
259
+ image = Image.open(uploaded_image).convert("RGB")
260
+ output_dir = '.'
261
+ caption_results = split_image(image, output_dir)
262
+
263
+ student_essay = st.text_area(
264
+ " 输入你的作文",
265
+ height= 400
266
+ )
267
+ push = False
268
+ if st.button("开始打分"):
269
+ push = True
270
+ with col2:
271
+ st.markdown(
272
+ """
273
+ <style>
274
+ /* 创建一个固定大小的容器,并允许滚动 */
275
+ .scrollable-container {
276
+ max-height: 400px; /* 容器的高度 */
277
+ overflow-y: auto; /* 允许垂直滚动 */
278
+ }
279
+ </style>
280
+ """,
281
+ unsafe_allow_html=True,
282
+ )
283
+
284
+ if student_essay and push and uploaded_image:
285
+ progress_placeholder = st.empty()
286
+ progress_text = "开始打分"
287
+ my_bar = progress_placeholder.progress(0, text=progress_text)
288
+ query = str(student_essay)
289
+ user_message = query
290
+ messages = [
291
+ {'role':'system',
292
+ 'content': system_message},
293
+ {'role':'user',
294
+ 'content': f"请记住你的设定和评分标准【{query}】, 三张图片对应的captions分别是:{caption_results}"},]
295
+ response = get_completion_from_messages(messages)
296
+ my_bar.progress(50, text="开始提供相关素材")
297
+ system_message_4 = '''
298
+ 你是一个KET老师,根据学生提供的文章,提供一些素材和观点的地道英文表达及中文释义;
299
+ 在每句英文的后面的括号里给出中文释义
300
+ 并把回答都整理成markdown格式。
301
+ 这是一个具体的例子:
302
+ 返回:
303
+ ❤ 此类文章可能会用到的高分素材和观点,提供给你参考 ❤
304
+ - it was a scorching summer day.
305
+
306
+ - ... along which there were a wide variety of shops.
307
+
308
+ - It was terrible to run on such a scorching/burning day.
309
+
310
+ - The moment he stopped for a short break, he had sweated heavily. (一般过去时与过去完成时连用)
311
+
312
+ - The reason why he went into a convenient store was that he wanted to get something icy to drink.
313
+
314
+ - ...sweated heavily; Jimmy asked the shopkeeper politely
315
+
316
+ - ...replied Jimmy in a cheerful manner.
317
+
318
+ 学生:'''
319
+ user_message = query
320
+ messages = [
321
+ {'role':'system',
322
+ 'content': system_message_4},
323
+ {'role':'user',
324
+ 'content': f"请记住你的设定【{query}】"},]
325
+ response3 = get_completion_from_messages(messages)
326
+ my_bar.progress(100, text="打分完成")
327
+ st.markdown('<div class="scrollable-container">', unsafe_allow_html=True)
328
+ with st.container():
329
+ st.markdown(response)
330
+ st.divider()
331
+ with st.container():
332
+ st.markdown("""
333
+ <h4 style='color: black;'>
334
+ 素材建议
335
+ </h4>
336
+ """,
337
+ unsafe_allow_html=True
338
+ )
339
+ st.write(response3)
340
+ my_bar.empty()
341
+ st.markdown('</div>', unsafe_allow_html=True)