Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +38 -97
src/streamlit_app.py
CHANGED
|
@@ -1,87 +1,32 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
-
import pandas as pd
|
| 3 |
-
import os
|
| 4 |
-
import asyncio
|
| 5 |
-
from llama_index.llms.openai import OpenAI
|
| 6 |
-
from llama_index.core.agent import ReActAgent
|
| 7 |
-
from llama_index.core.workflow import Context
|
| 8 |
-
from llama_index.core.tools import FunctionTool
|
| 9 |
|
| 10 |
-
# ---
|
| 11 |
-
|
| 12 |
|
| 13 |
-
# ---
|
| 14 |
-
#@st.cache_resource
|
| 15 |
-
def initialize_agent():
|
| 16 |
-
advisor = PokemonAdvisorTools()
|
| 17 |
-
tool_methods = [
|
| 18 |
-
advisor.get_card_info,
|
| 19 |
-
advisor.find_grading_opportunities,
|
| 20 |
-
advisor.assess_risk_volatility,
|
| 21 |
-
advisor.get_roi_metrics,
|
| 22 |
-
advisor.get_recent_price_spikes,
|
| 23 |
-
advisor.analyze_set_performance,
|
| 24 |
-
advisor.find_cards_by_artist,
|
| 25 |
-
advisor.get_market_movers
|
| 26 |
-
]
|
| 27 |
-
tools = [FunctionTool.from_defaults(fn=func) for func in tool_methods]
|
| 28 |
-
llm = OpenAI(model="gpt-4o-mini")
|
| 29 |
-
|
| 30 |
-
system_prompt = """### ROLE
|
| 31 |
-
You are the **Poke-Alpha Investment Advisor**, an expert algorithmic trading assistant for the Pokémon TCG market.
|
| 32 |
-
You rely **strictly** on data. You do not guess. You do not hallucinate prices.
|
| 33 |
-
|
| 34 |
-
### TOOL USAGE PROTOCOL
|
| 35 |
-
1. **Verify First:** If a user asks about a card, ALWAYS use `get_card_info` first to ensure it exists.
|
| 36 |
-
2. **Safety Check:** BEFORE recommending ANY card, you MUST call `assess_risk_volatility` (default to 6 months).
|
| 37 |
-
3. **Refining Search:** If the user corrects you (e.g., "Not that card"), use `get_card_info` again.
|
| 38 |
-
4. **Profit Hunting:** If a user asks "What should I buy?", use `find_grading_opportunities` or `get_market_movers`."""
|
| 39 |
-
|
| 40 |
-
agent = ReActAgent(
|
| 41 |
-
tools=tools,
|
| 42 |
-
llm=llm,
|
| 43 |
-
verbose=True,
|
| 44 |
-
system_prompt=system_prompt,
|
| 45 |
-
timeout=120
|
| 46 |
-
)
|
| 47 |
-
# Context should be initialized once
|
| 48 |
-
ctx = Context(agent)
|
| 49 |
-
return agent, ctx
|
| 50 |
-
|
| 51 |
-
agent, global_ctx = initialize_agent()
|
| 52 |
-
|
| 53 |
-
# --- 2. Page Configuration ---
|
| 54 |
-
st.set_page_config(page_title="cAsh Robo-Advisor", page_icon="🤖", layout="wide")
|
| 55 |
-
|
| 56 |
-
# --- 3. Chat Logic Function ---
|
| 57 |
-
async def process_chat(user_message):
|
| 58 |
-
"""Handles the async call to the agent and updates session state."""
|
| 59 |
-
st.session_state.messages.append({"role": "user", "content": user_message})
|
| 60 |
-
|
| 61 |
-
# Placeholder for the assistant's response while it thinks
|
| 62 |
-
with st.chat_message("assistant"):
|
| 63 |
-
response_placeholder = st.empty()
|
| 64 |
-
try:
|
| 65 |
-
# Execute the agent workflow
|
| 66 |
-
response = await agent.run(user_msg=user_message, ctx=global_ctx)
|
| 67 |
-
final_text = str(response)
|
| 68 |
-
response_placeholder.markdown(final_text)
|
| 69 |
-
st.session_state.messages.append({"role": "assistant", "content": final_text})
|
| 70 |
-
except Exception as e:
|
| 71 |
-
error_msg = f"⚠️ **Agent Error:** {str(e)}"
|
| 72 |
-
response_placeholder.markdown(error_msg)
|
| 73 |
-
|
| 74 |
-
# --- 4. Sidebar & UI Styling ---
|
| 75 |
st.title("🤖 cAsh Robo-Advisor")
|
| 76 |
-
st.markdown("
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
|
| 84 |
-
# Sidebar Examples
|
| 85 |
st.sidebar.header("Example Queries")
|
| 86 |
examples = [
|
| 87 |
"What are the top 3 grading opportunities right now?",
|
|
@@ -90,27 +35,23 @@ examples = [
|
|
| 90 |
"Show me profitable cards by Tomokazu Komiya."
|
| 91 |
]
|
| 92 |
|
| 93 |
-
# Sidebar helper: clicking an example sends it immediately
|
| 94 |
for ex in examples:
|
| 95 |
if st.sidebar.button(ex):
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 99 |
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 100 |
|
| 101 |
-
#
|
| 102 |
-
# Display existing messages
|
| 103 |
-
for message in st.session_state.messages: # Display the prior chat messages
|
| 104 |
-
with st.chat_message(message["role"]):
|
| 105 |
-
st.write(message["content"])
|
| 106 |
-
|
| 107 |
-
# User Input
|
| 108 |
-
if st.session_state.messages[-1]["role"] != "assistant":
|
| 109 |
with st.chat_message("assistant"):
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
message = {"role": "assistant", "content": response.response}
|
| 114 |
-
st.session_state.messages.append(message) # Add response to message history
|
| 115 |
|
| 116 |
-
|
|
|
|
| 1 |
import streamlit as st
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
+
# --- 1. Page Configuration ---
|
| 4 |
+
st.set_page_config(page_title="cAsh Robo-Advisor", page_icon="🤖")
|
| 5 |
|
| 6 |
+
# --- 2. Styling & Header ---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
st.title("🤖 cAsh Robo-Advisor")
|
| 8 |
+
st.markdown("""
|
| 9 |
+
**Your AI Quantitative Analyst for Pokemon Cards.**
|
| 10 |
+
|
| 11 |
+
Ask about:
|
| 12 |
+
* **Arbitrage:** "What are the best grading opportunities?"
|
| 13 |
+
* **Risk:** "Is Charizard VMAX a safe investment?"
|
| 14 |
+
* **Trends:** "What is crashing right now?"
|
| 15 |
+
* **Sets:** "How is Evolving Skies performing?"
|
| 16 |
+
""")
|
| 17 |
+
|
| 18 |
+
# --- 3. Initialize Chat History ---
|
| 19 |
+
# Streamlit reruns the whole script on every interaction,
|
| 20 |
+
# so we store messages in 'session_state'.
|
| 21 |
+
if "messages" not in st.session_state:
|
| 22 |
+
st.session_state.messages = []
|
| 23 |
+
|
| 24 |
+
# --- 4. Display Chat History ---
|
| 25 |
+
for message in st.session_state.messages:
|
| 26 |
+
with st.chat_message(message["role"]):
|
| 27 |
+
st.markdown(message["content"])
|
| 28 |
|
| 29 |
+
# --- 5. Sidebar Examples (Equivalent to Gradio Examples) ---
|
| 30 |
st.sidebar.header("Example Queries")
|
| 31 |
examples = [
|
| 32 |
"What are the top 3 grading opportunities right now?",
|
|
|
|
| 35 |
"Show me profitable cards by Tomokazu Komiya."
|
| 36 |
]
|
| 37 |
|
|
|
|
| 38 |
for ex in examples:
|
| 39 |
if st.sidebar.button(ex):
|
| 40 |
+
# This allows the sidebar buttons to act as user inputs
|
| 41 |
+
st.session_state.messages.append({"role": "user", "content": ex})
|
| 42 |
+
# Note: You'd call your 'ask_advisor' logic here for the example buttons
|
| 43 |
+
|
| 44 |
+
# --- 6. Chat Input Logic ---
|
| 45 |
+
if prompt := st.chat_input("Ask your Pokemon Quants advisor..."):
|
| 46 |
+
# Display user message
|
| 47 |
+
with st.chat_message("user"):
|
| 48 |
+
st.markdown(prompt)
|
| 49 |
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 50 |
|
| 51 |
+
# Generate Response (Replace 'ask_advisor' with your actual function)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
with st.chat_message("assistant"):
|
| 53 |
+
# response = ask_advisor(prompt, st.session_state.messages)
|
| 54 |
+
response = f"Analysis for: '{prompt}'. (Connect your ask_advisor function here)"
|
| 55 |
+
st.markdown(response)
|
|
|
|
|
|
|
| 56 |
|
| 57 |
+
st.session_state.messages.append({"role": "assistant", "content": response})
|