Spaces:
Sleeping
Sleeping
Add app file
Browse files
app.py
ADDED
|
@@ -0,0 +1,119 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from __future__ import annotations
|
| 2 |
+
|
| 3 |
+
from functools import lru_cache
|
| 4 |
+
import json
|
| 5 |
+
|
| 6 |
+
import gradio as gr
|
| 7 |
+
|
| 8 |
+
from agent import Agent
|
| 9 |
+
from config import AppConfig, load_environment
|
| 10 |
+
from model import ModelRunner
|
| 11 |
+
from prompts import DEFAULT_SYSTEM_PROMPT
|
| 12 |
+
from tools import build_default_tool_registry
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
@lru_cache(maxsize=1)
|
| 16 |
+
def get_agent() -> Agent:
|
| 17 |
+
api_key = load_environment()
|
| 18 |
+
runner = ModelRunner.load(
|
| 19 |
+
model_name=AppConfig.model_name,
|
| 20 |
+
api_key=api_key,
|
| 21 |
+
base_url=AppConfig.base_url,
|
| 22 |
+
)
|
| 23 |
+
tools = build_default_tool_registry()
|
| 24 |
+
return Agent(runner=runner, tools=tools, system_prompt=DEFAULT_SYSTEM_PROMPT)
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
def run_agent(task: str, max_steps: int, temperature: float) -> tuple[str, str]:
|
| 28 |
+
agent = get_agent()
|
| 29 |
+
result, trace = agent.run(task, max_steps=int(max_steps), temperature=temperature, verbose=False)
|
| 30 |
+
response = "" if result is None else str(result)
|
| 31 |
+
trace_text = json.dumps(trace, indent=2, ensure_ascii=False)
|
| 32 |
+
return response, trace_text
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
def build_demo() -> gr.Blocks:
|
| 36 |
+
custom_css = """
|
| 37 |
+
@import url('https://fonts.googleapis.com/css2?family=Inter:opsz,wght@14..32,300;14..32,400;14..32,500;14..32,600;14..32,700&display=swap');
|
| 38 |
+
|
| 39 |
+
/* ========== BASE ========== */
|
| 40 |
+
*, *::before, *::after {
|
| 41 |
+
font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', Helvetica, sans-serif !important;
|
| 42 |
+
box-sizing: border-box !important;
|
| 43 |
+
}
|
| 44 |
+
"""
|
| 45 |
+
|
| 46 |
+
with gr.Blocks(theme=gr.themes.Base(), title="Financial Agent", css=custom_css) as demo:
|
| 47 |
+
gr.Markdown(
|
| 48 |
+
"""
|
| 49 |
+
# Financial Agent
|
| 50 |
+
Uses **LLaMA 3.3 70B** via NVIDIA NIM (OpenAI-compatible API).
|
| 51 |
+
Ask for stock prices, company profiles, and more! The agent can use tools to fetch real-time data and provide accurate responses.
|
| 52 |
+
"""
|
| 53 |
+
)
|
| 54 |
+
|
| 55 |
+
with gr.Row():
|
| 56 |
+
with gr.Column(scale=1):
|
| 57 |
+
task_input = gr.Textbox(
|
| 58 |
+
label="User task",
|
| 59 |
+
value="",
|
| 60 |
+
lines=3,
|
| 61 |
+
placeholder="Ask for a stock price, e.g. Tesla, Apple, Nvidia...",
|
| 62 |
+
)
|
| 63 |
+
with gr.Row():
|
| 64 |
+
max_steps_slider = gr.Slider(
|
| 65 |
+
minimum=1,
|
| 66 |
+
maximum=20,
|
| 67 |
+
value=5,
|
| 68 |
+
step=1,
|
| 69 |
+
label="Max iterations",
|
| 70 |
+
)
|
| 71 |
+
temperature_slider = gr.Slider(
|
| 72 |
+
minimum=0.0,
|
| 73 |
+
maximum=1.0,
|
| 74 |
+
value=0.3,
|
| 75 |
+
step=0.05,
|
| 76 |
+
label="Temperature",
|
| 77 |
+
)
|
| 78 |
+
run_button = gr.Button("Run agent", variant="primary")
|
| 79 |
+
|
| 80 |
+
with gr.Column(scale=1):
|
| 81 |
+
response_output = gr.Textbox(label="Final response", lines=3)
|
| 82 |
+
trace_output = gr.Code(label="Tool trace", language="json", elem_id="trace_output")
|
| 83 |
+
|
| 84 |
+
run_button.click(
|
| 85 |
+
fn=run_agent,
|
| 86 |
+
inputs=[task_input, max_steps_slider, temperature_slider],
|
| 87 |
+
outputs=[response_output, trace_output],
|
| 88 |
+
)
|
| 89 |
+
|
| 90 |
+
gr.Markdown(
|
| 91 |
+
"""
|
| 92 |
+
# Agent User Guide
|
| 93 |
+
|
| 94 |
+
| If you want to... | Example prompt |
|
| 95 |
+
|------------------|----------------|
|
| 96 |
+
| Know the latest price of a stock | "What's the current price of Apple?" |
|
| 97 |
+
| Get company information | "Tell me about Tesla's business sector and industry" |
|
| 98 |
+
| Build a low-risk portfolio | "Create a minimum variance portfolio with Microsoft, Google, and Amazon" |
|
| 99 |
+
| Maximize return for the risk taken | "Give me the best risk-return portfolio using Apple, Nvidia, and Meta" |
|
| 100 |
+
| Reduce downside risk compared to the S&P500 | "Build a portfolio that minimizes losses relative to the S&P500 using these 5 stocks" |
|
| 101 |
+
| Check Mexican CETES rates | "What's the 28-day CETES rate today?" |
|
| 102 |
+
| Know monthly inflation in Mexico | "What was Mexico's monthly inflation last month?" |
|
| 103 |
+
| Know annual inflation in Mexico | "What's the current annual inflation rate in Mexico?" |
|
| 104 |
+
| Get the UDI value in Mexico | "What is the UDI value today?" |
|
| 105 |
+
| Check Mexican TIE interest rates | "Show me the 91-day TIE rate" |
|
| 106 |
+
| Know Mexico's central bank interest rate | "What is Mexico's target interest rate right now?" |
|
| 107 |
+
| Get cross-currency exchange rates | "What's the current exchange rate for EUR/USD?" |
|
| 108 |
+
| Analyze news sentiment for a stock | "What is the market sentiment around Tesla right now?" |
|
| 109 |
+
|
| 110 |
+
"""
|
| 111 |
+
)
|
| 112 |
+
|
| 113 |
+
return demo
|
| 114 |
+
|
| 115 |
+
|
| 116 |
+
demo = build_demo()
|
| 117 |
+
|
| 118 |
+
if __name__ == "__main__":
|
| 119 |
+
demo.launch()
|