Spaces:
No application file
No application file
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,32 +1,40 @@
|
|
| 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
|
| 8 |
-
|
| 9 |
-
#
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
#
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
#
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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
|