Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -15,17 +15,17 @@ if not groq_key:
|
|
| 15 |
|
| 16 |
client = Groq(api_key=groq_key)
|
| 17 |
|
| 18 |
-
class
|
| 19 |
def __init__(self):
|
| 20 |
pass
|
| 21 |
|
| 22 |
def check_spelling(self, text):
|
| 23 |
completion = client.chat.completions.create(
|
| 24 |
-
model="
|
| 25 |
messages=[
|
| 26 |
{
|
| 27 |
"role": "system",
|
| 28 |
-
"content": "
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
|
| 49 |
-
def
|
| 50 |
-
|
|
|
|
| 51 |
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
|
| 59 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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)
|