whyu commited on
Commit
fe5a20d
·
1 Parent(s): 1741778

add api base

Browse files
Files changed (1) hide show
  1. app.py +14 -7
app.py CHANGED
@@ -324,10 +324,14 @@ def grade(file_obj, key, model, progress=gr.Progress()):
324
 
325
 
326
  # --- Validate key and model before running grading ---
327
- def validate_key_and_model(key: str, model: str):
328
- openai.api_key = key.strip() # strip leading/trailing spaces
 
 
 
 
 
329
  try:
330
- # This call is fast and checks both key validity and model availability
331
  openai.Model.retrieve(model)
332
  return True, "OK"
333
  except openai.error.AuthenticationError:
@@ -347,10 +351,9 @@ def validate_key_and_model(key: str, model: str):
347
  return False, f"Unexpected error: {e}"
348
 
349
  # --- Wrapper for the grading function ---
350
- def run_grade(file_obj, key, model, progress=gr.Progress(track_tqdm=True)):
351
- ok, msg = validate_key_and_model(key, model)
352
  if not ok:
353
- # This will be visible to the user in the Gradio UI
354
  raise gr.Error(msg)
355
  return grade(file_obj, key, model, progress=progress)
356
 
@@ -376,6 +379,10 @@ The grading results will be downloaded as a zip file.
376
  with gr.Blocks() as demo:
377
  gr.Markdown(markdown)
378
  key = gr.Textbox(label=f"Enter your OpenAI API Key (this space will not save your API Key). [Pay Attention]: this evaluaiton may cost several dollars, please notice your OpenAI API Key balance.", type="password")
 
 
 
 
379
  model = gr.Dropdown(
380
  choices=["gpt-4-0613", "gpt-4.1", "gpt-4-turbo"],
381
  value="gpt-4.1",
@@ -385,7 +392,7 @@ with gr.Blocks() as demo:
385
  inp = gr.File(file_types=[".json"])
386
  out = gr.File(file_types=[".zip"])
387
  btn = gr.Button("Start grading", variant="primary")
388
- btn.click(fn=run_grade, inputs=[inp, key, model], outputs=out)
389
 
390
  if __name__ == "__main__":
391
  demo.queue(max_size=8).launch()
 
324
 
325
 
326
  # --- Validate key and model before running grading ---
327
+ def validate_key_and_model(key: str, model: str, api_base: str = None):
328
+ openai.api_key = key.strip()
329
+ if api_base and api_base.strip():
330
+ openai.api_base = api_base.strip() # 用户自定义 api_base
331
+ else:
332
+ openai.api_base = "https://api.openai.com/v1" # 默认官方 OpenAI
333
+
334
  try:
 
335
  openai.Model.retrieve(model)
336
  return True, "OK"
337
  except openai.error.AuthenticationError:
 
351
  return False, f"Unexpected error: {e}"
352
 
353
  # --- Wrapper for the grading function ---
354
+ def run_grade(file_obj, key, model, api_base, progress=gr.Progress(track_tqdm=True)):
355
+ ok, msg = validate_key_and_model(key, model, api_base)
356
  if not ok:
 
357
  raise gr.Error(msg)
358
  return grade(file_obj, key, model, progress=progress)
359
 
 
379
  with gr.Blocks() as demo:
380
  gr.Markdown(markdown)
381
  key = gr.Textbox(label=f"Enter your OpenAI API Key (this space will not save your API Key). [Pay Attention]: this evaluaiton may cost several dollars, please notice your OpenAI API Key balance.", type="password")
382
+ api_base = gr.Textbox(
383
+ label="Enter your OpenAI API Base (leave empty to use official OpenAI)",
384
+ value=""
385
+ )
386
  model = gr.Dropdown(
387
  choices=["gpt-4-0613", "gpt-4.1", "gpt-4-turbo"],
388
  value="gpt-4.1",
 
392
  inp = gr.File(file_types=[".json"])
393
  out = gr.File(file_types=[".zip"])
394
  btn = gr.Button("Start grading", variant="primary")
395
+ btn.click(fn=run_grade, inputs=[inp, key, model, api_base], outputs=out)
396
 
397
  if __name__ == "__main__":
398
  demo.queue(max_size=8).launch()