Arif commited on
Commit
814316f
Β·
1 Parent(s): 48d7cad

Adding all to hf branch

Browse files
Files changed (2) hide show
  1. app.py +253 -0
  2. requirements.txt +9 -0
app.py ADDED
@@ -0,0 +1,253 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import pandas as pd
3
+ import numpy as np
4
+ from llama_cpp import Llama
5
+ from huggingface_hub import hf_hub_download
6
+ import io
7
+ import os
8
+
9
+ # Page configuration
10
+ st.set_page_config(
11
+ page_title="πŸ“Š LLM Data Analyzer",
12
+ page_icon="πŸ“Š",
13
+ layout="wide",
14
+ initial_sidebar_state="expanded"
15
+ )
16
+
17
+ # Custom CSS for better UI
18
+ st.markdown("""
19
+ <style>
20
+ .main {
21
+ padding: 0rem 1rem;
22
+ }
23
+ .stTabs [data-baseweb="tab-list"] {
24
+ gap: 2px;
25
+ }
26
+ </style>
27
+ """, unsafe_allow_html=True)
28
+
29
+ # Title and description
30
+ st.title("πŸ“Š LLM Data Analyzer")
31
+ st.markdown("""
32
+ Analyze your CSV/Excel files and chat with an AI assistant powered by Llama 2.
33
+ This app runs on **free Hugging Face CPU** - response time ~5-10 seconds per query.
34
+ """)
35
+
36
+ # Cache model loading to avoid reloading
37
+ @st.cache_resource
38
+ def load_llm_model():
39
+ """Load Llama 2 model from Hugging Face Hub"""
40
+ st.info("πŸ“₯ Downloading model (first time only, ~4GB)... This may take 2-3 minutes.")
41
+
42
+ try:
43
+ model_path = hf_hub_download(
44
+ repo_id="TheBloke/Llama-2-7B-Chat-GGUF",
45
+ filename="llama-2-7b-chat.Q4_K_M.gguf"
46
+ )
47
+
48
+ llm = Llama(
49
+ model_path=model_path,
50
+ n_ctx=2048,
51
+ n_threads=4,
52
+ n_gpu_layers=0, # CPU only (free tier)
53
+ verbose=False
54
+ )
55
+ return llm
56
+ except Exception as e:
57
+ st.error(f"Error loading model: {e}")
58
+ return None
59
+
60
+ # Load model
61
+ llm = load_llm_model()
62
+
63
+ if llm is None:
64
+ st.error("Failed to load model. Please refresh the page.")
65
+ st.stop()
66
+
67
+ st.success("βœ… Model loaded successfully!")
68
+
69
+ # Create tabs
70
+ tab1, tab2, tab3 = st.tabs(["πŸ“€ Upload & Analyze", "πŸ’¬ Chat", "πŸ“Š About"])
71
+
72
+ # ============================================================================
73
+ # TAB 1: Upload & Analyze
74
+ # ============================================================================
75
+ with tab1:
76
+ st.header("πŸ“€ Upload and Analyze Data")
77
+
78
+ uploaded_file = st.file_uploader(
79
+ "Upload a CSV or Excel file",
80
+ type=["csv", "xlsx", "xls"],
81
+ help="Supported formats: CSV, Excel"
82
+ )
83
+
84
+ if uploaded_file is not None:
85
+ st.success(f"βœ… File uploaded: {uploaded_file.name}")
86
+
87
+ # Read the file
88
+ try:
89
+ if uploaded_file.name.endswith('.csv'):
90
+ df = pd.read_csv(uploaded_file)
91
+ else:
92
+ df = pd.read_excel(uploaded_file)
93
+
94
+ # Display data preview
95
+ st.subheader("πŸ“‹ Data Preview")
96
+ st.dataframe(df.head(10), use_container_width=True)
97
+
98
+ # Display statistics
99
+ st.subheader("πŸ“Š Data Statistics")
100
+ col1, col2, col3 = st.columns(3)
101
+
102
+ with col1:
103
+ st.metric("Rows", df.shape[0])
104
+ with col2:
105
+ st.metric("Columns", df.shape[1])
106
+ with col3:
107
+ st.metric("Memory", f"{df.memory_usage(deep=True).sum() / 1024:.2f} KB")
108
+
109
+ # Detailed statistics
110
+ st.write(df.describe().T)
111
+
112
+ # Ask AI about the data
113
+ st.subheader("❓ Ask AI About Your Data")
114
+ question = st.text_input(
115
+ "What would you like to know about this data?",
116
+ placeholder="e.g., What is the average value in column X?"
117
+ )
118
+
119
+ if question:
120
+ with st.spinner("πŸ€” AI is analyzing your data..."):
121
+ # Create prompt
122
+ data_summary = df.describe().to_string()
123
+ prompt = f"""You are a data analyst expert. You have the following data summary:
124
+
125
+ {data_summary}
126
+
127
+ Column names: {', '.join(df.columns.tolist())}
128
+
129
+ User's question: {question}
130
+
131
+ Please provide a clear, concise analysis based on the data summary. Focus on actionable insights."""
132
+
133
+ # Generate response
134
+ response = llm(
135
+ prompt,
136
+ max_tokens=300,
137
+ stop=["\n\nUser:", "Question:"],
138
+ echo=False,
139
+ temperature=0.7
140
+ )
141
+
142
+ answer = response['choices'][0]['text'].strip()
143
+ st.success("βœ… Analysis Complete")
144
+ st.write(answer)
145
+
146
+ except Exception as e:
147
+ st.error(f"Error reading file: {e}")
148
+
149
+ # ============================================================================
150
+ # TAB 2: Chat
151
+ # ============================================================================
152
+ with tab2:
153
+ st.header("πŸ’¬ Chat with AI Assistant")
154
+ st.write("Have a conversation with Llama 2. Ask anything!")
155
+
156
+ # Initialize session state for chat history
157
+ if "messages" not in st.session_state:
158
+ st.session_state.messages = []
159
+
160
+ # Display chat history
161
+ for message in st.session_state.messages:
162
+ with st.chat_message(message["role"]):
163
+ st.markdown(message["content"])
164
+
165
+ # Chat input
166
+ user_input = st.chat_input("Type your message here...")
167
+
168
+ if user_input:
169
+ # Add user message to history
170
+ st.session_state.messages.append({"role": "user", "content": user_input})
171
+
172
+ # Display user message
173
+ with st.chat_message("user"):
174
+ st.markdown(user_input)
175
+
176
+ # Generate AI response
177
+ with st.chat_message("assistant"):
178
+ with st.spinner("⏳ Generating response..."):
179
+ prompt = f"""You are a helpful AI assistant. The user asks: {user_input}
180
+
181
+ Provide a clear, helpful, and concise response."""
182
+
183
+ response = llm(
184
+ prompt,
185
+ max_tokens=300,
186
+ stop=["\n\nUser:", "User:"],
187
+ echo=False,
188
+ temperature=0.7
189
+ )
190
+
191
+ assistant_message = response['choices'][0]['text'].strip()
192
+ st.markdown(assistant_message)
193
+
194
+ # Add assistant message to history
195
+ st.session_state.messages.append({
196
+ "role": "assistant",
197
+ "content": assistant_message
198
+ })
199
+
200
+ # ============================================================================
201
+ # TAB 3: About
202
+ # ============================================================================
203
+ with tab3:
204
+ st.header("ℹ️ About This App")
205
+
206
+ st.markdown("""
207
+ ### 🎯 What is this?
208
+
209
+ **LLM Data Analyzer** is an AI-powered tool for analyzing data and having conversations with an intelligent assistant.
210
+
211
+ ### πŸ”§ Technology Stack
212
+
213
+ - **Model:** Llama 2 7B (quantized to 4-bit)
214
+ - **Framework:** Llama.cpp (CPU inference)
215
+ - **Frontend:** Streamlit
216
+ - **Hosting:** Hugging Face Spaces (Free Tier)
217
+
218
+ ### ⚑ Performance
219
+
220
+ - **Speed:** ~5-10 tokens per second (free CPU)
221
+ - **Context:** 2048 tokens max
222
+ - **Model Size:** 4GB (quantized)
223
+ - **Hardware:** Free tier CPU
224
+
225
+ ### πŸ’‘ Use Cases
226
+
227
+ 1. **Data Analysis**: Upload CSV/Excel and ask questions
228
+ 2. **Chat**: General conversation with AI
229
+ 3. **Learning**: Understand your data better
230
+
231
+ ### πŸš€ Faster Version Available
232
+
233
+ For **GPU acceleration** (70+ tokens/sec):
234
+ - Run locally on Apple Silicon Mac using MLX
235
+ - Upgrade to Hugging Face PRO tier
236
+ - Deploy on GPU-enabled cloud servers
237
+
238
+ ### πŸ“ Tips
239
+
240
+ - Keep questions focused and specific for best results
241
+ - First request takes longer (model loading)
242
+ - Data is processed locally, not stored on server
243
+
244
+ ### πŸ”— Links
245
+
246
+ - [GitHub Repository](#) - Source code
247
+ - [Hugging Face Hub](#) - Model info
248
+ - [Llama.cpp](#) - Inference engine
249
+
250
+ ---
251
+
252
+ **Version:** 1.0 | **Last Updated:** Dec 2025
253
+ """)
requirements.txt ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ streamlit==1.29.0
2
+ requests==2.31.0
3
+ pandas==2.1.3
4
+ numpy>=1.26.0,<2.0
5
+ openpyxl==3.1.2
6
+ plotly==5.17.0
7
+ python-dotenv==1.0.0
8
+ llama-cpp-python==0.2.35
9
+ huggingface-hub==0.17.3