youngtsai commited on
Commit
0bf1c9e
·
verified ·
1 Parent(s): c5b683a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +70 -16
app.py CHANGED
@@ -15,17 +15,17 @@ if not groq_key:
15
 
16
  client = Groq(api_key=groq_key)
17
 
18
- class SpellChecker:
19
  def __init__(self):
20
  pass
21
 
22
  def check_spelling(self, text):
23
  completion = client.chat.completions.create(
24
- model="llama-3.1-70b-versatile",
25
  messages=[
26
  {
27
  "role": "system",
28
- "content": "你是一個中文老師,專門挑出錯字,會將錯字列表,並用 ZH-TW 繁體中文指導我,example: 輸入:你是一個人累 ,輸出:你是一個人「累」,錯誤訂正:類"
29
  },
30
  {
31
  "role": "user",
@@ -38,24 +38,78 @@ class SpellChecker:
38
  stream=False,
39
  stop=None,
40
  )
 
 
 
 
 
41
 
42
- # Assuming the response contains a 'choices' list with 'message' objects
43
- response_content = ''.join([chunk.message.content for chunk in completion.choices])
44
-
45
  return response_content
46
 
47
- spell_checker = SpellChecker()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
48
 
49
- def check_spelling_interface(text):
50
- return spell_checker.check_spelling(text)
 
51
 
52
- with gr.Blocks(title="中文錯字檢查") as demo:
53
- gr.Markdown("# 中文錯字檢查")
54
-
55
- input_text = gr.Textbox(placeholder="輸入全文...", label="你的文本", lines=10)
56
- check_button = gr.Button("檢查錯字")
57
- output_text = gr.Textbox(label="錯字列表", lines=10)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
58
 
59
- check_button.click(check_spelling_interface, inputs=input_text, outputs=output_text)
 
 
 
 
60
 
61
  demo.launch(share=True)
 
15
 
16
  client = Groq(api_key=groq_key)
17
 
18
+ class EssayEvaluator:
19
  def __init__(self):
20
  pass
21
 
22
  def check_spelling(self, text):
23
  completion = client.chat.completions.create(
24
+ model="llama3-8b-8192",
25
  messages=[
26
  {
27
  "role": "system",
28
+ "content": "你是一個老師,請幫我挑錯字。"
29
  },
30
  {
31
  "role": "user",
 
38
  stream=False,
39
  stop=None,
40
  )
41
+
42
+ response_content = ""
43
+ for choice in completion.choices:
44
+ if hasattr(choice, 'message'):
45
+ response_content += choice.message.content
46
 
 
 
 
47
  return response_content
48
 
49
+ def overall_evaluation(self, text):
50
+ completion = client.chat.completions.create(
51
+ model="llama3-8b-8192",
52
+ messages=[
53
+ {
54
+ "role": "system",
55
+ "content": "你是一個老師,請幫我評價這篇作文。"
56
+ },
57
+ {
58
+ "role": "user",
59
+ "content": text
60
+ }
61
+ ],
62
+ temperature=1,
63
+ max_tokens=1024,
64
+ top_p=1,
65
+ stream=False,
66
+ stop=None,
67
+ )
68
+
69
+ response_content = ""
70
+ for choice in completion.choices:
71
+ if hasattr(choice, 'message'):
72
+ response_content += choice.message.content
73
+
74
+ return response_content
75
+
76
+ evaluator = EssayEvaluator()
77
 
78
+ def correct_essay(text):
79
+ feedback = evaluator.check_spelling(text)
80
+ evaluation = evaluator.overall_evaluation(text)
81
 
82
+ word_count = len(text.split())
83
+ sentence_count = len(text.split('. '))
84
+ avg_sentence_length = word_count / sentence_count if sentence_count else 0
85
+
86
+ feedback_html = '<h3>作文評價:</h3>'
87
+ feedback_html += f'<p>字數:{word_count}字</p>'
88
+ feedback_html += f'<p>句子數:{sentence_count}句</p>'
89
+ feedback_html += f'<p>平均句子長度:{avg_sentence_length:.2f}字/句</p>'
90
+
91
+ if word_count < 200:
92
+ feedback_html += '<p>建議:作文篇幅稍短,可以考慮增加一些細節描述或論述來豐富內容。</p>'
93
+ elif word_count > 800:
94
+ feedback_html += '<p>建議:作文篇幅較長,要注意是否有冗余的內容,可以考慮精簡一些不必要的描述。</p>'
95
+
96
+ if avg_sentence_length > 20:
97
+ feedback_html += '<p>建議:句子平均長度偏長,可以考慮將一些較長的句子拆分,以提高可讀性。</p>'
98
+ elif avg_sentence_length < 8:
99
+ feedback_html += '<p>建議:句子平均長度偏短,可以考慮將一些短句合併,使文章更加流暢。</p>'
100
+
101
+ feedback_html += f'<p>AI反饋:{feedback}</p>'
102
+ feedback_html += f'<p>整體評價:{evaluation}</p>'
103
+
104
+ return feedback_html
105
+
106
+ with gr.Blocks(title="AI作文批改助手") as demo:
107
+ gr.Markdown("# AI作文批改助手")
108
 
109
+ essay_input = gr.Textbox(placeholder="請在這裡粘貼您的作文...", label="你的文本", lines=10)
110
+ correct_button = gr.Button("批改作文")
111
+ feedback_output = gr.HTML(label="批改反饋")
112
+
113
+ correct_button.click(correct_essay, inputs=essay_input, outputs=feedback_output)
114
 
115
  demo.launch(share=True)