cesarleoni commited on
Commit
06247af
·
verified ·
1 Parent(s): ddb8c97

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -11
app.py CHANGED
@@ -2,37 +2,42 @@ import os
2
  import gradio as gr
3
  import requests
4
  import pandas as pd
5
- import openai
6
 
7
  # --- Constants ---
8
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
9
 
 
 
 
 
 
 
 
10
  # --- Simple OpenAI‑based Agent ---
11
  class GaiaAgent:
12
  def __init__(self, model_name: str = "gpt-4"):
13
- # Load your API key (no underscore)
14
- key = os.getenv("OPENAIAPIKEY")
15
- if not key:
16
- raise ValueError("Please set OPENAIAPIKEY in your Space secrets!")
17
- openai.api_key = key
18
  self.model = model_name
19
 
20
  def __call__(self, question: str) -> str:
21
  """
22
  Sends the question to OpenAI and returns the assistant's response.
23
  """
24
- prompt = [
25
- {"role": "system", "content": "You are a helpful assistant answering GAIA Level 1 questions. Be concise and return just the final answer."},
26
  {"role": "user", "content": question}
27
  ]
28
- resp = openai.ChatCompletion.create(
29
  model=self.model,
30
- messages=prompt,
31
  temperature=0.0,
32
  max_tokens=256,
33
  )
 
34
  return resp.choices[0].message.content.strip()
35
 
 
36
  def run_and_submit_all(profile: gr.OAuthProfile | None):
37
  """
38
  Fetches GAIA questions, runs GaiaAgent on each, submits answers,
@@ -102,9 +107,10 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
102
 
103
  return status, pd.DataFrame(results)
104
 
 
105
  # --- Gradio Interface ---
106
  with gr.Blocks() as demo:
107
- gr.Markdown("# GAIA Level 1 Agent (OpenAI‑only)")
108
  gr.Markdown(
109
  """
110
  1. Add your OpenAI key (no underscore) as `OPENAIAPIKEY` in Space secrets.
 
2
  import gradio as gr
3
  import requests
4
  import pandas as pd
5
+ from openai import OpenAI
6
 
7
  # --- Constants ---
8
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
9
 
10
+ # --- Initialize OpenAI client once ---
11
+ key = os.getenv("OPENAIAPIKEY")
12
+ if not key:
13
+ raise ValueError("Please set OPENAIAPIKEY in your Space secrets!")
14
+ client = OpenAI(api_key=key)
15
+
16
+
17
  # --- Simple OpenAI‑based Agent ---
18
  class GaiaAgent:
19
  def __init__(self, model_name: str = "gpt-4"):
20
+ self.client = client
 
 
 
 
21
  self.model = model_name
22
 
23
  def __call__(self, question: str) -> str:
24
  """
25
  Sends the question to OpenAI and returns the assistant's response.
26
  """
27
+ messages = [
28
+ {"role": "system", "content": "You are a helpful assistant answering GAIA Level 1 questions. Return only the final answer."},
29
  {"role": "user", "content": question}
30
  ]
31
+ resp = self.client.chat.completions.create(
32
  model=self.model,
33
+ messages=messages,
34
  temperature=0.0,
35
  max_tokens=256,
36
  )
37
+ # The new client returns a dict‑like object
38
  return resp.choices[0].message.content.strip()
39
 
40
+
41
  def run_and_submit_all(profile: gr.OAuthProfile | None):
42
  """
43
  Fetches GAIA questions, runs GaiaAgent on each, submits answers,
 
107
 
108
  return status, pd.DataFrame(results)
109
 
110
+
111
  # --- Gradio Interface ---
112
  with gr.Blocks() as demo:
113
+ gr.Markdown("# GAIA Level 1 Agent (new OpenAI client)")
114
  gr.Markdown(
115
  """
116
  1. Add your OpenAI key (no underscore) as `OPENAIAPIKEY` in Space secrets.