Spaces:
Sleeping
Sleeping
Update app.py
Browse files17 unmodified lines181920212223242526272829303110 unmodified lines42434445464748495051525354555657585960615 unmodified lines6768697071727341 unmodified lines11511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514617 unmodified linesfrom smolagents import CodeAgent, toolfrom smolagents.models import LiteLLMModel# (Keep Constants as is)# --- Constants ---DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"load_dotenv()@lru_cache(maxsize=100)def cached_web_search(query: str):with DDGS(timeout=10) as ddgs:return list(ddgs.text(query, max_results=1))10 unmodified lineswikipedia.set_lang("en")try:return wikipedia.summary(query, sentences=2)except wikipedia.DisambiguationError as e:try:return wikipedia.summary(e.options[0], sentences=2)except Exception:return "Wikipedia returned multiple possible pages."except wikipedia.PageError:return "Wikipedia page not found."except Exception as e:return f"Wikipedia Error: {e}"@tooldef web_search(query: str) -> str:5 unmodified lines"""try:results = cached_web_search(query)if not results:return "No search results."41 unmodified linestemperature=0,)self.agent = CodeAgent(model=self.model,tools=[web_search,wikipedia_search,],max_steps=3,add_base_tools=False,verbosity_level=0,)def __call__(self, question: str):prompt = f"""You are a precise QA agent.Rules:1. Answer correctly.2. Use at most ONE web search.3. Only use Wikipedia if the web search is insufficient.4. Never repeat the same search.5. Never search for the same entity twice.6. Do not explain your reasoning.7. Return ONLY the final answer.8. Stop as soon as you know the answer.Question:17 unmodified lines1819202122232425262728293031323334353610 unmodified lines4748495051525354555657585960616263646566676869705 unmodified lines767778798081828341 unmodified lines12512612712812913013113213313413513613713813914014114214314414514614714814915015115217 unmodified linesfrom smolagents import CodeAgent, toolfrom smolagents.models import LiteLLMModelfrom smolagents import ToolCallingAgent, toolfrom ddgs import DDGS# (Keep Constants as is)# --- Constants ---DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"load_dotenv()from functools import lru_cache@lru_cache(maxsize=100)def cached_search(query: str):with DDGS(timeout=10) as ddgs:return list(ddgs.text(query, max_results=1))10 unmodified lineswikipedia.set_lang("en")try:return wikipedia.summary(query, sentences=2)except wikipedia.DisambiguationError as e:try:return wikipedia.summary(e.options[0], sentences=2)except Exception:return "Wikipedia returned multiple pages."except wikipedia.PageError:return "Wikipedia page not found."except Exception as e:return str(e)@tooldef web_search(query: str) -> str:5 unmodified lines"""try:results = cached_search(query)if not results:return "No search results."41 unmodified linestemperature=0,)self.agent = ToolCallingAgent(model=self.model,tools=[web_search,wikipedia_search,],max_steps=3,)def __call__(self, question: str):prompt = f"""You are a factual question answering assistant.Rules:- Use as few tool calls as possible.- Prefer web_search first.- Use wikipedia_search only if needed.- Never repeat the same search.- Stop immediately once you know the answer.- Return ONLY the final answer.Question:
app.py+22-16