GoodLuckChuck commited on
Commit
6fbab6e
·
verified ·
1 Parent(s): 06ecce4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -24
app.py CHANGED
@@ -1,32 +1,40 @@
1
  import os
2
  import json
3
  import requests
4
- from typing import List, Dict, Any, Optional, Type
5
  from pydantic import BaseModel, Field
6
  from openai import OpenAI
7
- from dotenv import load_dotenv
8
-
9
- # Load environment variables from .env file
10
- env_path = '/home/nik/hf-agent-class/final test/.env'
11
- if not os.path.exists(env_path):
12
- print(f"Warning: .env file not found at {env_path}")
13
- load_dotenv(env_path)
14
-
15
- if not os.environ.get("OPENAI_API_KEY"):
16
- raise ValueError("OPENAI_API_KEY not found in environment variables")
17
-
18
- # --- Configuration ---
19
- # Ensure your OPENAI_API_KEY is set as an environment variable
20
- # client = OpenAI(api_key=os.environ.get("OPENAI_API_KEY"))
21
- # For this example, let's ensure the script can run even if the key isn't immediately set
22
- # by initializing client later or handling the potential error.
23
- try:
24
- client = OpenAI(api_key=os.environ.get("OPENAI_API_KEY"))
25
- if not os.environ.get("OPENAI_API_KEY"):
26
- print("Warning: OPENAI_API_KEY environment variable not set. API calls will fail.")
27
- except Exception as e:
28
- print(f"Error initializing OpenAI client: {e}. Ensure API key is set.")
29
- client = None # API calls will fail
 
 
 
 
 
 
 
 
30
 
31
  # Update model to use ChatGPT
32
  LLM_MODEL = "gpt-3.5-turbo" # Using ChatGPT model
 
1
  import os
2
  import json
3
  import requests
4
+ from typing import List, Dict, Any, Optional
5
  from pydantic import BaseModel, Field
6
  from openai import OpenAI
7
+ from dotenv import load_dotenv # Keep for potential local testing, but HF uses secrets
8
+
9
+ # --- API Configuration (from your course) ---
10
+ API_BASE_URL = "https://agents-course-unit4-scoring.hf.space"
11
+ HEADERS = {"Content-Type": "application/json", "accept": "application/json"}
12
+
13
+ # --- OpenAI Client Configuration ---
14
+ # For Hugging Face Spaces, the API key will be loaded from Secrets.
15
+ OPENAI_API_KEY_VALUE = os.environ.get("OPENAI_API_KEY")
16
+
17
+ # This 'load_dotenv()' is mainly for local convenience.
18
+ # In HF Spaces, if the secret OPENAI_API_KEY is set, the above line will get it.
19
+ if not OPENAI_API_KEY_VALUE:
20
+ print("OPENAI_API_KEY not found in environment variables. Attempting to load from a local .env file (if present)...")
21
+ load_dotenv() # Looks for .env in the current directory (root of your Space)
22
+ OPENAI_API_KEY_VALUE = os.environ.get("OPENAI_API_KEY")
23
+
24
+ if not OPENAI_API_KEY_VALUE:
25
+ print("CRITICAL ERROR: OPENAI_API_KEY is not set.")
26
+ print("Please ensure it's defined as a Secret in your Hugging Face Space settings.")
27
+ # In a real app, you might raise an error or exit if the key is crucial
28
+ client = None
29
+ else:
30
+ try:
31
+ client = OpenAI(api_key=OPENAI_API_KEY_VALUE)
32
+ print("OpenAI client initialized successfully.")
33
+ except Exception as e:
34
+ print(f"Error initializing OpenAI client: {e}")
35
+ client = None
36
+
37
+ # ... (rest of your script: LLM_MODEL, GAIA_FINAL_ANSWER_SYSTEM_PROMPT, Pydantic models, Tool functions, AIAgent class, API interaction functions like api_get_questions, api_download_file, api_submit_answers should remain as per my previous correct versions) ...
38
 
39
  # Update model to use ChatGPT
40
  LLM_MODEL = "gpt-3.5-turbo" # Using ChatGPT model