stefanches7 commited on
Commit
160f3dd
·
1 Parent(s): 9f9fb0c

pass openai api key with a window in gradio; pass openai key directly to the LLM agent constructor

Browse files
Files changed (2) hide show
  1. agent.py +3 -3
  2. gradio-ui.py +10 -1
agent.py CHANGED
@@ -9,15 +9,15 @@ import prompts
9
  class BIDSifierAgent:
10
  """Wrapper around OpenAI chat API for step-wise BIDSification."""
11
 
12
- def __init__(self, *, provider: Optional[str] = None, model: Optional[str] = None, temperature: float = 0.2):
13
  load_dotenv()
14
 
15
  if provider=="openai":
16
  if model == "gpt-5": #reasoning model that requires special handling
17
  temperature = 1.0
18
- lm = dspy.LM(f"{provider}/{model}", api_key=os.getenv("OPENAI_API_KEY"), temperature = temperature, max_tokens = 40000)
19
  else:
20
- lm = dspy.LM(f"{provider}/{model}", api_key=os.getenv("OPENAI_API_KEY"), temperature = temperature, max_tokens = 10000)
21
  else:
22
  lm = dspy.LM(f"{provider}/{model}", api_key="", max_tokens=10000)
23
 
 
9
  class BIDSifierAgent:
10
  """Wrapper around OpenAI chat API for step-wise BIDSification."""
11
 
12
+ def __init__(self, *, provider: Optional[str] = None, model: Optional[str] = None, openai_api_key: Optional[str] = None, temperature: float = 0.2):
13
  load_dotenv()
14
 
15
  if provider=="openai":
16
  if model == "gpt-5": #reasoning model that requires special handling
17
  temperature = 1.0
18
+ lm = dspy.LM(f"{provider}/{model}", api_key=openai_api_key or os.getenv("OPENAI_API_KEY"), temperature = temperature, max_tokens = 40000)
19
  else:
20
+ lm = dspy.LM(f"{provider}/{model}", api_key=openai_api_key or os.getenv("OPENAI_API_KEY"), temperature = temperature, max_tokens = 10000)
21
  else:
22
  lm = dspy.LM(f"{provider}/{model}", api_key="", max_tokens=10000)
23
 
gradio-ui.py CHANGED
@@ -122,6 +122,7 @@ def build_context(
122
  # Core callbacks
123
 
124
  def call_bidsifier_step(
 
125
  dataset_xml: str,
126
  readme_text: str,
127
  publication_text: str,
@@ -184,7 +185,7 @@ def call_bidsifier_step(
184
  step_id = BIDSIFIER_STEPS[step_label]
185
  context = build_context(dataset_xml, readme_text, publication_text, output_root)
186
 
187
- agent = BIDSifierAgent(provider=provider, model=model)
188
 
189
  # Decide whether to use the structured step prompt or a free-form query:
190
  if manual_prompt.strip():
@@ -203,6 +204,7 @@ def call_bidsifier_step(
203
  step_index = 0
204
 
205
  state = {
 
206
  "dataset_xml": dataset_xml,
207
  "readme_text": readme_text,
208
  "publication_text": publication_text,
@@ -386,6 +388,12 @@ with gr.Blocks(
386
  )
387
 
388
  with gr.Row():
 
 
 
 
 
 
389
  dataset_xml_input = gr.Textbox(
390
  label="Dataset XML",
391
  placeholder="Paste dataset_structure.xml content here (optional)",
@@ -474,6 +482,7 @@ with gr.Blocks(
474
  call_button.click(
475
  fn=call_bidsifier_step,
476
  inputs=[
 
477
  dataset_xml_input,
478
  readme_input,
479
  publication_input,
 
122
  # Core callbacks
123
 
124
  def call_bidsifier_step(
125
+ openai_api_key: str,
126
  dataset_xml: str,
127
  readme_text: str,
128
  publication_text: str,
 
185
  step_id = BIDSIFIER_STEPS[step_label]
186
  context = build_context(dataset_xml, readme_text, publication_text, output_root)
187
 
188
+ agent = BIDSifierAgent(provider=provider, model=model, openai_api_key=openai_api_key)
189
 
190
  # Decide whether to use the structured step prompt or a free-form query:
191
  if manual_prompt.strip():
 
204
  step_index = 0
205
 
206
  state = {
207
+ "openai_api_key": openai_api_key,
208
  "dataset_xml": dataset_xml,
209
  "readme_text": readme_text,
210
  "publication_text": publication_text,
 
388
  )
389
 
390
  with gr.Row():
391
+ openai_key_input = gr.Textbox(
392
+ label="OpenAI API Key",
393
+ placeholder="Paste your OpenAI API key here",
394
+ lines=1,
395
+ type="password",
396
+ )
397
  dataset_xml_input = gr.Textbox(
398
  label="Dataset XML",
399
  placeholder="Paste dataset_structure.xml content here (optional)",
 
482
  call_button.click(
483
  fn=call_bidsifier_step,
484
  inputs=[
485
+ openai_key_input,
486
  dataset_xml_input,
487
  readme_input,
488
  publication_input,