Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,10 +2,71 @@ import os
|
|
| 2 |
import gradio as gr
|
| 3 |
from openai import OpenAI
|
| 4 |
|
| 5 |
-
#
|
| 6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
if not message.strip():
|
| 10 |
return [], gr.Markdown("### 請輸入作文內容進行評分")
|
| 11 |
|
|
@@ -27,7 +88,7 @@ def evaluate_essay(message, additional_prompt):
|
|
| 27 |
history = []
|
| 28 |
total_score = 0
|
| 29 |
history.append(("作文內容:", message))
|
| 30 |
-
history.append(("", "
|
| 31 |
|
| 32 |
all_feedback = {}
|
| 33 |
|
|
@@ -46,16 +107,7 @@ def evaluate_essay(message, additional_prompt):
|
|
| 46 |
等第:
|
| 47 |
評語:"""
|
| 48 |
|
| 49 |
-
|
| 50 |
-
model="gpt-3.5-turbo",
|
| 51 |
-
messages=[
|
| 52 |
-
{"role": "system", "content": "你是一位資深的國文作文評閱委員,請依據提供的評分規準進行評分。"},
|
| 53 |
-
{"role": "user", "content": prompt}
|
| 54 |
-
],
|
| 55 |
-
temperature=0.7
|
| 56 |
-
)
|
| 57 |
-
|
| 58 |
-
result = response.choices[0].message.content
|
| 59 |
lines = result.lower().split('\n')
|
| 60 |
grade = '0'
|
| 61 |
comment = ""
|
|
@@ -78,13 +130,12 @@ def evaluate_essay(message, additional_prompt):
|
|
| 78 |
|
| 79 |
all_feedback[criterion] = feedback
|
| 80 |
|
| 81 |
-
# 分別顯示每個面向的評分
|
| 82 |
for criterion in criteria:
|
| 83 |
history.append(("", all_feedback[criterion]))
|
| 84 |
|
| 85 |
-
# 最後顯示總分
|
| 86 |
total_score_display = f"""
|
| 87 |
# 總評分結果
|
|
|
|
| 88 |
## 總分:{total_score:.1f}/100
|
| 89 |
"""
|
| 90 |
|
|
@@ -93,6 +144,9 @@ def evaluate_essay(message, additional_prompt):
|
|
| 93 |
except Exception as e:
|
| 94 |
return [("", f"評分過程發生錯誤:{str(e)}")], gr.Markdown("### ❌ 評分失敗")
|
| 95 |
|
|
|
|
|
|
|
|
|
|
| 96 |
# 建立 Gradio 介面
|
| 97 |
with gr.Blocks(title="國文作文自動評分系統") as demo:
|
| 98 |
gr.Markdown("""
|
|
@@ -119,35 +173,11 @@ with gr.Blocks(title="國文作文自動評分系統") as demo:
|
|
| 119 |
lines=2
|
| 120 |
)
|
| 121 |
|
| 122 |
-
#
|
| 123 |
with gr.Row():
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
with gr.Column(scale=2):
|
| 131 |
-
chatbot = gr.Chatbot(
|
| 132 |
-
show_copy_button=True,
|
| 133 |
-
render_markdown=True,
|
| 134 |
-
height=600,
|
| 135 |
-
label="評分詳情"
|
| 136 |
-
)
|
| 137 |
-
# 右側顯示總分
|
| 138 |
-
with gr.Column(scale=1):
|
| 139 |
-
score_display = gr.Markdown("### 等待評分...")
|
| 140 |
-
|
| 141 |
-
# 事件處理
|
| 142 |
-
submit.click(
|
| 143 |
-
evaluate_essay,
|
| 144 |
-
inputs=[msg, additional_prompt],
|
| 145 |
-
outputs=[chatbot, score_display]
|
| 146 |
-
)
|
| 147 |
-
clear.click(
|
| 148 |
-
lambda: [None, None, [], gr.Markdown("### 等待評分...")],
|
| 149 |
-
outputs=[msg, additional_prompt, chatbot, score_display]
|
| 150 |
-
)
|
| 151 |
-
|
| 152 |
-
# 啟動應用
|
| 153 |
-
demo.launch()
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
from openai import OpenAI
|
| 4 |
|
| 5 |
+
# 安裝並導入 Groq
|
| 6 |
+
try:
|
| 7 |
+
from groq import Groq
|
| 8 |
+
except ImportError:
|
| 9 |
+
os.system('pip install groq')
|
| 10 |
+
from groq import Groq
|
| 11 |
|
| 12 |
+
# 初始化 API clients
|
| 13 |
+
openai_client = OpenAI(api_key=os.getenv('OPENAI_API_KEY'))
|
| 14 |
+
groq_client = Groq(api_key=os.getenv('groq_key'))
|
| 15 |
+
|
| 16 |
+
# 模型設定
|
| 17 |
+
MODEL_CONFIGS = {
|
| 18 |
+
"openai": {
|
| 19 |
+
"gpt-3.5-turbo": {
|
| 20 |
+
"max_tokens": 4096,
|
| 21 |
+
"temperature": 0.7
|
| 22 |
+
},
|
| 23 |
+
"gpt-4": {
|
| 24 |
+
"max_tokens": 8192,
|
| 25 |
+
"temperature": 0.7
|
| 26 |
+
},
|
| 27 |
+
"gpt-4-turbo": {
|
| 28 |
+
"max_tokens": 4096,
|
| 29 |
+
"temperature": 0.7
|
| 30 |
+
}
|
| 31 |
+
},
|
| 32 |
+
"groq": {
|
| 33 |
+
"llama3-8b-8192": {
|
| 34 |
+
"max_tokens": 4090,
|
| 35 |
+
"temperature": 0.7
|
| 36 |
+
},
|
| 37 |
+
"gemma2-9b-it": {
|
| 38 |
+
"max_tokens": 1024,
|
| 39 |
+
"temperature": 0.7
|
| 40 |
+
}
|
| 41 |
+
}
|
| 42 |
+
}
|
| 43 |
+
|
| 44 |
+
def get_llm_response(prompt, provider, model):
|
| 45 |
+
if provider == "openai":
|
| 46 |
+
response = openai_client.chat.completions.create(
|
| 47 |
+
model=model,
|
| 48 |
+
messages=[
|
| 49 |
+
{"role": "system", "content": "你是一位資深的國文作文評閱委員,請依據提供的評分規準進行評分。"},
|
| 50 |
+
{"role": "user", "content": prompt}
|
| 51 |
+
],
|
| 52 |
+
**MODEL_CONFIGS["openai"][model]
|
| 53 |
+
)
|
| 54 |
+
return response.choices[0].message.content
|
| 55 |
+
else: # groq
|
| 56 |
+
completion = groq_client.chat.completions.create(
|
| 57 |
+
model=model,
|
| 58 |
+
messages=[
|
| 59 |
+
{"role": "system", "content": "你是一位資深的國文作文評閱委員,請依據提供的評分規準進行評分。"},
|
| 60 |
+
{"role": "user", "content": prompt}
|
| 61 |
+
],
|
| 62 |
+
**MODEL_CONFIGS["groq"][model],
|
| 63 |
+
stream=False,
|
| 64 |
+
top_p=1,
|
| 65 |
+
stop=None
|
| 66 |
+
)
|
| 67 |
+
return completion.choices[0].message.content
|
| 68 |
+
|
| 69 |
+
def evaluate_essay(message, additional_prompt, provider, model):
|
| 70 |
if not message.strip():
|
| 71 |
return [], gr.Markdown("### 請輸入作文內容進行評分")
|
| 72 |
|
|
|
|
| 88 |
history = []
|
| 89 |
total_score = 0
|
| 90 |
history.append(("作文內容:", message))
|
| 91 |
+
history.append(("", f"正在使用 {provider} ({model}) 進行評分分析..."))
|
| 92 |
|
| 93 |
all_feedback = {}
|
| 94 |
|
|
|
|
| 107 |
等第:
|
| 108 |
評語:"""
|
| 109 |
|
| 110 |
+
result = get_llm_response(prompt, provider, model)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 111 |
lines = result.lower().split('\n')
|
| 112 |
grade = '0'
|
| 113 |
comment = ""
|
|
|
|
| 130 |
|
| 131 |
all_feedback[criterion] = feedback
|
| 132 |
|
|
|
|
| 133 |
for criterion in criteria:
|
| 134 |
history.append(("", all_feedback[criterion]))
|
| 135 |
|
|
|
|
| 136 |
total_score_display = f"""
|
| 137 |
# 總評分結果
|
| 138 |
+
## 使用模型:{provider} ({model})
|
| 139 |
## 總分:{total_score:.1f}/100
|
| 140 |
"""
|
| 141 |
|
|
|
|
| 144 |
except Exception as e:
|
| 145 |
return [("", f"評分過程發生錯誤:{str(e)}")], gr.Markdown("### ❌ 評分失敗")
|
| 146 |
|
| 147 |
+
def update_model_choices(provider):
|
| 148 |
+
return gr.Dropdown(choices=list(MODEL_CONFIGS[provider].keys()))
|
| 149 |
+
|
| 150 |
# 建立 Gradio 介面
|
| 151 |
with gr.Blocks(title="國文作文自動評分系統") as demo:
|
| 152 |
gr.Markdown("""
|
|
|
|
| 173 |
lines=2
|
| 174 |
)
|
| 175 |
|
| 176 |
+
# 模型選擇
|
| 177 |
with gr.Row():
|
| 178 |
+
provider = gr.Radio(
|
| 179 |
+
choices=["openai", "groq"],
|
| 180 |
+
label="選擇服務提供者",
|
| 181 |
+
value="openai"
|
| 182 |
+
)
|
| 183 |
+
model = gr.D
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|