import streamlit as st import pandas as pd import io from utils import query_agent, detect_delimiter from dotenv import load_dotenv import os # Set Streamlit page configuration st.set_page_config( page_title="HF Data Analyst Agent", page_icon="📊", layout="centered" ) # Load environment variables load_dotenv() # Retrieve API Token from .env or environment variables HUGGINGFACEHUB_API_TOKEN = os.getenv("HUGGINGFACEHUB_API_TOKEN") # --- Custom Styling (Tailwind-like aesthetic using Streamlit's style) --- st.markdown(""" """, unsafe_allow_html=True) # --- Application Title and Setup --- st.markdown('
📊 AI Data Analyst powered by Mistral (HF)
', unsafe_allow_html=True) st.markdown( """ Upload a CSV file and ask natural language questions about your data. This application uses the **Mistral-7B-Instruct-v0.2** model via the Hugging Face Inference Endpoint for fast, accurate analysis. """, unsafe_allow_html=True ) if not HUGGINGFACEHUB_API_TOKEN or HUGGINGFACEHUB_API_TOKEN == "YOUR_HUGGINGFACE_TOKEN_HERE": st.error("⚠️ **API Token Missing!** Please set your `HUGGINGFACEHUB_API_TOKEN` in the `.env` file or as a secret in your Hugging Face Space.") else: # --- File Uploader --- uploaded_file = st.file_uploader( "Upload your CSV file", type=["csv"], accept_multiple_files=False, help="Upload a CSV file. The app automatically detects delimiters (like comma, semicolon, etc.)." ) if uploaded_file is not None: # Read the file content into bytes file_content_bytes = uploaded_file.getvalue() st.markdown('
', unsafe_allow_html=True) st.subheader("Data Preview (First 5 Rows)") try: delimiter = detect_delimiter(file_content_bytes) # Read the DataFrame just for display data_io = io.StringIO(file_content_bytes.decode('utf-8')) df_preview = pd.read_csv(data_io, sep=delimiter) st.dataframe(df_preview.head()) st.write(f"Detected Delimiter: `{delimiter}`") st.write(f"Shape: {df_preview.shape[0]} rows, {df_preview.shape[1]} columns") # --- Analysis Input Box --- st.markdown('
', unsafe_allow_html=True) st.markdown('
', unsafe_allow_html=True) st.subheader("Ask a question about the data") query = st.text_input( "Enter your question here:", placeholder="e.g., What is the average value of the 'Age' column?", key="user_query" ) # --- Analysis Execution --- if st.button("Run Analysis", key="run_analysis_button") and query: with st.spinner("Analyzing data... The Mistral Agent is working hard!"): # Pass the Hugging Face API token to the agent function response = query_agent(file_content_bytes, query, HUGGINGFACEHUB_API_TOKEN) st.success("Analysis Complete!") st.markdown("---") st.subheader("Agent Response") st.markdown(f"**{response}**") elif st.button("Run Analysis", key="run_analysis_button_disabled") and not query: st.warning("Please enter a question to analyze the data.") st.markdown('
', unsafe_allow_html=True) except Exception as e: st.error(f"Could not process the uploaded file or generate preview. Error: {e}")