Spaces:
Sleeping
Sleeping
Samuel Thomas
commited on
Commit
·
9420d94
1
Parent(s):
38df4e4
update app
Browse files
app.py
CHANGED
@@ -3,7 +3,7 @@ import gradio as gr
|
|
3 |
import requests
|
4 |
import inspect
|
5 |
import pandas as pd
|
6 |
-
from tools import intelligent_agent, get_file_type, write_bytes_to_temp_dir
|
7 |
|
8 |
# (Keep Constants as is)
|
9 |
# --- Constants ---
|
@@ -11,14 +11,14 @@ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
|
11 |
|
12 |
# --- Basic Agent Definition ---
|
13 |
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
14 |
-
class BasicAgent:
|
15 |
-
def __init__(self):
|
16 |
-
print("BasicAgent initialized.")
|
17 |
-
def __call__(self, question: str) -> str:
|
18 |
-
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
19 |
-
fixed_answer = "This is a default answer."
|
20 |
-
print(f"Agent returning fixed answer: {fixed_answer}")
|
21 |
-
return fixed_answer
|
22 |
|
23 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
24 |
"""
|
@@ -39,9 +39,37 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
39 |
questions_url = f"{api_url}/questions"
|
40 |
submit_url = f"{api_url}/submit"
|
41 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
42 |
# 1. Instantiate Agent ( modify this part to create your agent)
|
43 |
try:
|
44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
45 |
except Exception as e:
|
46 |
print(f"Error instantiating agent: {e}")
|
47 |
return f"Error initializing agent: {e}", None
|
@@ -70,23 +98,6 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
70 |
print(f"An unexpected error occurred fetching questions: {e}")
|
71 |
return f"An unexpected error occurred fetching questions: {e}", None
|
72 |
|
73 |
-
# 3. Create states
|
74 |
-
for item in hf_questions:
|
75 |
-
file_name = item.get('file_name', '')
|
76 |
-
if file_name == '':
|
77 |
-
item['input_file'] = None
|
78 |
-
item['file_type'] = None
|
79 |
-
item['file_path'] = None
|
80 |
-
else:
|
81 |
-
# Call the API to retrieve the file; adjust params as needed
|
82 |
-
task_id = item['task_id']
|
83 |
-
api_response = requests.get(f"{api_url}/{task_id}")
|
84 |
-
if api_response.status_code == 200:
|
85 |
-
item['input_file'] = api_response.content # Store file as bytes
|
86 |
-
item['file_type'] = get_file_type(file_name)
|
87 |
-
item['file_path'] = write_bytes_to_temp_dir(item['input_file'], file_name)
|
88 |
-
else:
|
89 |
-
item['input_file'] = None # Or handle error as needed
|
90 |
|
91 |
"""
|
92 |
# 3. Run your Agent
|
|
|
3 |
import requests
|
4 |
import inspect
|
5 |
import pandas as pd
|
6 |
+
from tools import intelligent_agent, get_file_type, write_bytes_to_temp_dir, State
|
7 |
|
8 |
# (Keep Constants as is)
|
9 |
# --- Constants ---
|
|
|
11 |
|
12 |
# --- Basic Agent Definition ---
|
13 |
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
14 |
+
#class BasicAgent:
|
15 |
+
# def __init__(self):
|
16 |
+
# print("BasicAgent initialized.")
|
17 |
+
# def __call__(self, question: str) -> str:
|
18 |
+
# print(f"Agent received question (first 50 chars): {question[:50]}...")
|
19 |
+
# fixed_answer = "This is a default answer."
|
20 |
+
# print(f"Agent returning fixed answer: {fixed_answer}")
|
21 |
+
# return fixed_answer
|
22 |
|
23 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
24 |
"""
|
|
|
39 |
questions_url = f"{api_url}/questions"
|
40 |
submit_url = f"{api_url}/submit"
|
41 |
|
42 |
+
# 1. Create states
|
43 |
+
for item in hf_questions:
|
44 |
+
file_name = item.get('file_name', '')
|
45 |
+
if file_name == '':
|
46 |
+
item['input_file'] = None
|
47 |
+
item['file_type'] = None
|
48 |
+
item['file_path'] = None
|
49 |
+
else:
|
50 |
+
# Call the API to retrieve the file; adjust params as needed
|
51 |
+
task_id = item['task_id']
|
52 |
+
api_response = requests.get(f"{api_url}/{task_id}")
|
53 |
+
if api_response.status_code == 200:
|
54 |
+
item['input_file'] = api_response.content # Store file as bytes
|
55 |
+
item['file_type'] = get_file_type(file_name)
|
56 |
+
item['file_path'] = write_bytes_to_temp_dir(item['input_file'], file_name)
|
57 |
+
else:
|
58 |
+
item['input_file'] = None # Or handle error as needed
|
59 |
+
|
60 |
# 1. Instantiate Agent ( modify this part to create your agent)
|
61 |
try:
|
62 |
+
ans = []
|
63 |
+
for r in range(len(hf_questions)):
|
64 |
+
s = State(question = hf_questions[r]['question'],
|
65 |
+
input_file = hf_questions[r]['input_file'],
|
66 |
+
file_type = hf_questions[r]['file_type'],
|
67 |
+
file_path = hf_questions[r]['file_path'])
|
68 |
+
print(s['question'])
|
69 |
+
temp_ans = intelligent_agent(s)
|
70 |
+
print(temp_ans)
|
71 |
+
print('\n')
|
72 |
+
ans.append(temp_ans)
|
73 |
except Exception as e:
|
74 |
print(f"Error instantiating agent: {e}")
|
75 |
return f"Error initializing agent: {e}", None
|
|
|
98 |
print(f"An unexpected error occurred fetching questions: {e}")
|
99 |
return f"An unexpected error occurred fetching questions: {e}", None
|
100 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
101 |
|
102 |
"""
|
103 |
# 3. Run your Agent
|