Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from tools import FreightAgent, EXAMPLE_QUERIES
|
3 |
+
from utils import initialize_database
|
4 |
+
from smolagents import CodeAgent, OpenAIServerModel
|
5 |
+
import os
|
6 |
+
from dotenv import load_dotenv
|
7 |
+
from sql_data import sql_query, get_schema, get_csv_as_dataframe
|
8 |
+
|
9 |
+
# Load environment variables
|
10 |
+
load_dotenv()
|
11 |
+
|
12 |
+
# Initialize the database if it doesn't exist
|
13 |
+
if not os.path.exists("freights.db"):
|
14 |
+
csv_url = "https://huggingface.co/datasets/sasu-SpidR/fretmaritime/resolve/main/freights.csv"
|
15 |
+
initialize_database(csv_url)
|
16 |
+
|
17 |
+
# Create the main agent
|
18 |
+
model_id = "gpt-4.1-mini"
|
19 |
+
model = OpenAIServerModel(model_id=model_id, api_key=os.environ["OPENAI_API_KEY"])
|
20 |
+
|
21 |
+
agent = CodeAgent(tools=[sql_query, get_schema, get_csv_as_dataframe], model=model)
|
22 |
+
|
23 |
+
def run_agent(question: str) -> str:
|
24 |
+
"""
|
25 |
+
Run the agent with the given question.
|
26 |
+
This ReAct Agent can make request to give you information about the freight data.
|
27 |
+
|
28 |
+
Args:
|
29 |
+
question: The question to run the agent with.
|
30 |
+
Returns:
|
31 |
+
The response of the agent.
|
32 |
+
"""
|
33 |
+
return agent.run(question,max_steps=5)
|
34 |
+
|
35 |
+
|
36 |
+
if __name__ == "__main__":
|
37 |
+
gr.Interface(
|
38 |
+
fn=run_agent,
|
39 |
+
inputs=gr.Textbox(lines=7, label="Question"),
|
40 |
+
outputs=gr.Textbox(),
|
41 |
+
title="Freight Agent MCP",
|
42 |
+
description="Ask a question about the freight data in natural language",
|
43 |
+
examples=EXAMPLE_QUERIES if "EXAMPLE_QUERIES" in globals() else None
|
44 |
+
).launch(mcp_server=True)
|
45 |
+
|