Update app.py
#345
by rohansh08 - opened
app.py
CHANGED
|
@@ -10,14 +10,67 @@ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
|
| 10 |
|
| 11 |
# --- Basic Agent Definition ---
|
| 12 |
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
class BasicAgent:
|
| 14 |
def __init__(self):
|
| 15 |
-
print("
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
def __call__(self, question: str) -> str:
|
| 17 |
-
print(f"
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 23 |
"""
|
|
|
|
| 10 |
|
| 11 |
# --- Basic Agent Definition ---
|
| 12 |
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
| 13 |
+
from duckduckgo_search import DDGS
|
| 14 |
+
from openai import OpenAI
|
| 15 |
+
|
| 16 |
+
client = OpenAI()
|
| 17 |
+
|
| 18 |
class BasicAgent:
|
| 19 |
def __init__(self):
|
| 20 |
+
print("Smart Agent initialized.")
|
| 21 |
+
|
| 22 |
+
def search_web(self, query):
|
| 23 |
+
try:
|
| 24 |
+
with DDGS() as ddgs:
|
| 25 |
+
results = ddgs.text(query, max_results=5)
|
| 26 |
+
return " ".join([r["body"] for r in results])
|
| 27 |
+
except:
|
| 28 |
+
return ""
|
| 29 |
+
|
| 30 |
def __call__(self, question: str) -> str:
|
| 31 |
+
print(f"Question: {question}")
|
| 32 |
+
|
| 33 |
+
try:
|
| 34 |
+
# 🔍 Step 1: Search
|
| 35 |
+
search_results = self.search_web(question)
|
| 36 |
+
|
| 37 |
+
# 🧠 Step 2: Reason
|
| 38 |
+
prompt = f"""
|
| 39 |
+
You are a smart AI agent.
|
| 40 |
+
|
| 41 |
+
Question:
|
| 42 |
+
{question}
|
| 43 |
+
|
| 44 |
+
Useful information:
|
| 45 |
+
{search_results}
|
| 46 |
+
|
| 47 |
+
Instructions:
|
| 48 |
+
- Think step by step
|
| 49 |
+
- Use the information if relevant
|
| 50 |
+
- Return ONLY the final answer
|
| 51 |
+
- NO explanation
|
| 52 |
+
- NO extra words
|
| 53 |
+
|
| 54 |
+
Answer:
|
| 55 |
+
"""
|
| 56 |
+
|
| 57 |
+
response = client.chat.completions.create(
|
| 58 |
+
model="gpt-4o-mini",
|
| 59 |
+
messages=[{"role": "user", "content": prompt}],
|
| 60 |
+
temperature=0
|
| 61 |
+
)
|
| 62 |
+
|
| 63 |
+
answer = response.choices[0].message.content.strip()
|
| 64 |
+
|
| 65 |
+
# 🧼 Clean output
|
| 66 |
+
answer = answer.replace("Answer:", "").strip()
|
| 67 |
+
|
| 68 |
+
print(f"Final Answer: {answer}")
|
| 69 |
+
return answer
|
| 70 |
+
|
| 71 |
+
except Exception as e:
|
| 72 |
+
print(f"Error: {e}")
|
| 73 |
+
return "unknown"
|
| 74 |
|
| 75 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 76 |
"""
|