Spaces:
Sleeping
Sleeping
Surbhi
commited on
Commit
Β·
5bb134b
1
Parent(s):
f28fb28
Feature extraction and model training
Browse files
app.py
CHANGED
@@ -66,8 +66,46 @@ dataset_mapping = {
|
|
66 |
dataset_path = dataset_mapping.get(problem, "datasets/spam_detection.csv")
|
67 |
df = pd.read_csv(dataset_path)
|
68 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
69 |
# Display dataset
|
70 |
-
st.subheader("Sample Dataset")
|
71 |
st.write(df.head())
|
72 |
|
73 |
# Preprocessing Steps
|
@@ -163,41 +201,4 @@ if model in ["Random Forest", "Decision Tree"]:
|
|
163 |
sns.barplot(x=importance_df["Importance"], y=importance_df["Feature"], ax=ax)
|
164 |
st.pyplot(fig)
|
165 |
|
166 |
-
# Show and Download Generated Code
|
167 |
-
generated_code = f"""
|
168 |
-
# AI Model Code
|
169 |
-
import pandas as pd
|
170 |
-
from sklearn.model_selection import train_test_split
|
171 |
-
from sklearn.preprocessing import StandardScaler
|
172 |
-
from {model_instance.__module__} import {model_instance.__class__.__name__}
|
173 |
-
|
174 |
-
# Load Data
|
175 |
-
df = pd.read_csv('{dataset_path}')
|
176 |
-
X = df.iloc[:, :-1]
|
177 |
-
y = df.iloc[:, -1]
|
178 |
-
|
179 |
-
# Train/Test Split
|
180 |
-
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
|
181 |
-
|
182 |
-
# Scaling
|
183 |
-
scaler = StandardScaler()
|
184 |
-
X_train = scaler.fit_transform(X_train)
|
185 |
-
X_test = scaler.transform(X_test)
|
186 |
-
|
187 |
-
# Train Model
|
188 |
-
model = {model_instance.__class__.__name__}()
|
189 |
-
model.fit(X_train, y_train)
|
190 |
-
|
191 |
-
# Predict
|
192 |
-
y_pred = model.predict(X_test)
|
193 |
-
print(y_pred)
|
194 |
-
"""
|
195 |
-
|
196 |
-
st.subheader("π Generated Code")
|
197 |
-
st.code(generated_code, language="python")
|
198 |
-
|
199 |
-
# Download buttons
|
200 |
-
st.download_button("π₯ Download Python Script (.py)", generated_code, file_name="ai_model.py", mime="text/x-python")
|
201 |
-
st.download_button("π₯ Download Jupyter Notebook (.ipynb)", json.dumps({"cells": [{"cell_type": "code", "source": generated_code.split("\n"), "metadata": {}}], "metadata": {}, "nbformat": 4, "nbformat_minor": 2}), file_name="ai_model.ipynb", mime="application/json")
|
202 |
-
|
203 |
st.success("Code generated! Download and start using it! π")
|
|
|
66 |
dataset_path = dataset_mapping.get(problem, "datasets/spam_detection.csv")
|
67 |
df = pd.read_csv(dataset_path)
|
68 |
|
69 |
+
# Generated AI Code
|
70 |
+
generated_code = f"""
|
71 |
+
# AI Model Code
|
72 |
+
import pandas as pd
|
73 |
+
from sklearn.model_selection import train_test_split
|
74 |
+
from sklearn.preprocessing import StandardScaler
|
75 |
+
from {model.__module__} import {model.__class__.__name__}
|
76 |
+
|
77 |
+
# Load Data
|
78 |
+
df = pd.read_csv('{dataset_path}')
|
79 |
+
X = df.iloc[:, :-1]
|
80 |
+
y = df.iloc[:, -1]
|
81 |
+
|
82 |
+
# Train/Test Split
|
83 |
+
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
|
84 |
+
|
85 |
+
# Scaling
|
86 |
+
scaler = StandardScaler()
|
87 |
+
X_train = scaler.fit_transform(X_train)
|
88 |
+
X_test = scaler.transform(X_test)
|
89 |
+
|
90 |
+
# Train Model
|
91 |
+
model = {model.__class__.__name__}()
|
92 |
+
model.fit(X_train, y_train)
|
93 |
+
|
94 |
+
# Predict
|
95 |
+
y_pred = model.predict(X_test)
|
96 |
+
print(y_pred)
|
97 |
+
"""
|
98 |
+
|
99 |
+
# Display AI Code
|
100 |
+
st.subheader("π Generated AI Model Code")
|
101 |
+
st.code(generated_code, language="python")
|
102 |
+
|
103 |
+
# Download Buttons (Top of UI)
|
104 |
+
st.download_button("π₯ Download Python Script (.py)", generated_code, file_name="ai_model.py", mime="text/x-python")
|
105 |
+
st.download_button("π₯ Download Jupyter Notebook (.ipynb)", json.dumps({"cells": [{"cell_type": "code", "source": generated_code.split("\n"), "metadata": {}}], "metadata": {}, "nbformat": 4, "nbformat_minor": 2}), file_name="ai_model.ipynb", mime="application/json")
|
106 |
+
|
107 |
# Display dataset
|
108 |
+
st.subheader("π Sample Dataset")
|
109 |
st.write(df.head())
|
110 |
|
111 |
# Preprocessing Steps
|
|
|
201 |
sns.barplot(x=importance_df["Importance"], y=importance_df["Feature"], ax=ax)
|
202 |
st.pyplot(fig)
|
203 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
204 |
st.success("Code generated! Download and start using it! π")
|