File size: 6,539 Bytes
de2b822
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
90a72cb
de2b822
 
 
 
 
 
90a72cb
de2b822
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
import importlib
import os
import json
from datetime import datetime
from dotenv import load_dotenv
from openai import OpenAI

# Load environment variables from .env file
load_dotenv()

# Set page title and layout
st.set_page_config(page_title="Data Science Tutor", layout="wide")

# Hide Streamlit's default page navigation menu
st.markdown("""
<style>
/* Hide the default "Pages" menu in the top-left sidebar */
[data-testid="stSidebarNav"] {
    display: none;
}
</style>
""", unsafe_allow_html=True)

# Sidebar with image above the CRISP-DM Steps
st.sidebar.image(
    "data2.jpeg",  # Replace with your file path or URL
    use_container_width=True
)

# Sidebar navigation
st.sidebar.title("CRISP-DM Steps")
sections = {
    "Main Page": None,
    "1. Business Understanding": "1_Business_understanding",
    "2. Data understanding": "2_Data_understanding",
    "3. Data Preparation": "3_Data_preparation",
    "4. Feature Engineering": "4_Feature_engineering",
    "5. Modeling": "5_Modeling",
    "6. Evaluation": "6_Evaluation",
    "7. Deployment & Testing": "7_Deployment",
    "8. ML, Deep Learning & Transformers": "8_Models"
}

# By default, make the first item (Main Page) selected.
selected_section = st.sidebar.radio("Select a topic:", list(sections.keys()), index=0)

# If the user selects β€œMain Page,” just show your introduction content.
if sections[selected_section] is None:
    st.title("πŸš€ Welcome to the Data Science Tutor!")
    st.markdown(
        """
        <div style="color: #2FA4E7; margin-top: 1rem;">
            <h2>About This App</h2>
            <p>
                This application is designed to guide you through the CRISP-DM process
                for data science projects. Each section in the sidebar highlights a
                different step in the process, providing structured lessons, best
                practices, and hands-on examples.
            </p>
            <h3>App Sections</h3>
            <ul>
                <li><strong>1. Business Understanding</strong> – Clarify project objectives, requirements, and success criteria.</li>
                <li><strong>2. Data Understanding</strong> – Explore data sources, structures, and initial insights.</li>
                <li><strong>3. Data Preparation</strong> – Clean, integrate, and transform the data for modeling.</li>
                <li><strong>4. Feature Engineering</strong> – Engineer and select relevant features for better models.</li>
                <li><strong>5. Modeling</strong> – Develop, train, and tune predictive models.</li>
                <li><strong>6. Evaluation</strong> – Assess performance metrics and refine models.</li>
                <li><strong>7. Deployment & Testing</strong> – Deploy models into production environments and validate.</li>
                <li><strong>8. ML, Deep Learning & Transformers</strong> – Delve deeper into advanced methods and architectures.</li>
            </ul>
        </div>
        """,
        unsafe_allow_html=True
    )
else:
    # Otherwise, load the selected module from the pages folder
    module_name = f"pages.{sections[selected_section]}"
    module = importlib.import_module(module_name)
    module.run()


# OpenAI API Section
st.sidebar.title("Ask AI")
api_key = os.getenv("OPENAI_API_KEY")
if not api_key:
    api_key = st.sidebar.text_input("Enter your OpenAI API Key", type="password")

client = OpenAI(api_key=api_key)

# Create side menus for toggles from the pages/ list
st.sidebar.title("Focus Areas")
focus_areas = [
    "Data Cleaning & Wrangling", 
    "Feature Engineering & Selection", 
    "Model Selection & Tuning", 
    "Interpretability & Explainability", 
    "Model Deployment & Monitoring"
]
selected_focus_areas = [area for area in focus_areas if st.sidebar.checkbox(area)]

# Main chat section
st.title("Data Science Tutor Chat")
st.image("https://miro.medium.com/v2/resize:fit:100/format:webp/1*NfE0G4nEj4xX7Z_8dSx83g.png")

# Initialize conversation in the session state
if "messages" not in st.session_state:
    st.session_state["messages"] = [
        {"role": "assistant", "content": "How can I assist you with Data Science today?"}
    ]

# Initialize context prompt added state
if "context_prompt_added" not in st.session_state:
    st.session_state["context_prompt_added"] = False

st.write("---")
st.subheader("Chat")

for msg in st.session_state["messages"]:
    st.chat_message(msg["role"]).write(msg["content"])

if prompt := st.chat_input("Enter your question here:"):
    # Add context to the messages if toggles are selected
    focus_context = ""
    if selected_focus_areas:
        focus_context = f"Focus on {', '.join(selected_focus_areas)} in your response."

    # Add context based on the selected section
    section_context = f"The user is currently viewing the {selected_section} section. "

    # If the context prompt hasn't been added yet, build & inject it once;
    # otherwise, just add the user's raw question.
    if not st.session_state["context_prompt_added"]:
        st.session_state["messages"].append({"role": "user", "content": f"{section_context}{prompt}\n{focus_context}"})
        st.session_state["context_prompt_added"] = True
    else:
        st.session_state["messages"].append({"role": "user", "content": f"{section_context}{prompt}"})

    # Display the latest user message in the chat
    st.chat_message("user").write(st.session_state["messages"][-1]["content"])

    # Now call GPT-4 with the entire conversation
    completion = client.chat.completions.create(
            model="gpt-4",
            messages=st.session_state["messages"]
    )
    response_text = completion.choices[0].message.content.strip()

    st.session_state["messages"].append({"role": "assistant", "content": response_text})
    st.chat_message("assistant").write(response_text)

    # Log the conversation
    log_entry = {
        "timestamp": datetime.now().isoformat(),
        "user_query": prompt,
        "assistant_response": response_text,
        "focus_areas": selected_focus_areas,
        "selected_section": selected_section
    }
    log_file_path = os.path.join("logs", "conversation_logs.json")
    os.makedirs(os.path.dirname(log_file_path), exist_ok=True)
    if os.path.exists(log_file_path):
        with open(log_file_path, "r") as log_file:
            logs = json.load(log_file)
    else:
        logs = []
    logs.append(log_entry)
    with open(log_file_path, "w") as log_file:
        json.dump(logs, log_file, indent=4)