Update app.py
Browse files
app.py
CHANGED
@@ -187,11 +187,12 @@ class AgentState(TypedDict):
|
|
187 |
# -------------------------
|
188 |
|
189 |
class BasicAgent:
|
190 |
-
def __init__(self):
|
191 |
if not OPENAI_API_KEY:
|
192 |
raise EnvironmentError("OPENAI_API_KEY not set")
|
193 |
self.llm = OpenAI(api_key=OPENAI_API_KEY)
|
194 |
self.workflow = self._build_workflow()
|
|
|
195 |
|
196 |
def _call_llm(self, prompt: str, max_tokens: int = 256) -> str:
|
197 |
try:
|
@@ -248,10 +249,7 @@ class BasicAgent:
|
|
248 |
if state["file_url"]:
|
249 |
try:
|
250 |
print(f"Downloading {state['file_url']} …")
|
251 |
-
|
252 |
-
if HF_TOKEN:
|
253 |
-
headers["Authorization"] = f"Bearer {HF_TOKEN}"
|
254 |
-
response = requests.get(state["file_url"], headers=headers, timeout=30)
|
255 |
response.raise_for_status()
|
256 |
data = response.content
|
257 |
print(f"Successfully downloaded file, size: {len(data)} bytes")
|
@@ -343,10 +341,13 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
343 |
questions_url = f"{api_url}/questions"
|
344 |
submit_url = f"{api_url}/submit"
|
345 |
|
|
|
|
|
|
|
346 |
# 1. Instantiate Agent
|
347 |
try:
|
348 |
print("Initializing agent...")
|
349 |
-
agent = BasicAgent()
|
350 |
print("Agent initialized successfully.")
|
351 |
except Exception as e:
|
352 |
print(f"Error instantiating agent: {e}")
|
@@ -355,7 +356,7 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
355 |
# 2. Fetch Questions
|
356 |
print(f"Fetching questions from: {questions_url}")
|
357 |
try:
|
358 |
-
response =
|
359 |
response.raise_for_status()
|
360 |
questions_data = response.json()
|
361 |
if not questions_data:
|
@@ -419,7 +420,7 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
419 |
}
|
420 |
|
421 |
try:
|
422 |
-
response =
|
423 |
response.raise_for_status()
|
424 |
result_data = response.json()
|
425 |
final_status = (
|
|
|
187 |
# -------------------------
|
188 |
|
189 |
class BasicAgent:
|
190 |
+
def __init__(self, session: requests.Session):
|
191 |
if not OPENAI_API_KEY:
|
192 |
raise EnvironmentError("OPENAI_API_KEY not set")
|
193 |
self.llm = OpenAI(api_key=OPENAI_API_KEY)
|
194 |
self.workflow = self._build_workflow()
|
195 |
+
self.session = session
|
196 |
|
197 |
def _call_llm(self, prompt: str, max_tokens: int = 256) -> str:
|
198 |
try:
|
|
|
249 |
if state["file_url"]:
|
250 |
try:
|
251 |
print(f"Downloading {state['file_url']} …")
|
252 |
+
response = self.session.get(state["file_url"], timeout=30)
|
|
|
|
|
|
|
253 |
response.raise_for_status()
|
254 |
data = response.content
|
255 |
print(f"Successfully downloaded file, size: {len(data)} bytes")
|
|
|
341 |
questions_url = f"{api_url}/questions"
|
342 |
submit_url = f"{api_url}/submit"
|
343 |
|
344 |
+
# Create a persistent session for all requests
|
345 |
+
sess = requests.Session()
|
346 |
+
|
347 |
# 1. Instantiate Agent
|
348 |
try:
|
349 |
print("Initializing agent...")
|
350 |
+
agent = BasicAgent(session=sess) # Pass session to agent
|
351 |
print("Agent initialized successfully.")
|
352 |
except Exception as e:
|
353 |
print(f"Error instantiating agent: {e}")
|
|
|
356 |
# 2. Fetch Questions
|
357 |
print(f"Fetching questions from: {questions_url}")
|
358 |
try:
|
359 |
+
response = sess.get(questions_url, timeout=30)
|
360 |
response.raise_for_status()
|
361 |
questions_data = response.json()
|
362 |
if not questions_data:
|
|
|
420 |
}
|
421 |
|
422 |
try:
|
423 |
+
response = sess.post(submit_url, json=submission_data, timeout=60)
|
424 |
response.raise_for_status()
|
425 |
result_data = response.json()
|
426 |
final_status = (
|