File size: 3,056 Bytes
a73d60f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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! ✨")