Spaces:
Build error
Build error
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +164 -38
src/streamlit_app.py
CHANGED
|
@@ -1,40 +1,166 @@
|
|
| 1 |
-
import
|
| 2 |
-
|
| 3 |
-
import pandas as pd
|
| 4 |
import streamlit as st
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
""
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
""
|
| 15 |
-
|
| 16 |
-
num_points = st.slider("Number of points in spiral", 1, 10000, 1100)
|
| 17 |
-
num_turns = st.slider("Number of turns in spiral", 1, 300, 31)
|
| 18 |
-
|
| 19 |
-
indices = np.linspace(0, 1, num_points)
|
| 20 |
-
theta = 2 * np.pi * num_turns * indices
|
| 21 |
-
radius = indices
|
| 22 |
-
|
| 23 |
-
x = radius * np.cos(theta)
|
| 24 |
-
y = radius * np.sin(theta)
|
| 25 |
-
|
| 26 |
-
df = pd.DataFrame({
|
| 27 |
-
"x": x,
|
| 28 |
-
"y": y,
|
| 29 |
-
"idx": indices,
|
| 30 |
-
"rand": np.random.randn(num_points),
|
| 31 |
-
})
|
| 32 |
-
|
| 33 |
-
st.altair_chart(alt.Chart(df, height=700, width=700)
|
| 34 |
-
.mark_point(filled=True)
|
| 35 |
-
.encode(
|
| 36 |
-
x=alt.X("x", axis=None),
|
| 37 |
-
y=alt.Y("y", axis=None),
|
| 38 |
-
color=alt.Color("idx", legend=None, scale=alt.Scale()),
|
| 39 |
-
size=alt.Size("rand", legend=None, scale=alt.Scale(range=[1, 150])),
|
| 40 |
-
))
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from datetime import datetime
|
|
|
|
| 3 |
import streamlit as st
|
| 4 |
+
import pandas as pd
|
| 5 |
+
from streamlit_chartjs.st_chart_component import st_chartjs
|
| 6 |
+
|
| 7 |
+
from api.coingecko_api import get_top_coins, get_historical_prices
|
| 8 |
+
from api.grok_api import get_groq_response
|
| 9 |
+
|
| 10 |
+
# Page config
|
| 11 |
+
st.set_page_config(page_title="Crypto Info Hub", layout="wide")
|
| 12 |
+
|
| 13 |
+
# CSS for chat bubbles and Bootstrap
|
| 14 |
+
st.markdown("""
|
| 15 |
+
<style>
|
| 16 |
+
.user-bubble {
|
| 17 |
+
background-color: #DCF8C6;
|
| 18 |
+
padding: 10px;
|
| 19 |
+
border-radius: 10px;
|
| 20 |
+
margin-bottom: 5px;
|
| 21 |
+
max-width: 75%;
|
| 22 |
+
text-align: left;
|
| 23 |
+
}
|
| 24 |
+
.bot-bubble {
|
| 25 |
+
background-color: #F1F0F0;
|
| 26 |
+
padding: 10px;
|
| 27 |
+
border-radius: 10px;
|
| 28 |
+
margin-bottom: 5px;
|
| 29 |
+
max-width: 75%;
|
| 30 |
+
text-align: left;
|
| 31 |
+
}
|
| 32 |
+
.user-container {
|
| 33 |
+
display: flex;
|
| 34 |
+
justify-content: flex-end;
|
| 35 |
+
}
|
| 36 |
+
.bot-container {
|
| 37 |
+
display: flex;
|
| 38 |
+
justify-content: flex-start;
|
| 39 |
+
}
|
| 40 |
+
</style>
|
| 41 |
+
""", unsafe_allow_html=True)
|
| 42 |
+
|
| 43 |
+
# Title and header
|
| 44 |
+
st.title("Crypto Info Hub")
|
| 45 |
+
st.components.v1.html("""
|
| 46 |
+
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.0/dist/css/bootstrap.min.css">
|
| 47 |
+
<div class="jumbotron bg-primary text-white">
|
| 48 |
+
<h1 class="display-4">Crypto Info Hub</h1>
|
| 49 |
+
<p class="lead">Real-time crypto dashboard with AI chatbot.</p>
|
| 50 |
+
</div>
|
| 51 |
+
""", height=350)
|
| 52 |
+
|
| 53 |
+
# ----- Dashboard: Top 10 Cryptocurrencies -----
|
| 54 |
+
st.header("Top 10 Cryptocurrencies")
|
| 55 |
+
top_coins_full = get_top_coins(per_page=50)
|
| 56 |
+
top10_coins = top_coins_full[:10]
|
| 57 |
+
table_top10 = []
|
| 58 |
+
for coin in top10_coins:
|
| 59 |
+
table_top10.append({
|
| 60 |
+
"Name": coin.get("name"),
|
| 61 |
+
"Symbol": coin.get("symbol").upper(),
|
| 62 |
+
"Price (USD)": coin.get("current_price"),
|
| 63 |
+
"Market Cap": coin.get("market_cap"),
|
| 64 |
+
"24h Volume": coin.get("total_volume")
|
| 65 |
+
})
|
| 66 |
+
st.table(table_top10)
|
| 67 |
+
|
| 68 |
+
# ----- Search Cryptocurrencies -----
|
| 69 |
+
st.header("Search Cryptocurrencies")
|
| 70 |
+
search_query = st.text_input("Search coins by name or symbol:")
|
| 71 |
+
if search_query:
|
| 72 |
+
filtered_coins = [c for c in top_coins_full if search_query.lower() in c.get('name', '').lower() or search_query.lower() in c.get('symbol', '').lower()]
|
| 73 |
+
else:
|
| 74 |
+
filtered_coins = top_coins_full
|
| 75 |
+
|
| 76 |
+
st.subheader("Search Results")
|
| 77 |
+
max_display = 20
|
| 78 |
+
table_search = []
|
| 79 |
+
for coin in filtered_coins[:max_display]:
|
| 80 |
+
table_search.append({
|
| 81 |
+
"Name": coin.get("name"),
|
| 82 |
+
"Symbol": coin.get("symbol").upper(),
|
| 83 |
+
"Price (USD)": coin.get("current_price"),
|
| 84 |
+
"Market Cap": coin.get("market_cap"),
|
| 85 |
+
"24h Volume": coin.get("total_volume")
|
| 86 |
+
})
|
| 87 |
+
st.table(table_search)
|
| 88 |
+
|
| 89 |
+
# ----- Historical Price Chart -----
|
| 90 |
+
st.subheader("Price Trend for Selected Coin")
|
| 91 |
+
# Use filtered list for historical view if search applied, else top10
|
| 92 |
+
hist_coin_ids = [c.get("id") for c in filtered_coins] if search_query else [c.get("id") for c in top10_coins]
|
| 93 |
+
selected_coin = st.selectbox("Select a coin for the historical chart:", hist_coin_ids)
|
| 94 |
+
days = st.slider("Number of past days:", min_value=1, max_value=365, value=30, key="hist_days")
|
| 95 |
+
|
| 96 |
+
if selected_coin:
|
| 97 |
+
prices = get_historical_prices(selected_coin, days=days)
|
| 98 |
+
if prices:
|
| 99 |
+
dates = [datetime.utcfromtimestamp(p[0] / 1000).strftime("%Y-%m-%d") for p in prices]
|
| 100 |
+
values = [p[1] for p in prices]
|
| 101 |
+
chart_data = {
|
| 102 |
+
"labels": dates,
|
| 103 |
+
"datasets": [{
|
| 104 |
+
"label": f"{selected_coin.capitalize()} Price (USD)",
|
| 105 |
+
"data": values,
|
| 106 |
+
"borderColor": "rgba(75, 192, 192, 1)",
|
| 107 |
+
"backgroundColor": "rgba(75, 192, 192, 0.2)",
|
| 108 |
+
"borderWidth": 2,
|
| 109 |
+
"fill": False
|
| 110 |
+
}]
|
| 111 |
+
}
|
| 112 |
+
st_chartjs(data=chart_data, chart_type="line", canvas_height=400)
|
| 113 |
+
else:
|
| 114 |
+
st.write("No historical data available for this coin.")
|
| 115 |
+
|
| 116 |
+
# ----- Compare Multiple Coins -----
|
| 117 |
+
st.header("Compare Multiple Coins")
|
| 118 |
+
coin_ids = [c.get("id") for c in top_coins_full]
|
| 119 |
+
selected_coins = st.multiselect("Select coins to compare:", options=coin_ids, default=["bitcoin", "ethereum"])
|
| 120 |
+
comp_days = st.slider("Select period (days):", min_value=1, max_value=365, value=30, key="comp_days")
|
| 121 |
+
|
| 122 |
+
if selected_coins:
|
| 123 |
+
compare_data = {}
|
| 124 |
+
for coin_id in selected_coins:
|
| 125 |
+
prices = get_historical_prices(coin_id, days=comp_days)
|
| 126 |
+
if prices:
|
| 127 |
+
dates = [datetime.utcfromtimestamp(p[0] / 1000).strftime("%Y-%m-%d") for p in prices]
|
| 128 |
+
values = [p[1] for p in prices]
|
| 129 |
+
compare_data[coin_id.capitalize()] = {"dates": dates, "values": values}
|
| 130 |
+
if compare_data:
|
| 131 |
+
# Create DataFrame for multiple line chart
|
| 132 |
+
df = pd.DataFrame(
|
| 133 |
+
{name: data["values"] for name, data in compare_data.items()},
|
| 134 |
+
index=next(iter(compare_data.values()))["dates"]
|
| 135 |
+
)
|
| 136 |
+
df.index = pd.to_datetime(df.index)
|
| 137 |
+
st.line_chart(df)
|
| 138 |
+
|
| 139 |
+
# ----- AI Chatbot Interface -----
|
| 140 |
+
st.header("AI Chatbot")
|
| 141 |
+
st.write("Ask questions about cryptocurrencies (e.g., \"What is the price of Bitcoin?\").")
|
| 142 |
+
|
| 143 |
+
# Initialize chat history
|
| 144 |
+
if "messages" not in st.session_state:
|
| 145 |
+
st.session_state.messages = []
|
| 146 |
+
|
| 147 |
+
# Render chat history
|
| 148 |
+
for msg in st.session_state.messages:
|
| 149 |
+
if msg['role'] == 'user':
|
| 150 |
+
st.markdown(f"<div class='user-container'><div class='user-bubble'>{msg['content']}</div></div>", unsafe_allow_html=True)
|
| 151 |
+
else:
|
| 152 |
+
st.markdown(f"<div class='bot-container'><div class='bot-bubble'>{msg['content']}</div></div>", unsafe_allow_html=True)
|
| 153 |
+
|
| 154 |
+
# Chat input form
|
| 155 |
+
|
| 156 |
+
|
| 157 |
|
| 158 |
+
with st.form("chat_form"):
|
| 159 |
+
question = st.text_input("Your question:")
|
| 160 |
+
submit = st.form_submit_button("Send")
|
| 161 |
+
if submit and question:
|
| 162 |
+
try:
|
| 163 |
+
answer = get_groq_response(question)
|
| 164 |
+
st.write(answer)
|
| 165 |
+
except Exception as e:
|
| 166 |
+
st.error(f"Error: {e}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|