File size: 12,567 Bytes
c3c3195
 
3836bc2
 
c3c3195
 
 
3836bc2
c3c3195
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ebbb600
 
 
c3c3195
 
ebbb600
 
 
 
 
 
 
 
 
 
 
 
c3c3195
 
 
ebbb600
 
 
c3c3195
 
301288a
c3c3195
ebbb600
c3c3195
ebbb600
c3c3195
ebbb600
c3c3195
301288a
ebbb600
 
 
 
c3c3195
301288a
 
 
c3c3195
ebbb600
c3c3195
ebbb600
c3c3195
ebbb600
c3c3195
 
ebbb600
 
 
 
 
 
 
 
 
 
 
 
c3c3195
 
 
 
 
3836bc2
c3c3195
ebbb600
 
 
 
c3c3195
 
 
 
ebbb600
 
c3c3195
ebbb600
c3c3195
3836bc2
ebbb600
3836bc2
c3c3195
 
 
 
 
 
ebbb600
 
 
 
c3c3195
 
ebbb600
 
 
 
 
 
 
 
 
 
 
c3c3195
ebbb600
c3c3195
ebbb600
 
 
 
c3c3195
ebbb600
c3c3195
ebbb600
c3c3195
ebbb600
3836bc2
c3c3195
 
 
ebbb600
3836bc2
 
c3c3195
ebbb600
c3c3195
ebbb600
c3c3195
 
ebbb600
 
 
 
 
c3c3195
ebbb600
 
c3c3195
ebbb600
 
 
c3c3195
ebbb600
 
 
c3c3195
ebbb600
3836bc2
ebbb600
 
 
3836bc2
ebbb600
 
c3c3195
ebbb600
 
c3c3195
3836bc2
ebbb600
c3c3195
ebbb600
 
 
 
c3c3195
c3b2831
ebbb600
 
 
 
 
3836bc2
ebbb600
 
301288a
ebbb600
301288a
ebbb600
 
 
 
 
 
c3c3195
ebbb600
c3c3195
ebbb600
3836bc2
ebbb600
 
c3c3195
 
 
ebbb600
c3c3195
 
ebbb600
 
c3c3195
 
ebbb600
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c3c3195
 
ebbb600
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3836bc2
ebbb600
 
 
 
 
 
 
 
 
 
 
 
 
c3c3195
 
 
 
 
 
 
 
 
3836bc2
 
c3c3195
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
import streamlit as st
import requests
import pandas as pd
import io
import os
from PIL import Image
import time

# Configure page
st.set_page_config(
    page_title="PromptPrepML - Auto ML Data Preprocessing",
    page_icon="πŸ€–",
    layout="wide",
    initial_sidebar_state="expanded"
)

# Custom CSS for better styling
st.markdown("""
<style>
    .main-header {
        font-size: 2.5rem;
        font-weight: bold;
        color: #1f2937;
        text-align: center;
        margin-bottom: 2rem;
    }
    .step-header {
        font-size: 1.5rem;
        font-weight: 600;
        color: #374151;
        margin: 1rem 0;
    }
    .success-box {
        background-color: #f0fdf4;
        border: 1px solid #bbf7d0;
        border-radius: 0.5rem;
        padding: 1rem;
        margin: 1rem 0;
    }
    .info-box {
        background-color: #eff6ff;
        border: 1px solid #bfdbfe;
        border-radius: 0.5rem;
        padding: 1rem;
        margin: 1rem 0;
    }
    .warning-box {
        background-color: #fffbeb;
        border: 1px solid #fed7aa;
        border-radius: 0.5rem;
        padding: 1rem;
        margin: 1rem 0;
    }
</style>
""", unsafe_allow_html=True)

# API base URLs - try deployed backend first, fallback to localhost
DEPLOYED_BACKEND = "https://promptprepml-backend.railway.app"
LOCAL_BACKEND = "http://localhost:8000"

def check_backend_health():
    """Check if backend is running (try deployed first, then local)"""
    backends = [DEPLOYED_BACKEND, LOCAL_BACKEND]
    
    for backend_url in backends:
        try:
            response = requests.get(f"{backend_url}/health", timeout=5)
            if response.status_code == 200:
                st.session_state.backend_url = backend_url
                return True, backend_url
        except:
            continue
    return False, None

def upload_dataset(uploaded_file):
    """Upload dataset to backend"""
    if 'backend_url' not in st.session_state:
        return None, "Backend not connected"
    
    try:
        files = {'file': uploaded_file}
        response = requests.post(f"{st.session_state.backend_url}/upload-dataset", files=files)
        if response.status_code == 200:
            return response.json(), None
        else:
            return None, f"Upload failed: {response.text}"
    except Exception as e:
        return None, f"Upload error: {str(e)}"

