Arif
Updated app.py to v12
9f22029
raw
history blame
9.05 kB
import streamlit as st
import pandas as pd
import os
# Page configuration
st.set_page_config(
page_title="πŸ“Š LLM Data Analyzer",
page_icon="πŸ“Š",
layout="wide",
initial_sidebar_state="expanded"
)
st.title("πŸ“Š LLM Data Analyzer")
st.write("*Analyze data and chat with AI - Powered by Hugging Face Spaces*")
# Simple AI responses without API calls
def get_ai_response(prompt):
"""Generate simple AI-like responses without external API"""
prompt_lower = prompt.lower()
# Data analysis responses
if "average" in prompt_lower or "mean" in prompt_lower:
return "Based on the data summary, the average values can be calculated from the statistical measures shown. For more detailed analysis, look at the mean values in the data description."
elif "trend" in prompt_lower or "pattern" in prompt_lower:
return "The data shows various patterns. Examine the min, max, and std deviation values to understand the distribution and trends in your dataset."
elif "correlation" in prompt_lower or "relationship" in prompt_lower:
return "To understand relationships between columns, look at how values change together. The standard deviation and percentiles in the summary can give insights."
elif "outlier" in prompt_lower or "unusual" in prompt_lower:
return "Check the min/max values and compare them to the mean and median. Large differences suggest outliers in your data."
elif "summary" in prompt_lower or "overview" in prompt_lower:
return "The data summary shows key statistics including count, mean, standard deviation, min, 25%, 50%, 75%, and max values for each column."
# General chat responses
elif "hello" in prompt_lower or "hi" in prompt_lower:
return "Hello! I'm the LLM Data Analyzer. I can help you understand your data better. Upload a CSV or Excel file and ask me questions about it!"
elif "what can you do" in prompt_lower or "help" in prompt_lower:
return "I can help you: 1) Upload and preview data 2) View statistics 3) Answer questions about your data 4) Have conversations. Try uploading a CSV or Excel file!"
elif "thank" in prompt_lower:
return "You're welcome! Feel free to ask more questions about your data anytime."
else:
return "That's an interesting question! To get the most accurate analysis, please upload your data and ask specific questions about the columns and values. I can then provide detailed insights based on your actual dataset."
# Create tabs
tab1, tab2, tab3 = st.tabs(["πŸ“€ Upload & Analyze", "πŸ’¬ Chat", "πŸ“Š About"])
# ============================================================================
# TAB 1: Upload & Analyze
# ============================================================================
with tab1:
st.header("πŸ“€ Upload and Analyze Data")
st.info("πŸ’‘ Tip: For best results, use smaller files (< 10MB). Try CSV format if Excel gives issues.")
uploaded_file = st.file_uploader(
"Upload a CSV or Excel file",
type=["csv", "xlsx", "xls"],
help="Supported formats: CSV, Excel (Max 200MB)"
)
if uploaded_file is not None:
try:
# Check file size
file_size = uploaded_file.size / (1024 * 1024) # Convert to MB
if file_size > 100:
st.warning(f"⚠️ File is {file_size:.2f}MB. Large files may take longer to process.")
st.success(f"βœ… File uploaded: {uploaded_file.name} ({file_size:.2f}MB)")
# Read file with error handling
try:
if uploaded_file.name.endswith('.csv'):
df = pd.read_csv(uploaded_file, on_bad_lines='skip')
else:
df = pd.read_excel(uploaded_file, engine='openpyxl')
except Exception as read_error:
st.error(f"❌ Error reading file. Try converting to CSV first.")
st.info(f"Details: {str(read_error)[:100]}")
st.stop()
# Validate dataframe
if df.empty:
st.error("❌ File is empty or has no readable data.")
st.stop()
# Display data preview
st.subheader("πŸ“‹ Data Preview")
st.dataframe(df.head(10), use_container_width=True)
# Display statistics
st.subheader("πŸ“Š Data Statistics")
col1, col2, col3 = st.columns(3)
with col1:
st.metric("Rows", df.shape[0])
with col2:
st.metric("Columns", df.shape[1])
with col3:
try:
memory = df.memory_usage(deep=True).sum() / 1024
st.metric("Memory", f"{memory:.2f} KB")
except:
st.metric("Memory", "N/A")
# Detailed statistics
try:
st.write(df.describe().T)
except:
st.info("Could not generate statistics for this data.")
# Ask AI about the data
st.subheader("❓ Ask AI About Your Data")
question = st.text_input(
"What would you like to know about this data?",
placeholder="e.g., What is the average value in column X?",
key="data_question"
)
if question:
response = get_ai_response(question)
st.success("βœ… Analysis Complete")
st.write(response)
except Exception as e:
st.error(f"❌ Error processing file: {str(e)[:100]}")
st.info("Try uploading a smaller file or converting to CSV format.")
# ============================================================================
# TAB 2: Chat
# ============================================================================
with tab2:
st.header("πŸ’¬ Chat with AI Assistant")
st.write("Have a conversation about data analysis and AI.")
# Initialize session state for chat history
if "messages" not in st.session_state:
st.session_state.messages = []
# Display chat history
for message in st.session_state.messages:
with st.chat_message(message["role"]):
st.markdown(message["content"])
# Chat input
user_input = st.text_input(
"Type your message:",
placeholder="Ask me anything...",
key="chat_input"
)
if user_input:
# Add user message immediately
st.session_state.messages.append({"role": "user", "content": user_input})
# Get response
response = get_ai_response(user_input)
# Add assistant message
st.session_state.messages.append({
"role": "assistant",
"content": response
})
# Display latest messages
st.divider()
with st.chat_message("assistant"):
st.markdown(response)
# ============================================================================
# TAB 3: About
# ============================================================================
with tab3:
st.header("ℹ️ About This App")
st.markdown("""
### 🎯 What is this?
**LLM Data Analyzer** is a tool for analyzing data and having conversations about your datasets.
### πŸ”§ Technology Stack
- **Framework:** Streamlit
- **Hosting:** Hugging Face Spaces (Free Tier)
- **Language:** Python
### ⚑ Features
1. **Data Analysis**: Upload CSV/Excel and ask questions about your data
2. **Chat**: Have conversations about data insights
3. **Statistics**: View comprehensive data summaries
### πŸ“ How to Use
1. **Upload Data** - Start by uploading a CSV or Excel file
2. **Preview** - Review your data and statistics
3. **Ask Questions** - Ask about patterns, averages, outliers, etc.
4. **Chat** - Have conversations about your analysis
### 🌐 Powered By
- [Hugging Face](https://huggingface.co/) - AI platform and hosting
- [Streamlit](https://streamlit.io/) - Web framework
- [Pandas](https://pandas.pydata.org/) - Data analysis
### πŸ“– Troubleshooting
**File upload fails?**
- Try converting Excel to CSV first
- Use smaller files (< 10MB)
- Check file format is valid
**Data preview is empty?**
- File may be corrupted
- Try opening in Excel and resaving as CSV
### πŸ”— Links
- [GitHub Repository](https://github.com/Arif-Badhon/LLM-Data-Analyzer)
- [Hugging Face Hub](https://huggingface.co/)
---
**Version:** 1.0 | **Last Updated:** Dec 2025
πŸ’‘ **Note:** This version uses intelligent pattern matching for responses. For more advanced AI features, you can integrate your own Hugging Face API token.
""")