Spaces:
Runtime error
Runtime error
File size: 13,345 Bytes
6e99b69 a66ea0f 6e99b69 | 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 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 | import streamlit as st
import pandas as pd
import numpy as np
from preprocessing import TextPreprocessor
from model import MisconceptionClassifier
from visualization import ResultVisualizer
from sklearn.model_selection import train_test_split
# Page config
st.set_page_config(
page_title="Math Misconception Analyzer",
layout="wide"
)
# Initialize components
@st.cache_resource
def load_preprocessor():
return TextPreprocessor()
@st.cache_resource
def load_model():
return MisconceptionClassifier.load_model()
preprocessor = load_preprocessor()
model = load_model()
visualizer = ResultVisualizer()
# Main interface
st.title("Mathematical Misconception Analyzer")
st.write("""
This tool analyzes student responses to identify mathematical misconceptions using advanced NLP techniques.
Upload your data to get detailed insights about common misconceptions and patterns.
""")
# Tabs for different functionalities
tab1, tab2, tab3 = st.tabs(["Analyze New Data", "Model Retraining", "Quick Analysis"])
with tab1:
# File upload for analysis
uploaded_file = st.file_uploader("Upload CSV file for analysis", type=['csv'], key='analysis_upload')
if uploaded_file is not None:
try:
# Load and display data
df = pd.read_csv(uploaded_file)
st.subheader("Data Preview")
st.dataframe(df.head())
# Column selection
text_column = st.selectbox(
"Select the column containing student responses",
df.columns
)
label_column = st.selectbox(
"Select the column containing misconception labels (if available)",
['None'] + list(df.columns)
)
if st.button("Analyze"):
with st.spinner("Processing data..."):
# Extract features with advanced NLP
features = preprocessor.extract_features(df, text_column)
# Show feature analysis
st.subheader("Text Analysis")
col1, col2, col3 = st.columns(3)
with col1:
st.metric("Average Response Length",
f"{features['text_length'].mean():.1f} words")
with col2:
st.metric("Mathematical Terms Density",
f"{features['math_density'].mean():.2f}")
with col3:
st.metric("Total Responses", len(df))
# Show topic modeling results if available
if hasattr(preprocessor, 'topic_keywords'):
st.subheader("Topic Analysis")
topic_fig = visualizer.plot_topic_distribution(
features,
preprocessor.topic_keywords
)
if topic_fig:
st.plotly_chart(topic_fig)
# Train model if labels are available
if label_column != 'None':
# Check if we have enough data
if len(df) < 4:
st.warning("Dataset is too small for reliable model training. Please provide more samples.")
st.stop()
X_train, X_test, y_train, y_test = train_test_split(
df[text_column],
df[label_column],
test_size=0.2,
random_state=42
)
# Train and evaluate model
with st.spinner("Training model..."):
model.train(X_train, y_train)
predictions = model.predict(X_test)
probabilities = model.predict_proba(X_test)
metrics = model.get_model_metrics()
# Display results
st.subheader("Model Performance")
# Show cross-validation results if available
cv_results = visualizer.plot_model_metrics(metrics)
if cv_results is not None:
if isinstance(cv_results, tuple):
for fig in cv_results:
st.plotly_chart(fig)
else:
st.plotly_chart(cv_results)
# Show prediction distribution and confidence
col1, col2 = st.columns(2)
with col1:
st.plotly_chart(
visualizer.plot_misconception_distribution(
pd.Series(predictions)
)
)
with col2:
st.plotly_chart(
visualizer.plot_confidence_scores(
probabilities,
model.pipeline.classes_
)
)
# Show embedding clusters
st.subheader("Response Embedding Analysis")
embeddings = np.array([preprocessor.get_document_embedding(text)
for text in X_test])
embedding_fig = visualizer.plot_embedding_clusters(
embeddings,
y_test
)
if embedding_fig is not None:
st.plotly_chart(embedding_fig)
else:
st.info("Not enough samples for meaningful embedding visualization. More data is needed for this analysis.")
# Feature importance
st.subheader("Feature Importance Analysis")
st.plotly_chart(
visualizer.plot_feature_importance(
model.get_feature_importance()
)
)
# Show detailed metrics
st.subheader("Classification Report")
report_df = pd.DataFrame(metrics['classification_report']).T
st.dataframe(report_df)
else:
st.warning("No labels provided. Only preprocessing results available.")
st.write("Extracted Features:")
st.dataframe(features)
except Exception as e:
st.error(f"Error processing file: {str(e)}")
with tab2:
st.header("Model Retraining")
st.write("""
Upload additional labeled data to retrain the model and improve its performance.
The model will be updated with the new data while maintaining its existing knowledge.
""")
# Training history
st.subheader("Training History")
history_fig = visualizer.plot_training_history(model.get_training_history())
if history_fig:
st.plotly_chart(history_fig)
else:
st.info("No training history available yet.")
# File upload for retraining
retrain_file = st.file_uploader("Upload CSV file for retraining", type=['csv'], key='retrain_upload')
if retrain_file is not None:
try:
# Load new data
new_data = pd.read_csv(retrain_file)
st.write("New Training Data Preview:")
st.dataframe(new_data.head())
# Column selection for retraining
text_col = st.selectbox(
"Select the column containing student responses",
new_data.columns,
key='retrain_text_col'
)
label_col = st.selectbox(
"Select the column containing misconception labels",
new_data.columns,
key='retrain_label_col'
)
if st.button("Retrain Model"):
with st.spinner("Retraining model with new data..."):
# Split new data
X_new_train, X_new_test, y_new_train, y_new_test = train_test_split(
new_data[text_col],
new_data[label_col],
test_size=0.2,
random_state=42
)
# Retrain model
model.train(X_new_train, y_new_train, is_retraining=True)
# Save updated model
model.save_model()
# Evaluate on new test data
new_predictions = model.predict(X_new_test)
new_probabilities = model.predict_proba(X_new_test)
new_metrics = model.get_model_metrics()
# Show updated performance
st.subheader("Updated Model Performance")
# Show cross-validation results
cv_results = visualizer.plot_model_metrics(new_metrics)
if cv_results is not None:
if isinstance(cv_results, tuple):
for fig in cv_results:
st.plotly_chart(fig)
else:
st.plotly_chart(cv_results)
# Show updated training history
st.subheader("Updated Training History")
updated_history_fig = visualizer.plot_training_history(model.get_training_history())
if updated_history_fig:
st.plotly_chart(updated_history_fig)
st.success("Model successfully retrained and saved!")
except Exception as e:
st.error(f"Error during retraining: {str(e)}")
with tab3:
st.header("Quick Misconception Analysis")
st.write("""
Enter a student's response to analyze potential mathematical misconceptions.
The model will identify the type of misconception and provide confidence scores.
""")
# Single response analysis
student_response = st.text_area(
"Enter student response",
placeholder="Example: When multiplying decimals, more decimal places means bigger number"
)
if st.button("Analyze Response"):
if student_response:
with st.spinner("Analyzing response..."):
# Get prediction and probability
prediction = model.predict([student_response])[0]
probabilities = model.predict_proba([student_response])[0]
# Show results
st.subheader("Analysis Results")
# Show detected misconception with high emphasis
st.markdown(f"""
### Detected Misconception:
**{prediction}**
""")
# Show confidence scores
st.subheader("Confidence Scores")
probs_df = pd.DataFrame({
'Misconception Type': model.pipeline.classes_,
'Confidence': probabilities
}).sort_values('Confidence', ascending=False)
st.dataframe(probs_df)
# Extract features for insight
features = preprocessor.preprocess_text(student_response)
# Show key mathematical elements detected
st.subheader("Key Mathematical Elements Detected")
col1, col2, col3 = st.columns(3)
with col1:
st.metric("Numbers Found", features['numbers_found'])
with col2:
st.metric("Operations Found", features['operations_found'])
with col3:
st.metric("Math Terms", features['math_terms_found'])
else:
st.warning("Please enter a student response to analyze.")
# Sample data
with st.expander("Sample Data Format"):
st.write("""
Upload a CSV file with the following columns:
- student_response: Text responses from students
- misconception_label: Category of mathematical misconception (optional)
Example misconception categories:
- number_properties
- decimal_operations
- fraction_operations
- fraction_comparison
- decimal_comparison
""")
sample_data = pd.DataFrame({
'student_response': [
"I think negative numbers can't have square roots",
"When multiplying decimals, more decimal places means bigger number",
"Adding fractions means adding numerators and denominators separately",
"The larger the denominator, the larger the fraction",
"0.1 is greater than 0.9 because 1 is less than 9"
],
'misconception_label': [
'number_properties',
'decimal_operations',
'fraction_operations',
'fraction_comparison',
'decimal_comparison'
]
})
st.dataframe(sample_data) |