SonyaX20 commited on
Commit
d5760d5
·
1 Parent(s): a284143
Files changed (1) hide show
  1. app.py +43 -8
app.py CHANGED
@@ -170,7 +170,7 @@ def extract_text_from_image(image):
170
  return f"图片处理出错: {str(e)}"
171
 
172
  def analyze_slide(text):
173
- """使用 GPT-4 分析幻灯片内容"""
174
  try:
175
  prompt = f"""请分析以下幻灯片内容,并提供清晰的讲解:
176
 
@@ -185,7 +185,7 @@ def analyze_slide(text):
185
  请用中文回答,语言要通俗易懂。"""
186
 
187
  response = client.chat.completions.create(
188
- model="gpt-4",
189
  messages=[{"role": "user", "content": prompt}],
190
  temperature=0.7,
191
  max_tokens=1000
@@ -193,7 +193,13 @@ def analyze_slide(text):
193
 
194
  return response.choices[0].message.content
195
  except Exception as e:
196
- return f"内容分析出错: {str(e)}"
 
 
 
 
 
 
197
 
198
  def chat_with_assistant(message, history, slide_text):
199
  """与 AI 助手对话"""
@@ -218,7 +224,7 @@ def chat_with_assistant(message, history, slide_text):
218
  messages.append({"role": "user", "content": message})
219
 
220
  response = client.chat.completions.create(
221
- model="gpt-4",
222
  messages=messages,
223
  temperature=0.7
224
  )
@@ -226,12 +232,20 @@ def chat_with_assistant(message, history, slide_text):
226
  history.append((message, response.choices[0].message.content))
227
  return history
228
  except Exception as e:
229
- history.append((message, f"回答出错: {str(e)}"))
 
 
 
 
 
 
 
230
  return history
231
 
232
  def check_api_key():
233
- """检查 API Key 是否已设置"""
234
- if not os.getenv('OPENAI_API_KEY'):
 
235
  return """
236
  <div style="padding: 1rem; background-color: #ffebee; border-radius: 0.5rem; margin: 1rem 0;">
237
  <h2 style="color: #c62828;">⚠️ OpenAI API Key 未设置</h2>
@@ -245,7 +259,28 @@ def check_api_key():
245
  </ol>
246
  </div>
247
  """
248
- return None
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
249
 
250
  # 创建 Gradio 界面
251
  with gr.Blocks(title="课程幻灯片理解助手") as demo:
 
170
  return f"图片处理出错: {str(e)}"
171
 
172
  def analyze_slide(text):
173
+ """使用 GPT-3.5-turbo 分析幻灯片内容"""
174
  try:
175
  prompt = f"""请分析以下幻灯片内容,并提供清晰的讲解:
176
 
 
185
  请用中文回答,语言要通俗易懂。"""
186
 
187
  response = client.chat.completions.create(
188
+ model="gpt-3.5-turbo", # 改用 GPT-3.5-turbo
189
  messages=[{"role": "user", "content": prompt}],
190
  temperature=0.7,
191
  max_tokens=1000
 
193
 
194
  return response.choices[0].message.content
195
  except Exception as e:
196
+ error_msg = str(e)
197
+ if "model_not_found" in error_msg:
198
+ return "API 配置错误:无法访问指定的模型。请确保您的 OpenAI API Key 有正确的访问权限。"
199
+ elif "invalid_request_error" in error_msg:
200
+ return "API 请求错误:请检查 API Key 是否正确设置。"
201
+ else:
202
+ return f"内容分析出错: {error_msg}"
203
 
204
  def chat_with_assistant(message, history, slide_text):
205
  """与 AI 助手对话"""
 
224
  messages.append({"role": "user", "content": message})
225
 
226
  response = client.chat.completions.create(
227
+ model="gpt-3.5-turbo", # 改用 GPT-3.5-turbo
228
  messages=messages,
229
  temperature=0.7
230
  )
 
232
  history.append((message, response.choices[0].message.content))
233
  return history
234
  except Exception as e:
235
+ error_msg = str(e)
236
+ if "model_not_found" in error_msg:
237
+ error_response = "API 配置错误:无法访问指定的模型。请确保您的 OpenAI API Key 有正确的访问权限。"
238
+ elif "invalid_request_error" in error_msg:
239
+ error_response = "API 请求错误:请检查 API Key 是否正确设置。"
240
+ else:
241
+ error_response = f"回答出错: {error_msg}"
242
+ history.append((message, error_response))
243
  return history
244
 
245
  def check_api_key():
246
+ """检查 API Key 是否已设置并测试连接"""
247
+ api_key = os.getenv('OPENAI_API_KEY')
248
+ if not api_key:
249
  return """
250
  <div style="padding: 1rem; background-color: #ffebee; border-radius: 0.5rem; margin: 1rem 0;">
251
  <h2 style="color: #c62828;">⚠️ OpenAI API Key 未设置</h2>
 
259
  </ol>
260
  </div>
261
  """
262
+
263
+ # 测试 API 连接
264
+ try:
265
+ client.chat.completions.create(
266
+ model="gpt-3.5-turbo",
267
+ messages=[{"role": "user", "content": "测试连接"}],
268
+ max_tokens=5
269
+ )
270
+ return None
271
+ except Exception as e:
272
+ return f"""
273
+ <div style="padding: 1rem; background-color: #ffebee; border-radius: 0.5rem; margin: 1rem 0;">
274
+ <h2 style="color: #c62828;">⚠️ API 连接测试失败</h2>
275
+ <p>错误信息:{str(e)}</p>
276
+ <p>请检查:</p>
277
+ <ol>
278
+ <li>API Key 是否正确</li>
279
+ <li>API Key 是否有效</li>
280
+ <li>是否有足够的额度</li>
281
+ </ol>
282
+ </div>
283
+ """
284
 
285
  # 创建 Gradio 界面
286
  with gr.Blocks(title="课程幻灯片理解助手") as demo: