-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
204 lines (179 loc) · 8.42 KB
/
app.py
File metadata and controls
204 lines (179 loc) · 8.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
import streamlit as st
import requests
import json
import pandas as pd
import plotly.io as pio
import base64
import numpy as np
def decode_bdata(bdata: str, dtype: str):
dtype_map = {
'i2': np.int16,
'i4': np.int32,
'f8': np.float64
}
if dtype not in dtype_map:
raise ValueError(f"Unsupported dtype: {dtype}")
byte_data = base64.b64decode(bdata)
return np.frombuffer(byte_data, dtype=dtype_map[dtype]).tolist()
def fix_plotly_template(plotly_json):
"""Fix common Plotly template issues"""
# Navigate to template data if it exists
if "layout" in plotly_json and "template" in plotly_json["layout"] and "data" in plotly_json["layout"]["template"]:
template_data = plotly_json["layout"]["template"]["data"]
# Fix scattermap -> scattermapbox
if "scattermap" in template_data:
template_data["scattermapbox"] = template_data.pop("scattermap")
# Remove any other unsupported trace types that might cause issues
supported_types = {
'barpolar', 'bar', 'box', 'candlestick', 'carpet', 'choroplethmapbox',
'choropleth', 'cone', 'contourcarpet', 'contour', 'densitymapbox',
'funnelarea', 'funnel', 'heatmapgl', 'heatmap', 'histogram2dcontour',
'histogram2d', 'histogram', 'icicle', 'image', 'indicator', 'isosurface',
'mesh3d', 'ohlc', 'parcats', 'parcoords', 'pie', 'pointcloud',
'sankey', 'scatter3d', 'scattercarpet', 'scattergeo', 'scattergl',
'scattermapbox', 'scatterpolargl', 'scatterpolar', 'scatter',
'scattersmith', 'scatterternary', 'splom', 'streamtube', 'sunburst',
'surface', 'table', 'treemap', 'violin', 'volume', 'waterfall'
}
# Remove unsupported types
keys_to_remove = [key for key in template_data.keys() if key not in supported_types]
for key in keys_to_remove:
template_data.pop(key, None)
return plotly_json
def clean_plotly_json(plotly_json):
# First fix template issues
plotly_json = fix_plotly_template(plotly_json)
# Then handle bdata decoding for all possible fields
for trace in plotly_json.get("data", []):
# Common fields that might have bdata encoding
bdata_fields = [
'x', 'y', 'z', 'text', 'hovertext', 'hovertemplate',
'values', 'labels', 'parents', 'ids', 'customdata',
'error_x', 'error_y', 'error_z', 'open', 'high', 'low', 'close'
]
for field in bdata_fields:
val = trace.get(field)
if isinstance(val, dict) and 'bdata' in val and 'dtype' in val:
try:
trace[field] = decode_bdata(val['bdata'], val['dtype'])
except Exception as e:
# Set to appropriate default based on field type
if field in ['text', 'hovertext', 'hovertemplate']:
trace[field] = [] # Empty list for text fields
else:
trace[field] = [] # Empty list for numeric fields
# Handle nested objects that might contain bdata (like marker, line, etc.)
def clean_nested_dict(obj):
if isinstance(obj, dict):
for key, value in obj.items():
if isinstance(value, dict):
if 'bdata' in value and 'dtype' in value:
try:
obj[key] = decode_bdata(value['bdata'], value['dtype'])
except Exception:
obj[key] = []
else:
clean_nested_dict(value)
elif isinstance(value, list):
for item in value:
if isinstance(item, dict):
clean_nested_dict(item)
# Clean nested structures
for nested_field in ['marker', 'line', 'fill', 'error_x', 'error_y', 'error_z']:
if nested_field in trace:
clean_nested_dict(trace[nested_field])
return plotly_json
# Streamlit config
st.set_page_config(page_title="Vanna Full Response Viewer", page_icon="https://vanna.ai/favicon.ico", layout="wide")
# API call function
def call_vanna_agent(agent_id: str, message: str, api_key: str, user_email: str):
try:
response = requests.post(
"https://app.vanna.ai/api/v0/chat_sse",
headers={
"Content-Type": "application/json",
"VANNA-API-KEY": api_key
},
data=json.dumps({
"message": message,
"user_email": user_email,
"agent_id": agent_id,
"acceptable_responses": [
"text", "image", "link", "buttons", "dataframe", "plotly", "sql"
]
}),
stream=True
)
responses = []
for line in response.iter_lines():
if line and line.decode("utf-8").startswith("data:"):
data = json.loads(line.decode("utf-8")[5:].strip())
responses.append(data)
return responses
except Exception as e:
st.error(f"Error: {str(e)}")
return []
# Sidebar
with st.sidebar:
st.header("🔐 Authentication")
api_key = st.text_input("Vanna API Key", type="password", value="", placeholder="Enter your Vanna API key")
user_email = st.text_input("User Email", value="", placeholder="Enter your email")
agent_id = st.text_input("Agent ID", value="", placeholder="Enter your agent ID")
# Main UI
st.title("Vanna Full Response Viewer")
st.markdown("Explore all Vanna response types from a single query")
user_prompt = st.text_area("Your Prompt", placeholder="e.g., Show me usage stats by email")
if st.button("▶️ Run Query", type="primary"):
if not api_key or not user_email or not agent_id or not user_prompt:
st.warning("Please fill in all fields.")
else:
with st.spinner("Calling Vanna agent..."):
responses = call_vanna_agent(agent_id, user_prompt, api_key, user_email)
st.subheader("📦 Response Breakdown")
for idx, data in enumerate(responses):
dtype = data.get("type", "")
st.markdown(f"### Response {idx+1} - `{dtype}`")
if dtype == "text":
st.info(data.get("text", ""))
elif dtype == "image":
image_url = data.get("image_url")
if image_url and isinstance(image_url, str):
st.image(image_url, caption=data.get("caption", ""), use_column_width=True)
else:
st.warning("⚠️ Image URL missing or invalid")
st.json(data)
elif dtype == "link":
st.markdown(f"[{data.get('title')}]({data.get('url')})")
if data.get("description"):
st.caption(data.get("description"))
elif dtype == "buttons":
st.markdown(data.get("text", ""))
for button in data.get("buttons", []):
st.button(button.get("label", "Unnamed Button"))
elif dtype == "dataframe":
df = pd.DataFrame(data["json_table"]["data"])
st.dataframe(df, use_container_width=True)
elif dtype == "plotly":
try:
cleaned = clean_plotly_json(data["json_plotly"])
fig = pio.from_json(json.dumps(cleaned))
st.plotly_chart(fig, use_container_width=True)
# Show raw plotly JSON for reference
with st.expander("View Plotly JSON", expanded=False):
st.json(data["json_plotly"])
except Exception as e:
st.error("⚠️ Plotly render failed")
st.exception(e)
st.json(data["json_plotly"])
elif dtype == "sql":
st.code(data.get("query", ""), language="sql")
elif dtype == "error":
st.error(data.get("error", "Unknown error"))
elif dtype == "end":
st.success("✅ End of response stream")
else:
st.warning(f"Unknown response type: {dtype}")
st.json(data)
# Footer
st.markdown("---")
st.caption("Built with Streamlit • Powered by Vanna AI")