jarguello76 commited on
Commit
b470a55
·
verified ·
1 Parent(s): 0e06bea

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +60 -152
app.py CHANGED
@@ -1,158 +1,66 @@
1
- import os
2
- import requests
3
- import gradio as gr
4
- import pandas as pd
5
- from smolagents import SmolAgent
6
- from smolagents.models import InferenceClientModel
7
- from smolagents.tools import Tool
8
- from duckduckgo_search import DDGS
9
-
10
- # --- Constants ---
11
- DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
12
-
13
- # --- Custom Search Tool ---
14
- class CustomSearchTool(Tool):
15
- name = "duckduckgo_search"
16
- description = "A simple tool that performs web search using DuckDuckGo."
17
- inputs = {"query": str}
18
- outputs = str
19
-
20
- def run(self, query: str) -> str:
21
- print(f"[SearchTool] Searching DuckDuckGo for: {query}")
22
- try:
23
- with DDGS() as ddgs:
24
- results = ddgs.text(query, max_results=3)
25
- output = "\n".join([r["body"] for r in results if "body" in r])
26
- return output.strip() or "No results found."
27
- except Exception as e:
28
- print(f"[SearchTool] Search failed: {e}")
29
- return f"Search error: {e}"
30
-
31
- # --- BasicAgent Class ---
32
- class BasicAgent(SmolAgent):
33
- def __init__(self):
34
- model = InferenceClientModel("mistralai/Mixtral-8x7B-Instruct-v0.1")
35
- tools = [CustomSearchTool()]
36
- super().__init__(model=model, tools=tools)
37
-
38
- # --- Submission + Evaluation ---
39
- def run_and_submit_all(profile: gr.OAuthProfile | None):
40
- space_id = os.getenv("SPACE_ID")
41
-
42
- if profile:
43
- username = f"{profile.username}"
44
- print(f"User logged in: {username}")
45
- else:
46
- print("User not logged in.")
47
- return "Please Login to Hugging Face with the button.", None
48
-
49
- api_url = DEFAULT_API_URL
50
- questions_url = f"{api_url}/questions"
51
- submit_url = f"{api_url}/submit"
52
-
53
- try:
54
- agent = BasicAgent()
55
- except Exception as e:
56
- print(f"Error instantiating agent: {e}")
57
- return f"Error initializing agent: {e}", None
58
-
59
- agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
60
- print(f"Agent code: {agent_code}")
61
-
62
- # Fetch questions
63
- print(f"Fetching questions from: {questions_url}")
64
- try:
65
- response = requests.get(questions_url, timeout=15)
66
- response.raise_for_status()
67
- questions_data = response.json()
68
- if not questions_data:
69
- return "Fetched questions list is empty or invalid format.", None
70
- except Exception as e:
71
- return f"Error fetching questions: {e}", None
72
-
73
- results_log = []
74
- answers_payload = []
75
-
76
- print(f"Running agent on {len(questions_data)} questions...")
77
- for item in questions_data:
78
- task_id = item.get("task_id")
79
- question_text = item.get("question")
80
- if not task_id or question_text is None:
81
- continue
82
- try:
83
- submitted_answer = agent(question_text)
84
- answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
85
- results_log.append({
86
- "Task ID": task_id,
87
- "Question": question_text,
88
- "Submitted Answer": submitted_answer
89
- })
90
- except Exception as e:
91
- results_log.append({
92
- "Task ID": task_id,
93
- "Question": question_text,
94
- "Submitted Answer": f"AGENT ERROR: {e}"
95
- })
96
 
97
- if not answers_payload:
98
- return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
 
99
 
100
- # Submit
101
- submission_data = {
102
- "username": username.strip(),
103
- "agent_code": agent_code,
104
- "answers": answers_payload
105
- }
106
- try:
107
- response = requests.post(submit_url, json=submission_data, timeout=60)
108
- response.raise_for_status()
109
- result_data = response.json()
110
- final_status = (
111
- f"Submission Successful!\n"
112
- f"User: {result_data.get('username')}\n"
113
- f"Overall Score: {result_data.get('score', 'N/A')}% "
114
- f"({result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')} correct)\n"
115
- f"Message: {result_data.get('message', 'No message received.')}"
116
  )
117
- return final_status, pd.DataFrame(results_log)
118
- except Exception as e:
119
- return f"Submission Failed: {e}", pd.DataFrame(results_log)
120
 
