DasariHarshitha commited on
Commit
3a525e0
Β·
verified Β·
1 Parent(s): 4244670

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +90 -48
app.py CHANGED
@@ -1,58 +1,100 @@
1
  import streamlit as st
2
- from datetime import datetime
3
-
4
- # ---------------------------
5
- # Dummy AI Logic (Data Science Tutor)
6
- # ---------------------------
7
- def ds_tutor_response(user_query, chat_history):
8
- """
9
- Returns AI-like answers to Data Science queries.
10
- Simulated without API key.
11
- """
12
- # Simple keyword-based logic for simulation
13
- if "regression" in user_query.lower():
14
- return "πŸ“Š Regression is a supervised ML technique used to predict continuous values. Example: predicting house prices."
15
- elif "classification" in user_query.lower():
16
- return "πŸ” Classification is used when the target variable is categorical (e.g., spam vs not spam)."
17
- elif "neural network" in user_query.lower():
18
- return "🧠 A Neural Network is inspired by the human brain. It has layers of interconnected neurons that learn patterns in data."
19
- elif "pca" in user_query.lower():
20
- return "πŸ“‰ PCA (Principal Component Analysis) is a dimensionality reduction technique that transforms features into fewer components."
21
- elif "accuracy" in user_query.lower():
22
- return "βœ… Accuracy = (Correct Predictions / Total Predictions). But for imbalanced data, metrics like F1-score are better."
23
- else:
24
- return "πŸ€” I'm your Data Science Tutor. Can you clarify your question in Data Science terms?"
25
 
26
  # ---------------------------
27
- # Streamlit UI
28
  # ---------------------------
29
- st.set_page_config("AI Data Science Tutor", layout="centered")
30
- st.title("πŸŽ“ AI Conversational Data Science Tutor")
31
 
32
- st.write("Ask me any Data Science doubt, and I'll help you understand. Memory is enabled, so I remember our conversation.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33
 
34
- # Session state for chat history
35
  if "chat_history" not in st.session_state:
36
  st.session_state.chat_history = []
37
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38
  # Chat UI
39
- for msg in st.session_state.chat_history:
40
- with st.chat_message(msg["role"]):
41
- st.markdown(msg["content"])
42
-
43
- # User input
44
- user_input = st.chat_input("Ask your Data Science question...")
45
-
46
- if user_input:
47
- # Save user message
48
- st.session_state.chat_history.append({"role": "user", "content": user_input})
49
- with st.chat_message("user"):
50
- st.markdown(user_input)
51
-
52
- # Generate AI response
53
- ai_reply = ds_tutor_response(user_input, st.session_state.chat_history)
54
-
55
- # Save AI message
56
- st.session_state.chat_history.append({"role": "assistant", "content": ai_reply})
57
- with st.chat_message("assistant"):
58
- st.markdown(ai_reply)
 
1
  import streamlit as st
2
+ from langchain.chat_models import ChatOpenAI
3
+ from langchain.schema import AIMessage, HumanMessage
4
+ from langchain.memory import ConversationBufferMemory
5
+
6
+ # ---------------------------
7
+ # Streamlit UI Config
8
+ # ---------------------------
9
+ st.set_page_config(page_title="AI Data Science Tutor", layout="wide")
10
+ st.title("πŸ“Š AI Conversational Data Science Tutor")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
 
12
  # ---------------------------
13
+ # Sidebar - Settings
14
  # ---------------------------
15
+ st.sidebar.header("βš™οΈ Settings")
 
16
 
17
+ mode = st.sidebar.radio(
18
+ "Choose Tutor Mode:",
19
+ ("Dummy Tutor (No API Key)", "OpenAI Tutor (API Key Required)")
20
+ )
21
+
22
+ if mode == "OpenAI Tutor (API Key Required)":
23
+ openai_api_key = st.sidebar.text_input("Enter your OpenAI API Key:", type="password")
24
+ else:
25
+ openai_api_key = None
26
+
27
+ # ---------------------------
28
+ # Initialize Memory
29
+ # ---------------------------
30
+ if "memory" not in st.session_state:
31
+ st.session_state.memory = ConversationBufferMemory(return_messages=True)
32
 
 
33
  if "chat_history" not in st.session_state:
34
  st.session_state.chat_history = []
35
 
36
+ # ---------------------------
37
+ # Dummy Tutor Response Logic
38
+ # ---------------------------
39
+ def dummy_tutor_response(user_query):
40
+ q = user_query.lower()
41
+ if "regression" in q:
42
+ return "πŸ“Š Regression is a supervised ML technique used to predict continuous values."
43
+ elif "classification" in q:
44
+ return "πŸ” Classification predicts categorical labels, e.g., spam vs not spam."
45
+ elif "neural" in q or "deep learning" in q:
46
+ return "🧠 Neural Networks consist of layers of neurons that learn patterns from data."
47
+ elif "pca" in q:
48
+ return "πŸ“‰ PCA reduces dimensions while preserving variance."
49
+ elif "accuracy" in q or "precision" in q or "recall" in q:
50
+ return "βœ… Accuracy = correct predictions / total. For imbalanced data, use precision, recall, or F1-score."
51
+ elif "clustering" in q:
52
+ return "πŸ“Œ Clustering groups similar points without labels (unsupervised learning)."
53
+ elif "overfitting" in q:
54
+ return "⚠️ Overfitting means the model memorizes data instead of generalizing."
55
+ else:
56
+ return f"πŸ€” I didn’t fully get that. Can you rephrase your Data Science question? (You asked: {user_query})"
57
+
58
+ # ---------------------------
59
+ # Get AI Tutor Response
60
+ # ---------------------------
61
+ def get_tutor_response(user_query):
62
+ if mode == "Dummy Tutor (No API Key)":
63
+ return dummy_tutor_response(user_query)
64
+
65
+ if mode == "OpenAI Tutor (API Key Required)" and openai_api_key:
66
+ llm = ChatOpenAI(
67
+ model="gpt-4o-mini", # You can also try "gpt-4o" or "gpt-4"
68
+ openai_api_key=openai_api_key,
69
+ temperature=0.5
70
+ )
71
+
72
+ # Add user message to memory
73
+ st.session_state.memory.chat_memory.add_user_message(user_query)
74
+
75
+ # Generate response
76
+ response = llm(st.session_state.memory.chat_memory.messages)
77
+
78
+ # Add AI message to memory
79
+ st.session_state.memory.chat_memory.add_ai_message(response.content)
80
+
81
+ return response.content
82
+
83
+ return "⚠️ Please provide your OpenAI API key in the sidebar."
84
+
85
+ # ---------------------------
86
  # Chat UI
87
+ # ---------------------------
88
+ user_query = st.chat_input("Ask me a Data Science question...")
89
+
90
+ if user_query:
91
+ response = get_tutor_response(user_query)
92
+ st.session_state.chat_history.append(("You", user_query))
93
+ st.session_state.chat_history.append(("Tutor", response))
94
+
95
+ # Display Chat History
96
+ for sender, msg in st.session_state.chat_history:
97
+ if sender == "You":
98
+ st.markdown(f"**πŸ‘©β€πŸ’» {sender}:** {msg}")
99
+ else:
100
+ st.markdown(f"**πŸ€– {sender}:** {msg}")