Spaces:
Sleeping
Sleeping
| import os | |
| import gradio as gr | |
| from smolagents import CodeAgent, InferenceClientModel, WebSearchTool | |
| HF_USERNAME = "dharshini3" # replace if using another account | |
| class BasicAgent: | |
| def __init__(self): | |
| token = os.getenv("HF_TOKEN") | |
| if not token: | |
| raise ValueError( | |
| "HF_TOKEN secret not found. Add it in Settings > Variables and secrets." | |
| ) | |
| self.model = InferenceClientModel( | |
| api_key=token | |
| ) | |
| self.agent = CodeAgent( | |
| tools=[WebSearchTool()], | |
| model=self.model, | |
| max_steps=5 | |
| ) | |
| def answer(self, question): | |
| try: | |
| result = self.agent.run(question) | |
| return str(result) | |
| except Exception as e: | |
| return f"Error: {e}" | |
| agent = BasicAgent() | |
| def ask_agent(question): | |
| return agent.answer(question) | |
| demo = gr.Interface( | |
| fn=ask_agent, | |
| inputs=gr.Textbox( | |
| lines=3, | |
| placeholder="Ask a question..." | |
| ), | |
| outputs=gr.Textbox(lines=10), | |
| title="My Hugging Face Agent", | |
| description="Simple SmolAgents + WebSearchTool Agent" | |
| ) | |
| if __name__ == "__main__": | |
| demo.launch() |