121
- # --- Gradio UI ---
122
- with gr.Blocks() as demo:
123
- gr.Markdown("# Basic Agent Evaluation Runner")
124
- gr.Markdown(
125
- """
126
- **Instructions:**
127
- 1. Clone this space, then modify your agent logic, tools, and setup.
128
- 2. Log in to your Hugging Face account below.
129
- 3. Click "Run Evaluation & Submit All Answers" to start.
130
- ---
131
- **Note:** This is intentionally basic. Improve and optimize your agent for better scores.
132
- """
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
133
  )
134
 
135
- gr.LoginButton()
136
-
137
- run_button = gr.Button("Run Evaluation & Submit All Answers")
138
- status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
139
- results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
140
-
141
- run_button.click(fn=run_and_submit_all, outputs=[status_output, results_table])
142
-
143
- # --- Launch ---
144
- if __name__ == "__main__":
145
- print("\n" + "-" * 30 + " App Starting " + "-" * 30)
146
- space_host = os.getenv("SPACE_HOST")
147
- space_id = os.getenv("SPACE_ID")
148
-
149
- if space_host:
150
- print(f"✅ SPACE_HOST found: {space_host}")
151
- print(f" Runtime URL: https://{space_host}.hf.space")
152
- if space_id:
153
- print(f"✅ SPACE_ID found: {space_id}")
154
- print(f" Repo URL: https://huggingface.co/spaces/{space_id}")
155
- print("-" * 70 + "\n")
156
-
157
- print("Launching Gradio Interface...")
158
- demo.launch(debug=True, share=False)
 
1
+ import datasets
2
+ from langchain.docstore.document import Document
3
+ from langchain.text_splitter import RecursiveCharacterTextSplitter
4
+ from langchain_community.retrievers import BM25Retriever
5
+ from smolagents import Tool, CodeAgent, InferenceClientModel
6
+
7
+ # Define RetrieverTool exactly as in your example
8
+ class RetrieverTool(Tool):
9
+ name = "retriever"
10
+ description = (
11
+ "Uses lexical search to retrieve the parts of transformers documentation most relevant to answer your query."
12
+ )
13
+ inputs = {
14
+ "query": {
15
+ "type": "string",
16
+ "description": "The query to perform. Use affirmative form rather than a question.",
17
+ }
18
+ }
19
+ output_type = "string"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
 
21
+ def __init__(self, docs, **kwargs):
22
+ super().__init__(**kwargs)
23
+ self.retriever = BM25Retriever.from_documents(docs, k=10)
24
 
25
+ def forward(self, query: str) -> str:
26
+ assert isinstance(query, str), "Query must be a string"
27
+ docs = self.retriever.invoke(query)
28
+ return "\nRetrieved documents:\n" + "".join(
29
+ [f"\n\n===== Document {i} =====\n{doc.page_content}" for i, doc in enumerate(docs)]
 
 
 
 
 
 
 
 
 
 
 
30
  )
 
 
 
31
 
32
+ # Replace agent creation inside try block
33
+ try:
34
+ # Load and prepare docs once here
35
+ knowledge_base = datasets.load_dataset("m-ric/huggingface_doc", split="train")
36
+ knowledge_base = knowledge_base.filter(lambda row: row["source"].startswith("huggingface/transformers"))
37
+
38
+ source_docs = [
39
+ Document(page_content=doc["text"], metadata={"source": doc["source"].split("/")[1]})
40
+ for doc in knowledge_base
41
+ ]
42
+
43
+ text_splitter = RecursiveCharacterTextSplitter(
44
+ chunk_size=500,
45
+ chunk_overlap=50,
46
+ add_start_index=True,
47
+ strip_whitespace=True,
48
+ separators=["\n\n", "\n", ".", " ", ""],
49
+ )
50
+ docs_processed = text_splitter.split_documents(source_docs)
51
+
52
+ # Instantiate RetrieverTool with processed docs
53
+ retriever_tool = RetrieverTool(docs_processed)
54
+
55
+ # Instantiate the smolagents CodeAgent with model
56
+ agent = CodeAgent(
57
+ tools=[retriever_tool],
58
+ model=InferenceClientModel(model_id="Qwen/Qwen2.5-Coder-32B-Instruct"),
59
+ max_steps=4,
60
+ verbosity_level=2,
61
+ stream_outputs=True,
62
  )
63
 
64
+ except Exception as e:
65
+ print(f"Error instantiating agent: {e}")
66
+ return f"Error initializing agent: {e}", None