yuxbox commited on
Commit
e794581
·
verified ·
1 Parent(s): a1aaf30

Upload folder using huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +23 -9
app.py CHANGED
@@ -674,6 +674,7 @@ def run_agent_on_case(
674
  case: MedicalCase,
675
  backend: str,
676
  context_mode: str,
 
677
  ) -> tuple[str, str, str]:
678
  """
679
  Run the agent on a case and return formatted markdown outputs.
@@ -685,11 +686,14 @@ def run_agent_on_case(
685
  model_name = "simulated"
686
  else:
687
  try:
688
- client = create_client(backend)
 
 
 
689
  except Exception as e:
690
  return (
691
  f"**Error creating {backend} client:** {e}\n\n"
692
- "Make sure your API key is set in `.env` or environment variables. "
693
  "Or select **simulated (no API key)** to see a demo trace.",
694
  "", "",
695
  )
@@ -756,13 +760,13 @@ def on_demo_case_selected(case_name: str) -> tuple[str, str]:
756
  return "", ""
757
 
758
 
759
- def run_demo_case(case_name: str, backend: str, context_mode: str):
760
  """Run agent on a selected demo case."""
761
  if case_name not in DEMO_CASES:
762
  return "Please select a demo case.", "", ""
763
 
764
  case = DEMO_CASES[case_name]["case"]()
765
- return run_agent_on_case(case, backend, context_mode)
766
 
767
 
768
  def run_custom_case(
@@ -772,6 +776,7 @@ def run_custom_case(
772
  ch3_name: str, ch3_type: str, ch3_value: str,
773
  uploaded_image,
774
  backend: str, context_mode: str,
 
775
  ):
776
  """Run agent on a custom user-defined case."""
777
  if not scenario.strip():
@@ -784,7 +789,7 @@ def run_custom_case(
784
  ch3_name, ch3_type, ch3_value,
785
  uploaded_image,
786
  )
787
- return run_agent_on_case(case, backend, context_mode)
788
 
789
 
790
  # ============================================================
@@ -804,9 +809,10 @@ def create_app():
804
  elem_classes="header-text",
805
  )
806
 
807
- # Build backend choices: simulation always available, real backends if keys exist
808
- backend_choices = ["simulated (no API key)"] + AVAILABLE_BACKENDS
809
- default_backend = AVAILABLE_BACKENDS[0] if AVAILABLE_BACKENDS else "simulated (no API key)"
 
810
 
811
  with gr.Row():
812
  backend = gr.Dropdown(
@@ -823,6 +829,13 @@ def create_app():
823
  info="How the agent manages conversation history",
824
  scale=1,
825
  )
 
 
 
 
 
 
 
826
 
827
  with gr.Tabs():
828
  # ---- Tab 1: Demo Cases ----
@@ -857,7 +870,7 @@ def create_app():
857
 
858
  run_demo_btn.click(
859
  fn=run_demo_case,
860
- inputs=[case_selector, backend, context_mode],
861
  outputs=[demo_steps, demo_entropy, demo_summary],
862
  )
863
 
@@ -931,6 +944,7 @@ def create_app():
931
  ch3_name, ch3_type, ch3_value,
932
  custom_image,
933
  backend, context_mode,
 
934
  ],
935
  outputs=[custom_steps, custom_entropy, custom_summary],
936
  )
 
674
  case: MedicalCase,
675
  backend: str,
676
  context_mode: str,
677
+ user_api_key: str = "",
678
  ) -> tuple[str, str, str]:
679
  """
680
  Run the agent on a case and return formatted markdown outputs.
 
686
  model_name = "simulated"
687
  else:
688
  try:
689
+ kwargs = {}
690
+ if user_api_key and user_api_key.strip():
691
+ kwargs["api_key"] = user_api_key.strip()
692
+ client = create_client(backend, **kwargs)
693
  except Exception as e:
694
  return (
695
  f"**Error creating {backend} client:** {e}\n\n"
696
+ "Make sure you enter your API key above, or set it in environment variables. "
697
  "Or select **simulated (no API key)** to see a demo trace.",
698
  "", "",
699
  )
 
760
  return "", ""
761
 
762
 
763
+ def run_demo_case(case_name: str, backend: str, context_mode: str, user_api_key: str = ""):
764
  """Run agent on a selected demo case."""
765
  if case_name not in DEMO_CASES:
766
  return "Please select a demo case.", "", ""
767
 
768
  case = DEMO_CASES[case_name]["case"]()
769
+ return run_agent_on_case(case, backend, context_mode, user_api_key)
770
 
771
 
772
  def run_custom_case(
 
776
  ch3_name: str, ch3_type: str, ch3_value: str,
777
  uploaded_image,
778
  backend: str, context_mode: str,
779
+ user_api_key: str = "",
780
  ):
781
  """Run agent on a custom user-defined case."""
782
  if not scenario.strip():
 
789
  ch3_name, ch3_type, ch3_value,
790
  uploaded_image,
791
  )
792
+ return run_agent_on_case(case, backend, context_mode, user_api_key)
793
 
794
 
795
  # ============================================================
 
809
  elem_classes="header-text",
810
  )
811
 
812
+ # Build backend choices: always show real backends (user can paste their own key)
813
+ all_backends = ["openai", "anthropic", "together"]
814
+ backend_choices = ["simulated (no API key)"] + all_backends
815
+ default_backend = "openai"
816
 
817
  with gr.Row():
818
  backend = gr.Dropdown(
 
829
  info="How the agent manages conversation history",
830
  scale=1,
831
  )
832
+ user_api_key = gr.Textbox(
833
+ label="API Key (optional)",
834
+ placeholder="sk-... (paste your OpenAI/Anthropic key)",
835
+ type="password",
836
+ info="Enter your own API key to use a real VLM backend",
837
+ scale=1,
838
+ )
839
 
840
  with gr.Tabs():
841
  # ---- Tab 1: Demo Cases ----
 
870
 
871
  run_demo_btn.click(
872
  fn=run_demo_case,
873
+ inputs=[case_selector, backend, context_mode, user_api_key],
874
  outputs=[demo_steps, demo_entropy, demo_summary],
875
  )
876
 
 
944
  ch3_name, ch3_type, ch3_value,
945
  custom_image,
946
  backend, context_mode,
947
+ user_api_key,
948
  ],
949
  outputs=[custom_steps, custom_entropy, custom_summary],
950
  )