Spaces:
Sleeping
Sleeping
Upload app.py
Browse files
app.py
ADDED
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# π§° Required libraries
|
2 |
+
from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, tool
|
3 |
+
from huggingface_hub import InferenceClient
|
4 |
+
import requests
|
5 |
+
from bs4 import BeautifulSoup
|
6 |
+
import json
|
7 |
+
import os
|
8 |
+
import gradio as gr
|
9 |
+
|
10 |
+
HF_TOKEN = os.getenv("HF_API_KEY")
|
11 |
+
client = InferenceClient(token=HF_TOKEN)
|
12 |
+
|
13 |
+
# π οΈ Custom tool to get Hugging Face top daily paper
|
14 |
+
@tool
|
15 |
+
def get_hugging_face_top_daily_paper() -> str:
|
16 |
+
"""
|
17 |
+
Returns the title of the most upvoted paper on Hugging Face daily papers.
|
18 |
+
"""
|
19 |
+
try:
|
20 |
+
url = "https://huggingface.co/papers"
|
21 |
+
response = requests.get(url)
|
22 |
+
response.raise_for_status()
|
23 |
+
soup = BeautifulSoup(response.content, "html.parser")
|
24 |
+
|
25 |
+
# Use more general selector
|
26 |
+
containers = soup.find_all('div', attrs={'data-props': True})
|
27 |
+
top_paper = ""
|
28 |
+
|
29 |
+
for container in containers:
|
30 |
+
data_props = container.get('data-props', '')
|
31 |
+
if data_props:
|
32 |
+
try:
|
33 |
+
json_data = json.loads(data_props.replace('"', '"'))
|
34 |
+
if 'dailyPapers' in json_data:
|
35 |
+
top_paper = json_data['dailyPapers'][0]['title']
|
36 |
+
break
|
37 |
+
except json.JSONDecodeError:
|
38 |
+
continue
|
39 |
+
|
40 |
+
return top_paper or "No top paper found."
|
41 |
+
except requests.exceptions.RequestException as e:
|
42 |
+
return f"Error occurred while fetching the paper: {e}"
|
43 |
+
|
44 |
+
# π Run the agent with a sample query
|
45 |
+
if __name__ == "__main__":
|
46 |
+
# π€ Load a model from Hugging Face Hub
|
47 |
+
model = HfApiModel(model_id="Qwen/Qwen2.5-Coder-32B-Instruct", api_key=HF_TOKEN)
|
48 |
+
|
49 |
+
# π§ Initialize tools
|
50 |
+
search_tool = DuckDuckGoSearchTool()
|
51 |
+
|
52 |
+
# π§ Build the agent
|
53 |
+
agent = CodeAgent(
|
54 |
+
tools=[search_tool, get_hugging_face_top_daily_paper],
|
55 |
+
model=model,
|
56 |
+
additional_authorized_imports=["requests", "bs4", "json"]
|
57 |
+
)
|
58 |
+
|
59 |
+
# π₯οΈ Web interface
|
60 |
+
def run_agent_interface(query):
|
61 |
+
return agent.run(query)
|
62 |
+
|
63 |
+
gr.Interface(fn=run_agent_interface, inputs="text", outputs="text").launch()
|