Spaces:
Runtime error
Runtime error
Download app.py from harrycool12/MISCONCEPTION: direct link, hf CLI and curl.
- Browser
- Download file 13.3 kB
-
https://huggingface.co/spaces/harrycool12/MISCONCEPTION/resolve/main/app.py
- Command line
-
hf download hf://spaces/harrycool12/MISCONCEPTION/app.py
-
curl -L -o app.py https://huggingface.co/spaces/harrycool12/MISCONCEPTION/resolve/main/app.py
13.3 kB
| 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 | |
| def load_preprocessor(): | |
| return TextPreprocessor() | |
| 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) |