Browen0311 commited on
Commit
34c33dc
·
verified ·
1 Parent(s): e360323

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -131
app.py CHANGED
@@ -14,147 +14,29 @@ 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
-
73
- criteria = {
74
- '題旨發揮': {'weight': 0.4, 'max_score': 40},
75
- '資料掌握': {'weight': 0.2, 'max_score': 20},
76
- '結構安排': {'weight': 0.2, 'max_score': 20},
77
- '字句運用': {'weight': 0.2, 'max_score': 20}
78
- }
79
-
80
- grade_scores = {
81
- 'A+': 95, 'A': 90, 'A-': 85,
82
- 'B+': 80, 'B': 75, 'B-': 70,
83
- 'C+': 65, 'C': 60, 'C-': 55,
84
- '0': 0
85
- }
86
-
87
- try:
88
- history = []
89
- total_score = 0
90
- history.append(("作文內容:", message))
91
- history.append(("", f"正在使用 {provider} ({model}) 進行評分分析..."))
92
-
93
- all_feedback = {}
94
-
95
- for criterion, details in criteria.items():
96
- prompt = f"""評估以下作文的{criterion}(權重{details['weight']*100}%):
97
-
98
- 作文內容:
99
- {message}
100
-
101
- {additional_prompt if additional_prompt else ''}
102
-
103
- 請依三等九級制(A+、A、A-、B+、B、B-、C+、C、C-)評分,並提供簡短評語。
104
- 如果是缺考、未作答、完全文不對題或作答內容完全照抄試題,請給予0分。
105
-
106
- 請按以下格式回覆:
107
- 等第:
108
- 評語:"""
109
-
110
- result = get_llm_response(prompt, provider, model)
111
- lines = result.lower().split('\n')
112
- grade = '0'
113
- comment = ""
114
-
115
- for line in lines:
116
- if '等第:' in line or '等第:' in line:
117
- grade_text = line.split(':')[-1].strip().upper()
118
- if grade_text in grade_scores:
119
- grade = grade_text
120
- elif '評語:' in line or '評語:' in line:
121
- comment = line.split(':')[-1].strip()
122
-
123
- weighted_score = (grade_scores[grade] / 100) * details['max_score']
124
- total_score += weighted_score
125
-
126
- feedback = f"### {criterion}\n"
127
- feedback += f"- **等第**:{grade}\n"
128
- feedback += f"- **得分**:{weighted_score:.1f}/{details['max_score']}\n"
129
- feedback += f"- **評語**:{comment}\n"
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
-
142
- return history, gr.Markdown(total_score_display)
143
-
144
- except Exception as e:
145
- return [("", f"評分過程發生錯誤:{str(e)}")], gr.Markdown("### ❌ 評分失敗")
146
 
 
147
  def update_model_choices(provider):
148
  if provider == "openai":
149
- return {
150
- "choices": list(MODEL_CONFIGS["openai"].keys()),
151
- "value": "gpt-3.5-turbo"
152
- }
153
  else:
154
- return {
155
- "choices": list(MODEL_CONFIGS["groq"].keys()),
156
- "value": "llama3-8b-8192"
157
- }
158
 
159
  # 建立 Gradio 介面
160
  with gr.Blocks(title="國文作文自動評分系統") as demo:
@@ -190,7 +72,7 @@ with gr.Blocks(title="國文作文自動評分系統") as demo:
190
  value="openai"
191
  )
192
  model = gr.Dropdown(
193
- choices=list(MODEL_CONFIGS["openai"].keys()),
194
  label="選擇模型",
195
  value="gpt-3.5-turbo",
196
  interactive=True
 
14
  groq_client = Groq(api_key=os.getenv('groq_key'))
15
 
16
  # 模型設定
17
+ OPENAI_MODELS = ["gpt-3.5-turbo", "gpt-4", "gpt-4-turbo"]
18
+ GROQ_MODELS = ["llama3-8b-8192", "gemma2-9b-it"]
19
+
20
  MODEL_CONFIGS = {
21
  "openai": {
22
+ "gpt-3.5-turbo": {"max_tokens": 4096, "temperature": 0.7},
23
+ "gpt-4": {"max_tokens": 8192, "temperature": 0.7},
24
+ "gpt-4-turbo": {"max_tokens": 4096, "temperature": 0.7}
 
 
 
 
 
 
 
 
 
25
  },
26
  "groq": {
27
+ "llama3-8b-8192": {"max_tokens": 4090, "temperature": 0.7},
28
+ "gemma2-9b-it": {"max_tokens": 1024, "temperature": 0.7}
 
 
 
 
 
 
29
  }
30
  }
31
 
32
+ [... 其他函數保持不變 ...]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33
 
34
+ # 模型選擇切換函數
35
  def update_model_choices(provider):
36
  if provider == "openai":
37
+ return gr.Dropdown(choices=OPENAI_MODELS, value="gpt-3.5-turbo")
 
 
 
38
  else:
39
+ return gr.Dropdown(choices=GROQ_MODELS, value="llama3-8b-8192")
 
 
 
40
 
41
  # 建立 Gradio 介面
42
  with gr.Blocks(title="國文作文自動評分系統") as demo:
 
72
  value="openai"
73
  )
74
  model = gr.Dropdown(
75
+ choices=OPENAI_MODELS,
76
  label="選擇模型",
77
  value="gpt-3.5-turbo",
78
  interactive=True