def process_pipeline(uploaded_file, prompt):
    """Process dataset through ML pipeline"""
    if 'backend_url' not in st.session_state:
        return None, "Backend not connected"
    
    try:
        files = {'file': uploaded_file}
        data = {'prompt': prompt}
        response = requests.post(f"{st.session_state.backend_url}/process-pipeline", files=files, data=data)
        if response.status_code == 200:
            return response.json(), None
        else:
            return None, f"Processing failed: {response.text}"
    except Exception as e:
        return None, f"Processing error: {str(e)}"

def download_file(filename):
    """Download processed file"""
    if 'backend_url' not in st.session_state:
        return None, "Backend not connected"
    
    try:
        response = requests.get(f"{st.session_state.backend_url}/api/download/{filename}")
        if response.status_code == 200:
            return response.content, None
        else:
            return None, f"Download failed: {response.text}"
    except Exception as e:
        return None, f"Download error: {str(e)}"

def main():
    # Main header
    st.markdown('<h1 class="main-header">πŸ€– PromptPrepML</h1>', unsafe_allow_html=True)
    st.markdown('<p style="text-align: center; color: #6b7280; font-size: 1.1rem;">Convert natural language prompts into ML-ready datasets</p>', unsafe_allow_html=True)
    
    # Check backend health
    backend_healthy, backend_url = check_backend_health()
    
    if not backend_healthy:
        st.error("❌ Backend is not running! Please start the backend:")
        st.code("""
cd promptprepml/backend
venv\\Scripts\\activate
python app/main.py

# OR wait for deployed backend to be ready
""")
        st.info("πŸš€ **Deploying backend to cloud...** This will make the app work standalone!")
        return
    
    st.success(f"βœ… Backend connected at: {backend_url}")
    
    # Sidebar for navigation
    st.sidebar.title("πŸ“‹ Processing Steps")
    
    # Initialize session state
    if 'step' not in st.session_state:
        st.session_state.step = 'upload'
    if 'upload_result' not in st.session_state:
        st.session_state.upload_result = None
    if 'processing_result' not in st.session_state:
        st.session_state.processing_result = None
    
    # Step indicators
    steps = ['πŸ“€ Upload', 'βš™οΈ Configure', 'πŸš€ Process', 'πŸ“Š Results']
    current_step_index = 0
    
    if st.session_state.step == 'upload':
        current_step_index = 0
    elif st.session_state.step == 'configure':
        current_step_index = 1
    elif st.session_state.step == 'process':
        current_step_index = 2
    elif st.session_state.step == 'results':
        current_step_index = 3
    
    # Display step indicators
    for i, step in enumerate(steps):
        if i < current_step_index:
            st.sidebar.success(f"βœ… {step}")
        elif i == current_step_index:
            st.sidebar.info(f"πŸ”„ {step}")
        else:
            st.sidebar.write(f"⏳ {step}")
    
    # Step 1: Upload Dataset
    if st.session_state.step == 'upload':
        st.markdown('<h2 class="step-header">πŸ“€ Step 1: Upload Dataset</h2>', unsafe_allow_html=True)
        
        uploaded_file = st.file_uploader(
            "Choose a CSV file",
            type=['csv'],
            help="Upload your dataset for preprocessing"
        )
        
        if uploaded_file is not None:
            st.info(f"πŸ“„ File uploaded: `{uploaded_file.name}`")
            
            # Show file preview
            try:
                df = pd.read_csv(uploaded_file)
                st.markdown('<div class="info-box">', unsafe_allow_html=True)
                st.markdown(f"**Dataset Shape:** {df.shape}")
                st.markdown(f"**Columns:** {', '.join(df.columns)}")
                st.dataframe(df.head())
                st.markdown('</div>', unsafe_allow_html=True)
                
                if st.button("πŸš€ Continue to Configuration", type="primary"):
                    # Upload to backend
                    with st.spinner("Uploading dataset..."):
                        result, error = upload_dataset(uploaded_file)
                        if error:
                            st.error(f"❌ Upload failed: {error}")
                        else:
                            st.session_state.upload_result = result
                            st.session_state.step = 'configure'
                            st.rerun()
            except Exception as e:
                st.error(f"❌ Error reading file: {str(e)}")
    
    # Step 2: Configure Processing
    elif st.session_state.step == 'configure':
        st.markdown('<h2 class="step-header">βš™οΈ Step 2: Configure Processing</h2>', unsafe_allow_html=True)
        
        if st.session_state.upload_result:
            file_info = st.session_state.upload_result
            st.markdown('<div class="info-box">', unsafe_allow_html=True)
            st.markdown(f"**File:** {file_info.get('filename', 'Unknown')}")
            st.markdown(f"**Size:** {file_info.get('size', 'Unknown')} bytes")
            st.markdown('</div>', unsafe_allow_html=True)
        
        # Processing options
        prompt = st.text_area(
            "Describe your preprocessing needs:",
            value="Prepare this dataset for machine learning. Handle missing values, remove identifier columns, extract date features, encode categorical variables, and scale numeric features.",
            height=100,
            help="Describe what you want to do with your dataset in natural language"
        )
        
        col1, col2 = st.columns([1, 1])
        with col1:
            if st.button("⬅️ Back", type="secondary"):
                st.session_state.step = 'upload'
                st.rerun()
        
        with col2:
            if st.button("πŸš€ Start Processing", type="primary"):
                if uploaded_file is not None:
                    with st.spinner("Processing dataset... This may take a few minutes."):
                        result, error = process_pipeline(uploaded_file, prompt)
                        if error:
                            st.error(f"❌ Processing failed: {error}")
                        else:
                            st.session_state.processing_result = result
                            st.session_state.step = 'results'
                            st.rerun()
    
    # Step 3: Results
    elif st.session_state.step == 'results':
        st.markdown('<h2 class="step-header">πŸ“Š Step 3: Results</h2>', unsafe_allow_html=True)
        
        if st.session_state.processing_result:
            result = st.session_state.processing_result
            
            # Success message
            st.markdown('<div class="success-box">', unsafe_allow_html=True)
            st.success("βœ… Dataset processed successfully!")
            st.markdown('</div>', unsafe_allow_html=True)
            
            # Results summary
            col1, col2 = st.columns([2, 1])
            
            with col1:
                st.markdown("### πŸ“ˆ Processing Summary")
                
                dataset_info = result.get('dataset_info', {})
                if dataset_info:
                    basic_info = dataset_info.get('basic_info', {})
                    st.markdown(f"- **Original Shape:** {basic_info.get('shape', 'Unknown')}")
                    st.markdown(f"- **Columns:** {basic_info.get('columns', 'Unknown')}")
                
                preprocessing_info = result.get('preprocessing_info', {})
                if preprocessing_info:
                    st.markdown(f"- **Processed Shape:** {preprocessing_info.get('processed_shape', 'Unknown')}")
                
                # Dataset preview
                st.markdown("### πŸ‘€ Dataset Preview")
                preview_data = result.get('preview_data', [])
                if preview_data:
                    df_preview = pd.DataFrame(preview_data)
                    st.dataframe(df_preview)
            
            with col2:
                st.markdown("### πŸ“₯ Download Files")
                
                download_links = [
                    ("Processed Dataset", "processed_dataset.csv"),
                    ("Training Set", "train.csv"),
                    ("Test Set", "test.csv"),
                    ("Pipeline", "pipeline.pkl"),
                    ("EDA Report", "eda_report.html")
                ]
                
                for name, filename in download_links:
                    if st.button(f"πŸ“₯ {name}", key=f"download_{filename}"):
                        with st.spinner(f"Downloading {filename}..."):
                            file_content, error = download_file(filename)
                            if error:
                                st.error(f"❌ Download failed: {error}")
                            else:
                                st.download_button(
                                    label=f"πŸ’Ύ Save {filename}",
                                    data=file_content,
                                    file_name=filename,
                                    mime="application/octet-stream"
                                )
        
        # Action buttons
        col1, col2 = st.columns([1, 1])
        with col1:
            if st.button("πŸ”„ Process New Dataset", type="secondary"):
                # Reset session state
                for key in list(st.session_state.keys()):
                    del st.session_state[key]
                st.session_state.step = 'upload'
                st.rerun()
        
        with col2:
            if st.button("πŸ“ˆ View EDA Report", type="primary"):
                st.info("πŸ“Š EDA Report feature coming soon!")
    
    # Footer
    st.markdown("---")
    st.markdown("""
    <div style="text-align: center; color: #6b7280; margin-top: 2rem;">
        <p><strong>PromptPrepML</strong> - Automated ML Data Preprocessing</p>
        <p><small>Convert natural language prompts into ML-ready datasets</small></p>
    </div>
    """, unsafe_allow_html=True)

if __name__ == "__main__":
    main()