Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from flask import Flask, request, jsonify
|
| 2 |
+
from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel
|
| 3 |
+
|
| 4 |
+
app = Flask(__name__)
|
| 5 |
+
|
| 6 |
+
def get_tool_instance(tool_name):
|
| 7 |
+
if tool_name == "DuckDuckGoSearchTool":
|
| 8 |
+
return DuckDuckGoSearchTool()
|
| 9 |
+
# Add other tools here (e.g., from LangChain)
|
| 10 |
+
return None
|
| 11 |
+
|
| 12 |
+
@app.route('/run_agent', methods=['POST'])
|
| 13 |
+
def run_agent():
|
| 14 |
+
data = request.get_json()
|
| 15 |
+
query = data.get('query')
|
| 16 |
+
tool_names = data.get('tools',)
|
| 17 |
+
|
| 18 |
+
tools = [get_tool_instance(tool_name) for tool_name in tool_names if get_tool_instance(tool_name)]
|
| 19 |
+
|
| 20 |
+
model = HfApiModel() # Or any other LLM model you want to use
|
| 21 |
+
agent = CodeAgent(tools=tools, model=model)
|
| 22 |
+
|
| 23 |
+
try:
|
| 24 |
+
result = agent.run(query)
|
| 25 |
+
return jsonify({'result': result})
|
| 26 |
+
except Exception as e:
|
| 27 |
+
return jsonify({'result': f"Error: {str(e)}"}), 500
|
| 28 |
+
|
| 29 |
+
if __name__ == '__main__':
|
| 30 |
+
app.run(debug=False) # Important: Change to debug=False for production
|