Spaces:
No application file
No application file
Update app.py
Browse files
app.py
CHANGED
|
@@ -14,6 +14,8 @@ from fastapi import FastAPI
|
|
| 14 |
from fastapi.middleware.cors import CORSMiddleware
|
| 15 |
import os
|
| 16 |
import pandas as pd
|
|
|
|
|
|
|
| 17 |
|
| 18 |
# Configure logging
|
| 19 |
logging.basicConfig(
|
|
@@ -146,8 +148,8 @@ class LocalSearch(Tool):
|
|
| 146 |
|
| 147 |
class ImprovedAgent:
|
| 148 |
"""Enhanced GAIA agent implementation"""
|
| 149 |
-
def __init__(self,
|
| 150 |
-
self.
|
| 151 |
self.tools = {tool.name: tool for tool in tools}
|
| 152 |
self.state = AgentState()
|
| 153 |
self._executor = ThreadPoolExecutor(max_workers=3)
|
|
@@ -222,10 +224,18 @@ FINAL ANSWER: [Your conclusive answer]"""
|
|
| 222 |
return "\n".join(context)
|
| 223 |
|
| 224 |
async def _get_model_response(self, context: str) -> str:
|
| 225 |
-
"""Get the model's response
|
| 226 |
try:
|
| 227 |
-
response =
|
| 228 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 229 |
except Exception as e:
|
| 230 |
logger.error(f"Error getting model response: {e}")
|
| 231 |
return ""
|
|
@@ -348,14 +358,30 @@ app.add_middleware(
|
|
| 348 |
allow_headers=["*"],
|
| 349 |
)
|
| 350 |
|
| 351 |
-
# Initialize agent globally
|
|
|
|
| 352 |
agent = None
|
| 353 |
try:
|
| 354 |
-
from
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 355 |
knowledge_base_path = Path(__file__).parent / "knowledge_base"
|
| 356 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 357 |
except Exception as e:
|
| 358 |
-
print(f"Error initializing agent: {e}")
|
| 359 |
|
| 360 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
| 361 |
"""Process questions and submit answers"""
|
|
|
|
| 14 |
from fastapi.middleware.cors import CORSMiddleware
|
| 15 |
import os
|
| 16 |
import pandas as pd
|
| 17 |
+
import openai
|
| 18 |
+
from openai import OpenAI
|
| 19 |
|
| 20 |
# Configure logging
|
| 21 |
logging.basicConfig(
|
|
|
|
| 148 |
|
| 149 |
class ImprovedAgent:
|
| 150 |
"""Enhanced GAIA agent implementation"""
|
| 151 |
+
def __init__(self, openai_client, tools: List[Tool]):
|
| 152 |
+
self.client = openai_client
|
| 153 |
self.tools = {tool.name: tool for tool in tools}
|
| 154 |
self.state = AgentState()
|
| 155 |
self._executor = ThreadPoolExecutor(max_workers=3)
|
|
|
|
| 224 |
return "\n".join(context)
|
| 225 |
|
| 226 |
async def _get_model_response(self, context: str) -> str:
|
| 227 |
+
"""Get the model's response using GPT-4"""
|
| 228 |
try:
|
| 229 |
+
response = self.client.chat.completions.create(
|
| 230 |
+
model="gpt-4",
|
| 231 |
+
messages=[
|
| 232 |
+
{"role": "system", "content": self.system_prompt},
|
| 233 |
+
{"role": "user", "content": context}
|
| 234 |
+
],
|
| 235 |
+
temperature=0.1,
|
| 236 |
+
max_tokens=1000
|
| 237 |
+
)
|
| 238 |
+
return response.choices[0].message.content
|
| 239 |
except Exception as e:
|
| 240 |
logger.error(f"Error getting model response: {e}")
|
| 241 |
return ""
|
|
|
|
| 358 |
allow_headers=["*"],
|
| 359 |
)
|
| 360 |
|
| 361 |
+
# Initialize OpenAI client and agent globally
|
| 362 |
+
client = None
|
| 363 |
agent = None
|
| 364 |
try:
|
| 365 |
+
# Get API key from environment (HF Secrets)
|
| 366 |
+
api_key = os.environ.get("OPENAI_API_KEY", "").strip()
|
| 367 |
+
if not api_key:
|
| 368 |
+
raise ValueError("OPENAI_API_KEY not found in environment variables")
|
| 369 |
+
|
| 370 |
+
client = OpenAI(api_key=api_key)
|
| 371 |
knowledge_base_path = Path(__file__).parent / "knowledge_base"
|
| 372 |
+
|
| 373 |
+
# Test the API key
|
| 374 |
+
test_response = client.chat.completions.create(
|
| 375 |
+
model="gpt-4",
|
| 376 |
+
messages=[{"role": "user", "content": "test"}],
|
| 377 |
+
max_tokens=5
|
| 378 |
+
)
|
| 379 |
+
print("OpenAI client initialized successfully with GPT-4")
|
| 380 |
+
|
| 381 |
+
# Create agent with OpenAI client
|
| 382 |
+
agent = create_agent(client, knowledge_base_path)
|
| 383 |
except Exception as e:
|
| 384 |
+
print(f"Error initializing OpenAI client or agent: {e}")
|
| 385 |
|
| 386 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
| 387 |
"""Process questions and submit answers"""
|