Spaces:
Sleeping
Sleeping
EtienneB
commited on
Commit
·
43da00a
1
Parent(s):
2d991f8
Update app.py
Browse files
app.py
CHANGED
@@ -1,3 +1,4 @@
|
|
|
|
1 |
import inspect
|
2 |
import os
|
3 |
|
@@ -28,36 +29,56 @@ HUGGINGFACEHUB_API_TOKEN = os.getenv("HUGGINGFACEHUB_API_TOKEN")
|
|
28 |
class BasicAgent:
|
29 |
def __init__(self):
|
30 |
print("BasicAgent initialized.")
|
31 |
-
self.llm = HuggingFaceEndpoint(
|
|
|
|
|
|
|
32 |
self.chat = ChatHuggingFace(llm=self.llm, verbose=True)
|
33 |
self.tools = [
|
34 |
-
multiply,
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
divide,
|
39 |
-
modulus,
|
40 |
-
square_root,
|
41 |
-
floor_divide,
|
42 |
-
absolute,
|
43 |
-
logarithm,
|
44 |
-
exponential,
|
45 |
-
web_search,
|
46 |
-
roman_calculator_converter,
|
47 |
-
get_current_time_in_timezone,
|
48 |
]
|
49 |
self.chat_with_tools = self.chat.bind_tools(self.tools)
|
50 |
print(f"Total tools available: {len(self.tools)}")
|
51 |
|
52 |
-
|
53 |
-
def __call__(self, question: str) -> str:
|
54 |
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
55 |
-
|
56 |
-
# Question in a HumanMessage from langchain_core
|
57 |
messages = [HumanMessage(content=question)]
|
58 |
-
|
59 |
-
|
60 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
61 |
|
62 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
63 |
"""
|
|
|
1 |
+
import asyncio
|
2 |
import inspect
|
3 |
import os
|
4 |
|
|
|
29 |
class BasicAgent:
|
30 |
def __init__(self):
|
31 |
print("BasicAgent initialized.")
|
32 |
+
self.llm = HuggingFaceEndpoint(
|
33 |
+
repo_id="Qwen/Qwen2.5-Coder-32B-Instruct",
|
34 |
+
huggingfacehub_api_token=HUGGINGFACEHUB_API_TOKEN,
|
35 |
+
)
|
36 |
self.chat = ChatHuggingFace(llm=self.llm, verbose=True)
|
37 |
self.tools = [
|
38 |
+
multiply, add, subtract, power, divide, modulus,
|
39 |
+
square_root, floor_divide, absolute, logarithm,
|
40 |
+
exponential, web_search, roman_calculator_converter,
|
41 |
+
get_current_time_in_timezone
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
42 |
]
|
43 |
self.chat_with_tools = self.chat.bind_tools(self.tools)
|
44 |
print(f"Total tools available: {len(self.tools)}")
|
45 |
|
46 |
+
async def answer(self, question: str) -> str:
|
|
|
47 |
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
|
|
|
|
48 |
messages = [HumanMessage(content=question)]
|
49 |
+
response = await asyncio.to_thread(self.chat_with_tools.invoke, {"messages": messages})
|
50 |
+
return response['messages'][-1].content[14:]
|
51 |
+
|
52 |
+
async def run_agent_async(agent, questions_data):
|
53 |
+
results_log, answers_payload = [], []
|
54 |
+
tasks = []
|
55 |
+
|
56 |
+
for item in questions_data:
|
57 |
+
task_id = item.get("task_id")
|
58 |
+
question = item.get("question")
|
59 |
+
if not task_id or question is None:
|
60 |
+
print(f"Skipping item with missing task_id or question: {item}")
|
61 |
+
continue
|
62 |
+
tasks.append((task_id, question))
|
63 |
+
|
64 |
+
async def process(task_id, question):
|
65 |
+
try:
|
66 |
+
answer = await agent.answer(question)
|
67 |
+
return task_id, question, answer, None
|
68 |
+
except Exception as e:
|
69 |
+
return task_id, question, None, str(e)
|
70 |
+
|
71 |
+
results = await asyncio.gather(*(process(tid, q) for tid, q in tasks))
|
72 |
+
for tid, question, answer, error in results:
|
73 |
+
if error:
|
74 |
+
print(f"Error running agent on task {tid}: {error}")
|
75 |
+
results_log.append({"Task ID": tid, "Question": question, "Submitted Answer": f"AGENT ERROR: {error}"})
|
76 |
+
else:
|
77 |
+
answers_payload.append({"task_id": tid, "submitted_answer": answer})
|
78 |
+
results_log.append({"Task ID": tid, "Question": question, "Submitted Answer": answer})
|
79 |
+
|
80 |
+
return results_log, answers_payload
|
81 |
+
|
82 |
|
83 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
84 |
"""
|