Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -9,27 +9,32 @@ openai.api_key = os.getenv("OPENAI_API_KEY")
|
|
| 9 |
|
| 10 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 11 |
|
| 12 |
-
# --- Smart Agent
|
| 13 |
class SmartAgent:
|
| 14 |
def __init__(self):
|
| 15 |
print("SmartAgent initialized.")
|
| 16 |
|
| 17 |
def __call__(self, question: str) -> str:
|
| 18 |
-
print(f"[Agent] Processing question: {question[:
|
| 19 |
-
prompt = f"""
|
| 20 |
-
You are an expert AI agent. Answer the following question clearly and precisely.
|
| 21 |
-
If external data is required (e.g. Wikipedia), use your knowledge base.
|
| 22 |
-
Only return the final answer. Do NOT include 'Answer:' or any extra words.
|
| 23 |
-
|
| 24 |
-
Question:
|
| 25 |
-
{question}
|
| 26 |
-
"""
|
| 27 |
try:
|
| 28 |
response = openai.ChatCompletion.create(
|
| 29 |
model="gpt-4",
|
| 30 |
-
messages=[
|
| 31 |
-
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
)
|
| 34 |
answer = response.choices[0].message["content"].strip()
|
| 35 |
print(f"[Agent] Answer: {answer}")
|
|
@@ -103,6 +108,21 @@ with gr.Blocks() as demo:
|
|
| 103 |
|
| 104 |
run_btn.click(fn=run_and_submit_all, outputs=[status, table])
|
| 105 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 106 |
if __name__ == "__main__":
|
| 107 |
print("Launching GAIA Agent App")
|
|
|
|
| 108 |
demo.launch()
|
|
|
|
|
|
| 9 |
|
| 10 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 11 |
|
| 12 |
+
# --- Improved Smart Agent ---
|
| 13 |
class SmartAgent:
|
| 14 |
def __init__(self):
|
| 15 |
print("SmartAgent initialized.")
|
| 16 |
|
| 17 |
def __call__(self, question: str) -> str:
|
| 18 |
+
print(f"[Agent] Processing question: {question[:60]}...")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
try:
|
| 20 |
response = openai.ChatCompletion.create(
|
| 21 |
model="gpt-4",
|
| 22 |
+
messages=[
|
| 23 |
+
{
|
| 24 |
+
"role": "system",
|
| 25 |
+
"content": (
|
| 26 |
+
"You are a highly logical AI assistant specialized in answering "
|
| 27 |
+
"knowledge and reasoning questions precisely and concisely. "
|
| 28 |
+
"Provide only the final answer, with no extra commentary or labels."
|
| 29 |
+
)
|
| 30 |
+
},
|
| 31 |
+
{"role": "user", "content": question}
|
| 32 |
+
],
|
| 33 |
+
temperature=0.2, # Lower temp for more deterministic answers
|
| 34 |
+
max_tokens=300,
|
| 35 |
+
top_p=1,
|
| 36 |
+
frequency_penalty=0,
|
| 37 |
+
presence_penalty=0,
|
| 38 |
)
|
| 39 |
answer = response.choices[0].message["content"].strip()
|
| 40 |
print(f"[Agent] Answer: {answer}")
|
|
|
|
| 108 |
|
| 109 |
run_btn.click(fn=run_and_submit_all, outputs=[status, table])
|
| 110 |
|
| 111 |
+
# --- Optional local test for debugging ---
|
| 112 |
+
def local_test():
|
| 113 |
+
print("Running local test...")
|
| 114 |
+
agent = SmartAgent()
|
| 115 |
+
test_questions = [
|
| 116 |
+
"How many studio albums did Mercedes Sosa publish between 2000 and 2009?",
|
| 117 |
+
"What is the capital city of France?",
|
| 118 |
+
"List the vegetables in this list: milk, eggs, flour, whole bean coffee, Oreos, sweet potatoes, fresh basil, plums, green beans, rice, corn, bell pepper, whole allspice, acorns, broccoli, celery, zucchini, lettuce, peanuts."
|
| 119 |
+
]
|
| 120 |
+
for q in test_questions:
|
| 121 |
+
ans = agent(q)
|
| 122 |
+
print(f"Q: {q}\nA: {ans}\n{'-'*40}")
|
| 123 |
+
|
| 124 |
if __name__ == "__main__":
|
| 125 |
print("Launching GAIA Agent App")
|
| 126 |
+
local_test()
|
| 127 |
demo.launch()
|
| 128 |
+
|