"""
Data Analyst Agent - Streamlit App
Features: Streaming, Audio Input (OpenAI Whisper), Chart Rendering
"""
import streamlit as st
import openai
import base64
import re
import os
import io
import tempfile
import requests
import pandas as pd
from pathlib import Path
# Page config
st.set_page_config(
page_title="Data Analyst Agent for Transmed",
page_icon="š",
layout="wide"
)
# API Keys
OPENAI_API_KEY = "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"
# Temp directory
TEMP_DIR = Path(tempfile.gettempdir()) / "data_analyst"
TEMP_DIR.mkdir(exist_ok=True)
# Custom CSS for stunning UI
st.markdown("""
""", unsafe_allow_html=True)
# Header
st.markdown("""
š Data Analyst Agent
Upload your data ⢠Ask questions in natural language ⢠Get instant insights & visualizations
""", unsafe_allow_html=True)
# Sidebar
with st.sidebar:
st.markdown("## āļø Configuration")
st.markdown("---")
default_key = os.environ.get("POE_API_KEY", "")
api_key = st.text_input(
"š Poe API Key",
type="password",
value=st.session_state.get("api_key", default_key),
help="Enter your Poe API key to connect to the agent"
)
if api_key:
st.session_state.api_key = api_key
bot_name = st.text_input(
"š¤ Bot Name",
value=st.session_state.get("bot_name", "Transmed-Agent"),
help="The name of the Poe bot to use for analysis"
)
if bot_name:
st.session_state.bot_name = bot_name
st.markdown("---")
st.markdown("### šÆ Quick Actions")
if st.button("šļø Clear Conversation", use_container_width=True):
st.session_state.messages = []
st.session_state.uploaded_file_data = None
st.session_state.uploaded_file_name = None
st.session_state.df_preview = None
for f in TEMP_DIR.glob("*"):
try: f.unlink()
except: pass
st.rerun()
st.markdown("---")
st.markdown("""
### š” Tips
- Upload CSV or Excel files
- Ask questions naturally
- Use voice for hands-free input
- Get charts & insights automatically
""")
# Session state
if "messages" not in st.session_state:
st.session_state.messages = []
if "uploaded_file_data" not in st.session_state:
st.session_state.uploaded_file_data = None
if "uploaded_file_name" not in st.session_state:
st.session_state.uploaded_file_name = None
if "df_preview" not in st.session_state:
st.session_state.df_preview = None
# File upload section
st.markdown("### š Upload Your Data")
uploaded_file = st.file_uploader(
"Drag and drop your CSV or Excel file here",
type=["csv", "xlsx", "xls"],
help="Supported formats: CSV, XLSX, XLS"
)
if uploaded_file:
file_bytes = uploaded_file.read()
st.session_state.uploaded_file_data = file_bytes
st.session_state.uploaded_file_name = uploaded_file.name
uploaded_file.seek(0)
try:
if uploaded_file.name.endswith(".csv"):
df = pd.read_csv(uploaded_file)
else:
df = pd.read_excel(uploaded_file)
st.session_state.df_preview = df
# Beautiful data preview
st.markdown(f"""
', unsafe_allow_html=True)
st.markdown('
š Generated Visualization
', unsafe_allow_html=True)
st.image(str(filepath), use_container_width=True)
st.markdown('
', unsafe_allow_html=True)
elif suffix == '.html':
# Render HTML chart (Plotly)
st.markdown('', unsafe_allow_html=True)
st.markdown('
š Interactive Chart
', unsafe_allow_html=True)
html_content = filepath.read_text()
st.components.v1.html(html_content, height=500, scrolling=True)
st.markdown('
', unsafe_allow_html=True)
else:
# Show file info with clickable download link
st.markdown(f"""