OppaAI / app.py
AIVAHr-project's picture
Upload app.py
f8b8032 verified
# 🧰 Required libraries
from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, tool
from huggingface_hub import InferenceClient
import requests
from bs4 import BeautifulSoup
import json
import os
import gradio as gr
HF_TOKEN = os.getenv("HF_API_KEY")
client = InferenceClient(token=HF_TOKEN)
# πŸ› οΈ Custom tool to get Hugging Face top daily paper
@tool
def get_hugging_face_top_daily_paper() -> str:
"""
Returns the title of the most upvoted paper on Hugging Face daily papers.
"""
try:
url = "https://huggingface.co/papers"
response = requests.get(url)
response.raise_for_status()
soup = BeautifulSoup(response.content, "html.parser")
# Use more general selector
containers = soup.find_all('div', attrs={'data-props': True})
top_paper = ""
for container in containers:
data_props = container.get('data-props', '')
if data_props:
try:
json_data = json.loads(data_props.replace('"', '"'))
if 'dailyPapers' in json_data:
top_paper = json_data['dailyPapers'][0]['title']
break
except json.JSONDecodeError:
continue
return top_paper or "No top paper found."
except requests.exceptions.RequestException as e:
return f"Error occurred while fetching the paper: {e}"
# πŸš€ Run the agent with a sample query
if __name__ == "__main__":
# πŸ€– Load a model from Hugging Face Hub
model = HfApiModel(model_id="Qwen/Qwen2.5-Coder-32B-Instruct", api_key=HF_TOKEN)
# πŸ”§ Initialize tools
search_tool = DuckDuckGoSearchTool()
# 🧠 Build the agent
agent = CodeAgent(
tools=[search_tool, get_hugging_face_top_daily_paper],
model=model,
additional_authorized_imports=["requests", "bs4", "json"]
)
# πŸ–₯️ Web interface
def run_agent_interface(query):
return agent.run(query)
gr.Interface(fn=run_agent_interface, inputs="text", outputs="text").launch()