Surbhi
Initial commit
a73d60f
raw
history blame
3.06 kB
import streamlit as st
import pandas as pd
import textwrap
# Sidebar UI
st.sidebar.title("AI Code Generator 🧠")
st.sidebar.markdown("Generate AI models instantly!")
# Model Selection
model_options = ["KNN", "SVM", "Random Forest", "Decision Tree", "Perceptron"]
model = st.sidebar.selectbox("Choose a Model:", model_options)
# Task Selection
task_options = ["Classification", "Regression"]
task = st.sidebar.selectbox("Choose a Task:", task_options)
# Problem Selection based on Task and Model
problems = {
"Classification": {
"KNN": ["Disease Prediction", "Spam Detection"],
"SVM": ["Image Recognition", "Text Classification"],
"Random Forest": ["Fraud Detection", "Customer Segmentation"],
"Decision Tree": ["Loan Approval", "Churn Prediction"],
"Perceptron": ["Handwritten Digit Recognition", "Sentiment Analysis"]
},
"Regression": {
"KNN": ["House Price Prediction", "Stock Prediction"],
"SVM": ["Sales Forecasting", "Stock Market Trends"],
"Random Forest": ["Energy Consumption", "Patient Survival Prediction"],
"Decision Tree": ["House Price Estimation", "Revenue Prediction"],
"Perceptron": ["Weather Forecasting", "Traffic Flow Prediction"]
}
}
problem = st.sidebar.selectbox("Choose a Problem:", problems[task][model])
# Generate AI Model Code
def generate_code(model, task, problem):
model_mapping = {
"KNN": "KNeighborsClassifier" if task == "Classification" else "KNeighborsRegressor",
"SVM": "SVC" if task == "Classification" else "SVR",
"Random Forest": "RandomForestClassifier" if task == "Classification" else "RandomForestRegressor",
"Decision Tree": "DecisionTreeClassifier" if task == "Classification" else "DecisionTreeRegressor",
"Perceptron": "Perceptron" if task == "Classification" else "Perceptron"
}
selected_model = model_mapping[model]
template = f"""
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.{model.lower()} import {selected_model}
# Load Dataset (Replace with your own dataset)
data = pd.read_csv('dataset.csv')
X = data.iloc[:, :-1] # Features
y = data.iloc[:, -1] # Target
# Train-Test Split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# Standardize Features (if needed)
scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
X_test = scaler.transform(X_test)
# Model Initialization
model = {selected_model}()
# Training the model
model.fit(X_train, y_train)
# Evaluate Model
accuracy = model.score(X_test, y_test)
print("Model Accuracy:", accuracy)
"""
return textwrap.dedent(template)
code = generate_code(model, task, problem)
st.code(code, language="python")
# Download Buttons
st.download_button("🐍 Download (.py)", code, "ai_model.py")
st.download_button("πŸ““ Download (.ipynb)", code, "ai_model.ipynb")
st.success("Code generated! Download and do magic! ✨")