File size: 7,110 Bytes
814316f
 
9581ef6
814316f
 
 
 
 
 
 
 
 
 
9581ef6
814316f
9581ef6
814316f
9581ef6
 
814316f
9581ef6
814316f
9581ef6
814316f
 
9581ef6
814316f
9581ef6
 
814316f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9581ef6
 
 
814316f
 
 
 
 
 
 
9581ef6
 
 
 
 
 
 
 
 
 
 
 
 
814316f
 
 
 
 
 
 
 
 
9581ef6
814316f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9581ef6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
814316f
 
 
 
 
 
 
 
 
 
 
 
 
 
9581ef6
 
814316f
9581ef6
814316f
9581ef6
814316f
9581ef6
 
 
814316f
9581ef6
814316f
9581ef6
 
 
 
814316f
9581ef6
814316f
9581ef6
 
814316f
9581ef6
814316f
 
9581ef6
814316f
 
 
 
9581ef6
 
814316f
 
 
 
 
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
205
206
207
208
209
210
import streamlit as st
import pandas as pd
from huggingface_hub import InferenceClient

# 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 Inference API*")

# Initialize HF Inference Client
@st.cache_resource
def get_hf_client():
    """Get Hugging Face Inference Client"""
    try:
        return InferenceClient()
    except Exception as e:
        st.error(f"Error initializing HF client: {e}")
        return None

client = get_hf_client()

if client is None:
    st.error("Failed to initialize Hugging Face client")
    st.stop()

# Create tabs
tab1, tab2, tab3 = st.tabs(["πŸ“€ Upload & Analyze", "πŸ’¬ Chat", "πŸ“Š About"])

# ============================================================================
# TAB 1: Upload & Analyze
# ============================================================================
with tab1:
    st.header("πŸ“€ Upload and Analyze Data")
    
    uploaded_file = st.file_uploader(
        "Upload a CSV or Excel file",
        type=["csv", "xlsx", "xls"],
        help="Supported formats: CSV, Excel"
    )
    
    if uploaded_file is not None:
        st.success(f"βœ… File uploaded: {uploaded_file.name}")
        
        try:
            if uploaded_file.name.endswith('.csv'):
                df = pd.read_csv(uploaded_file)
            else:
                df = pd.read_excel(uploaded_file)
            
            # 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:
                st.metric("Memory", f"{df.memory_usage(deep=True).sum() / 1024:.2f} KB")
            
            # Detailed statistics
            st.write(df.describe().T)
            
            # 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?"
            )
            
            if question:
                with st.spinner("πŸ€” AI is analyzing your data..."):
                    try:
                        data_summary = df.describe().to_string()
                        prompt = f"""You are a data analyst expert. You have the following data summary:

{data_summary}

Column names: {', '.join(df.columns.tolist())}

User's question: {question}

Please provide a clear, concise analysis based on the data summary."""
                        
                        # Use Hugging Face Inference API
                        response = client.text_generation(
                            prompt,
                            max_new_tokens=300,
                            temperature=0.7,
                        )
                        
                        st.success("βœ… Analysis Complete")
                        st.write(response)
                    except Exception as e:
                        st.error(f"Error analyzing data: {e}")
        
        except Exception as e:
            st.error(f"Error reading file: {e}")

# ============================================================================
# TAB 2: Chat
# ============================================================================
with tab2:
    st.header("πŸ’¬ Chat with AI Assistant")
    st.write("Have a conversation with an AI assistant powered by Hugging Face.")
    
    # 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.chat_input("Type your message here...")
    
    if user_input:
        # Add user message to history
        st.session_state.messages.append({"role": "user", "content": user_input})
        
        # Display user message
        with st.chat_message("user"):
            st.markdown(user_input)
        
        # Generate AI response
        with st.chat_message("assistant"):
            with st.spinner("⏳ Generating response..."):
                try:
                    prompt = f"User: {user_input}\n\nAssistant:"
                    
                    response = client.text_generation(
                        prompt,
                        max_new_tokens=300,
                        temperature=0.7,
                    )
                    
                    assistant_message = response.strip()
                    st.markdown(assistant_message)
                    
                    # Add assistant message to history
                    st.session_state.messages.append({
                        "role": "assistant",
                        "content": assistant_message
                    })
                except Exception as e:
                    st.error(f"Error generating response: {e}")

# ============================================================================
# TAB 3: About
# ============================================================================
with tab3:
    st.header("ℹ️ About This App")
    
    st.markdown("""
    ### 🎯 What is this?
    
    **LLM Data Analyzer** is an AI-powered tool for analyzing data and having conversations with an intelligent assistant.
    
    ### πŸ”§ Technology Stack
    
    - **Framework:** Streamlit
    - **AI Engine:** Hugging Face Inference API
    - **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 with an AI assistant
    3. **Statistics**: View data summaries and insights
    
    ### πŸ“ How to Use
    
    1. **Upload Data** - Start by uploading a CSV or Excel file
    2. **Preview** - Review your data and statistics
    3. **Ask Questions** - Get AI-powered analysis
    4. **Chat** - Have follow-up conversations
    
    ### 🌐 Powered By
    
    - [Hugging Face](https://huggingface.co/) - AI models and hosting
    - [Streamlit](https://streamlit.io/) - Web framework
    
    ### πŸ“– Quick Tips
    
    - Keep questions focused and specific for best results
    - Responses may take a few seconds
    - Data is processed locally, not stored on server
    
    ### πŸ”— Links
    
    - [GitHub Repository](https://github.com/Arif-Badhon/LLM-Data-Analyzer)
    - [Hugging Face Hub](https://huggingface.co/)
    
    ---
    
    **Version:** 1.0 | **Last Updated:** Dec 2025
    """)