Spaces:
Sleeping
Sleeping
Refactor agent interaction and update response handling in BasicAgent
Browse files
agent.py
CHANGED
|
@@ -33,24 +33,11 @@ def extract_text_from_image(image_path: str) -> str:
|
|
| 33 |
|
| 34 |
def create_agent(llm_model: str = "qwen-qwq-32b"):
|
| 35 |
SYSTEM_PROMPT_TEMPLATE = """
|
| 36 |
-
You are
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
- Arxiv for academic paper search
|
| 42 |
-
- Wikipedia for general knowledge
|
| 43 |
-
- Code Interpreter for executing code and performing calculations
|
| 44 |
-
- Image Text Extraction for extracting text from images
|
| 45 |
-
|
| 46 |
-
RULES
|
| 47 |
-
-----
|
| 48 |
-
• Each task expects ONE exact answer.
|
| 49 |
-
• Finish with the line: FINAL ANSWER: <answer>
|
| 50 |
-
─ Use as few words or characters as possible.
|
| 51 |
-
─ When the answer is numeric do NOT use thousands separators or units
|
| 52 |
-
(%, $, etc.) unless the question explicitly asks for them.
|
| 53 |
-
─ Lists must be comma-separated with NO extra spaces.
|
| 54 |
""".strip()
|
| 55 |
llm = Groq(model=llm_model)
|
| 56 |
arxiv_tools = ArxivToolSpec().to_tool_list()
|
|
@@ -73,17 +60,22 @@ def create_agent(llm_model: str = "qwen-qwq-32b"):
|
|
| 73 |
|
| 74 |
|
| 75 |
async def main():
|
| 76 |
-
""
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
|
| 88 |
|
| 89 |
if __name__ == "__main__":
|
|
|
|
| 33 |
|
| 34 |
def create_agent(llm_model: str = "qwen-qwq-32b"):
|
| 35 |
SYSTEM_PROMPT_TEMPLATE = """
|
| 36 |
+
You are a helpful assistant tasked with answering questions using a set of tools.
|
| 37 |
+
Now, I will ask you a question. Report your thoughts, and finish your answer with the following template:
|
| 38 |
+
FINAL ANSWER: [YOUR FINAL ANSWER].
|
| 39 |
+
YOUR FINAL ANSWER should be a number OR as few words as possible OR a comma separated list of numbers and/or strings. If you are asked for a number, don't use comma to write your number neither use units such as $ or percent sign unless specified otherwise. If you are asked for a string, don't use articles, neither abbreviations (e.g. for cities), and write the digits in plain text unless specified otherwise. If you are asked for a comma separated list, Apply the rules above for each element (number or string), ensure there is exactly one space after each comma.
|
| 40 |
+
Your answer should only start with "FINAL ANSWER: ", then follows with the answer.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
""".strip()
|
| 42 |
llm = Groq(model=llm_model)
|
| 43 |
arxiv_tools = ArxivToolSpec().to_tool_list()
|
|
|
|
| 60 |
|
| 61 |
|
| 62 |
async def main():
|
| 63 |
+
agent = create_agent(llm_model="qwen-qwq-32b")
|
| 64 |
+
question = "What year was Rafa Nadal born?"
|
| 65 |
+
response = await agent.run(user_msg=question)
|
| 66 |
+
|
| 67 |
+
# Parse and print final answer
|
| 68 |
+
if isinstance(response, str):
|
| 69 |
+
raw = response
|
| 70 |
+
else:
|
| 71 |
+
raw = str(response)
|
| 72 |
+
|
| 73 |
+
if "FINAL ANSWER:" in raw:
|
| 74 |
+
answer = raw.split("FINAL ANSWER:")[-1].strip()
|
| 75 |
+
else:
|
| 76 |
+
answer = raw.strip()
|
| 77 |
+
|
| 78 |
+
print(f"\nFinal Answer: {answer}")
|
| 79 |
|
| 80 |
|
| 81 |
if __name__ == "__main__":
|
app.py
CHANGED
|
@@ -3,6 +3,8 @@ import gradio as gr
|
|
| 3 |
import requests
|
| 4 |
import inspect
|
| 5 |
import pandas as pd
|
|
|
|
|
|
|
| 6 |
|
| 7 |
# (Keep Constants as is)
|
| 8 |
# --- Constants ---
|
|
@@ -14,12 +16,42 @@ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
|
| 14 |
class BasicAgent:
|
| 15 |
def __init__(self):
|
| 16 |
print("BasicAgent initialized.")
|
|
|
|
| 17 |
|
| 18 |
def __call__(self, question: str) -> str:
|
| 19 |
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
|
| 25 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
|
|
| 3 |
import requests
|
| 4 |
import inspect
|
| 5 |
import pandas as pd
|
| 6 |
+
from agent import create_agent
|
| 7 |
+
import asyncio
|
| 8 |
|
| 9 |
# (Keep Constants as is)
|
| 10 |
# --- Constants ---
|
|
|
|
| 16 |
class BasicAgent:
|
| 17 |
def __init__(self):
|
| 18 |
print("BasicAgent initialized.")
|
| 19 |
+
self.agent = create_agent(llm_model="qwen-qwq-32b")
|
| 20 |
|
| 21 |
def __call__(self, question: str) -> str:
|
| 22 |
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
| 23 |
+
|
| 24 |
+
# Run async agent.run(...) from a sync context (Gradio / HF Spaces safe)
|
| 25 |
+
try:
|
| 26 |
+
loop = asyncio.get_event_loop()
|
| 27 |
+
except RuntimeError:
|
| 28 |
+
# No running loop yet → create one
|
| 29 |
+
loop = asyncio.new_event_loop()
|
| 30 |
+
asyncio.set_event_loop(loop)
|
| 31 |
+
|
| 32 |
+
if loop.is_running():
|
| 33 |
+
# Already inside an event loop (e.g., Gradio)
|
| 34 |
+
future = asyncio.run_coroutine_threadsafe(
|
| 35 |
+
self.agent.run(user_msg=question), loop
|
| 36 |
+
)
|
| 37 |
+
response = future.result()
|
| 38 |
+
else:
|
| 39 |
+
# Safe to run directly
|
| 40 |
+
response = loop.run_until_complete(self.agent.run(user_msg=question))
|
| 41 |
+
|
| 42 |
+
# Extract final answer from response
|
| 43 |
+
if isinstance(response, str):
|
| 44 |
+
raw = response
|
| 45 |
+
else:
|
| 46 |
+
raw = str(response)
|
| 47 |
+
|
| 48 |
+
if "FINAL ANSWER:" in raw:
|
| 49 |
+
answer = raw.split("FINAL ANSWER:")[-1].strip()
|
| 50 |
+
else:
|
| 51 |
+
answer = raw.strip()
|
| 52 |
+
|
| 53 |
+
print(f"Agent returning answer: {answer}")
|
| 54 |
+
return answer
|
| 55 |
|
| 56 |
|
| 57 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|