import streamlit as st import os import google.generativeai as genai from huggingface_hub import hf_hub_download import base64 MODEL_ID = "gemini-2.0-flash-exp" # Keep the model ID as is try: api_key = os.getenv("GEMINI_API_KEY") model_id = MODEL_ID genai.configure(api_key=api_key) except Exception as e: st.error(f"Error: {e}") st.stop model = genai.GenerativeModel(MODEL_ID) chat = model.start_chat() def download_pdf(): """ Downloads the PDF file from the Hugging Face Hub using the correct repo path and filename. """ try: hf_token = os.getenv("HF_TOKEN") repo_id = "louiecerv/vqa_machine_learning_dataset" # Corrected dataset repo path filename = "Supervised_Learning_Alogirthms.pdf" filepath = hf_hub_download(repo_id=repo_id, filename=filename, token=hf_token, repo_type="dataset") return filepath except Exception as e: st.error(f"Failed to download PDF from Hugging Face Hub: {e}") st.stop() # Stop if the download fails # Initialize conversation history in Streamlit session state if "conversation_history" not in st.session_state: st.session_state.conversation_history = [] if "uploaded_file_part" not in st.session_state: # Store the file *part* st.session_state.uploaded_file_part = None if "uploaded_pdf_path" not in st.session_state: st.session_state.uploaded_pdf_path = download_pdf() def multimodal_prompt(pdf_path, text_prompt): """ Sends a multimodal prompt to Gemini, handling file uploads efficiently. Args: pdf_path: The path to the PDF file. text_prompt: The text prompt for the model. Returns: The model's response as a string, or an error message. """ try: if st.session_state.uploaded_file_part is None: # First time, upload pdf_part = genai.upload_file(pdf_path, mime_type="application/pdf") st.session_state.uploaded_file_part = pdf_part prompt = [text_prompt, pdf_part] # First turn includes the actual file else: # Subsequent turns, reference the file prompt = [text_prompt, st.session_state.uploaded_file_part] # Subsequent turns include the file reference response = chat.send_message(prompt) # Update conversation history st.session_state.conversation_history.append({"role": "user", "content": text_prompt, "has_pdf": True}) st.session_state.conversation_history.append({"role": "assistant", "content": response.text}) return response.text except Exception as e: return f"An error occurred: {e}" def display_download_button(file_path, file_name): try: with open(file_path, "rb") as f: file_bytes = f.read() b64 = base64.b64encode(file_bytes).decode() href = f'Download the source document (PDF)' st.markdown(href, unsafe_allow_html=True) except FileNotFoundError: st.error("File not found for download.") except Exception as e: st.error(f"Error during download: {e}") # Define the ML Models roles = ["Linear Regression", "Logistic Regression", "Naive Bayes", "Support Vector Machine", "Decision Tree", "Random Forest", "K-Nearest Neighbor", "Gradient Boosting Manchines", "Neural Network"] # --- Sidebar --- st.sidebar.title("🤖 Visual Q and A") selected_model = st.sidebar.selectbox("Select the ML Model", roles) # --- Main Page --- st.title("📚 VQA on the Supervised Machine Learning Algorithms") about = """ **How to use this App** This app leverages Gemini 2.0 to provide insights on the provided document. Select a question from the dropdown menu or enter your own question to get Gemini's generated response based on the provided document. """ with st.expander("How to use this App"): st.markdown(about) # --- Q and A Tab --- st.header("Questions and Answers") # Generate 5 questions based on the selected model if selected_model == "Linear Regression": questions = [ "What is the fundamental concept of Linear Regression and how does it connect to the goal of predicting a target variable?", "Explain the different components of the Linear Regression equation: y, x1, x2,..., xn, β0, β1, β2,..., βn, and e.", "How does the Ordinary Least Squares (OLS) method help in finding the best-fit line in Linear Regression?", "What is the role of the error term (e) in Linear Regression and how does it affect the accuracy of predictions?", "What are the primary advantages of using Linear Regression, especially in terms of simplicity and efficiency?", "In what situations would the assumption of a linear relationship in Linear Regression become a major drawback?", "How do outliers impact a Linear Regression model and what methods can be used to reduce their influence?", "Explain the concept of multicollinearity in the context of Linear Regression and its potential effects on model performance.", "Can you provide real-world examples where Linear Regression is a suitable and effective solution?", "How can Linear Regression be used to predict a continuous value, such as the price of a house or stock?", "What are the limitations of Linear Regression when dealing with non-linear or categorical data?", "How does Linear Regression differ from Logistic Regression, especially in terms of the types of problems they address?", "What are the relative strengths and weaknesses of Linear Regression compared to more complex models like Support Vector Machines (SVM) or Decision Trees?", "How can Linear Regression be adapted or modified to handle non-linear relationships between variables?", "What techniques can be used for feature selection or dimensionality reduction to enhance the performance of Linear Regression?", "How can regularization methods like Ridge or Lasso regression prevent overfitting in Linear Regression models?", "What are the consequences of heteroscedasticity in Linear Regression and how can it be dealt with?", "What metrics are commonly used to evaluate the performance of a Linear Regression model, such as R-squared or mean squared error?", "How can the coefficients (β1, β2,..., βn) in a Linear Regression model be interpreted to understand the effect of each independent variable on the target variable?", "Can you explain the limitations of Linear Regression when dealing with high-dimensional data or datasets with a large number of features?" ] if selected_model == "Logistic Regression": questions =[ "What is the fundamental concept of Logistic Regression and how does it apply to binary classification problems?", "Explain the role of the sigmoid function in Logistic Regression and how it helps predict probabilities.", "How does Logistic Regression determine the threshold for classifying an instance into one of the two classes?", "What are the main advantages of using Logistic Regression, particularly its interpretability and ability to provide probability estimates?", "In what situations would the inability of Logistic Regression to handle non-linear relationships effectively become a significant limitation?", "How does Logistic Regression handle irrelevant features in the dataset and how does it affect the model's performance?", "Can you provide real-world examples where Logistic Regression is commonly used for classification tasks?", "How does Logistic Regression compare to Linear Regression in terms of their applications and the types of problems they address?", "What are the limitations of Logistic Regression when dealing with multi-class classification problems?", "How can Logistic Regression be extended or modified to handle non-linear decision boundaries?", "What techniques can be used to address overfitting in Logistic Regression models?", "What metrics are commonly used to evaluate the performance of a Logistic Regression model, such as accuracy, precision, recall, and F1-score?", "How can the coefficients in a Logistic Regression model be interpreted to understand the influence of each independent variable on the predicted probabilities?", "What are the implications of imbalanced datasets in Logistic Regression and how can they be addressed?", "How does Logistic Regression handle categorical features and how are they incorporated into the model?", "What are the advantages and disadvantages of using Logistic Regression compared to other classification models like Support Vector Machines or Decision Trees?", "How can regularization techniques be applied in Logistic Regression to improve model generalization?", "What are the challenges in interpreting the results of Logistic Regression when dealing with high-dimensional data?", "How can Logistic Regression be used in applications like spam detection, fraud detection, or medical diagnosis?", "What are the ethical considerations when using Logistic Regression in sensitive domains like healthcare or criminal justice?" ] if selected_model == "Naive Bayes": questions = [ "Explain the underlying principle of Naïve Bayes and its connection to Bayes' Theorem.", "What is the assumption of independence among predictors in Naïve Bayes, and how does it affect the model's performance?", "Describe how Naïve Bayes calculates the probability of a class C given features X, using the formula and explaining each term.", "What are the key advantages of using Naïve Bayes, such as speed, scalability, and robustness to irrelevant features?", "In what situations would the assumption of feature independence in Naïve Bayes become a significant limitation?", "How does Naïve Bayes handle continuous features and incorporate them into the model?", "Provide real-world examples where Naïve Bayes is commonly used for classification tasks.", "Compare Naïve Bayes to other classification models like Logistic Regression or Support Vector Machines, considering accuracy and efficiency.", "What are the different variations of Naïve Bayes, like Gaussian Naïve Bayes or Multinomial Naïve Bayes, and how do their applications differ?", "Explain how Naïve Bayes can be used in text classification, spam filtering, or sentiment analysis.", "What techniques can improve the performance of Naïve Bayes when dealing with correlated features?", "How does Naïve Bayes handle missing values in the dataset?", "What are the limitations of Naïve Bayes when dealing with high-dimensional data or datasets with many features?", "How can Naïve Bayes be combined with other machine learning techniques, such as ensemble methods?", "What ethical considerations arise when using Naïve Bayes in sensitive areas like healthcare or finance?", "How does Naïve Bayes handle imbalanced datasets, and how can this issue be addressed?", "What challenges are associated with interpreting the results of Naïve Bayes, especially with many features?", "How can Naïve Bayes be used in real-time applications that require quick predictions?", "Compare the advantages and disadvantages of using Naïve Bayes versus deep learning models for classification tasks.", "How can Naïve Bayes be adapted or modified to handle both continuous and categorical features together?" ] if selected_model == "Support Vector Machine": questions = [ "What is the core concept behind Support Vector Machines (SVM) and how does it relate to finding the optimal hyperplane for classification?", "Explain the concept of a margin in SVM and its importance in achieving good generalization performance.", "How does SVM handle non-linearly separable data using the Kernel Trick?", "What are the different types of kernel functions used in SVM, such as linear, polynomial, and radial basis function (RBF) kernels?", "What are the main advantages of using SVM, particularly its effectiveness in high-dimensional spaces and ability to handle non-linear classification?", "What are the limitations of SVM, such as its computational cost for large datasets and difficulty in tuning hyperparameters?", "How does SVM compare to other classification models like Logistic Regression or Decision Trees in terms of accuracy and complexity?", "How can SVM be used for both classification and regression tasks?", "What are the challenges in choosing the appropriate kernel function for an SVM model?", "How does SVM handle outliers in the dataset and how do they affect the model's performance?", "What are the different optimization algorithms used to train SVM models, such as quadratic programming or gradient descent?", "How can SVM be used in applications like image recognition, text classification, or bioinformatics?", "What are the implications of imbalanced datasets in SVM and how can they be addressed?", "How does SVM handle categorical features and how are they incorporated into the model?", "What are the advantages and disadvantages of using SVM compared to deep learning models for classification tasks?", "How can SVM be used in conjunction with other machine learning techniques, such as ensemble methods or feature selection?", "What are the ethical considerations when using SVM in sensitive domains like healthcare or finance?", "How can SVM be adapted or modified to handle online learning scenarios where data arrives sequentially?", "What are the challenges in interpreting the results of SVM, particularly when using non-linear kernel functions?", "How can SVM be used in real-time applications where quick predictions are required?" ] if selected_model == "Decision Tree": questions = [ "What is the fundamental concept of a Decision Tree and how does it use a tree-like structure for classification or regression?", "Explain the process of constructing a Decision Tree, including how the data is recursively split based on features.", "What are the different splitting criteria used in Decision Trees, such as information gain, Gini impurity, or entropy?", "What are the advantages of using Decision Trees, such as their interpretability and ability to handle both numerical and categorical data?", "What are the disadvantages of Decision Trees, such as their tendency to overfit the training data?", "How can overfitting be prevented in Decision Trees using techniques like pruning or setting limits on the tree depth?", "How can Decision Trees be used for both classification and regression tasks?", "How do you interpret the results of a Decision Tree and understand the decision rules it has learned?", "What are the limitations of Decision Trees when dealing with complex relationships or high-dimensional data?", "How does a Decision Tree compare to other models like Linear Regression or Support Vector Machines in terms of interpretability and accuracy?", "What are the different ensemble methods that utilize Decision Trees, such as Random Forest or Gradient Boosting Machines?", "How can Decision Trees be used in applications like customer churn prediction, medical diagnosis, or fraud detection?", "How can Decision Trees handle missing values in the dataset?", "What are the ethical considerations when using Decision Trees in sensitive domains like healthcare or education?", "How can Decision Trees be used in conjunction with other machine learning techniques, such as feature selection or data visualization?", "What are the challenges in interpreting the results of Decision Trees when dealing with a large number of features or complex branching structures?", "How can Decision Trees be adapted or modified to handle imbalanced datasets?", "What are the advantages and disadvantages of using Decision Trees compared to deep learning models for classification or regression tasks?", "How can Decision Trees be used in real-time applications where quick predictions are required?", "How can Decision Trees be used to identify important features or variables that contribute most to the prediction task?" ] if selected_model == "Random Forest": questions = [ "What is the fundamental concept of a Random Forest and how does it differ from a single Decision Tree?", "Explain the process of constructing a Random Forest, including the steps involved in creating multiple Decision Trees and combining their predictions.", "How does the Random Forest algorithm utilize bagging (bootstrap aggregating) to enhance the diversity and robustness of the model?", "What are the key parameters that influence the performance of a Random Forest, such as the number of trees, tree depth, and number of features considered at each split?", "How does Random Forest achieve better generalization performance and reduce overfitting compared to a single Decision Tree?", "Explain the role of feature randomness in Random Forest and how it helps to decorrelate the trees and improve model accuracy.", "What are the main advantages of using Random Forest, such as its ability to handle high-dimensional data, missing values, and irrelevant features?", "What are the limitations of Random Forest, such as its computational cost and potential lack of interpretability compared to a single Decision Tree?", "How does Random Forest compare to other ensemble methods like Gradient Boosting Machines in terms of accuracy, complexity, and interpretability?", "How can Random Forest be used for both classification and regression tasks, and what are the differences in its implementation for each type of problem?", "How do you interpret the results of a Random Forest, including understanding feature importance and visualizing the decision boundaries?", "What metrics are commonly used to evaluate the performance of a Random Forest model, such as accuracy, precision, recall, F1-score, and AUC?", "How can Random Forest be used in real-world applications, such as image classification, object detection, fraud detection, and bioinformatics?", "How does Random Forest handle imbalanced datasets and what techniques can be used to address this issue?", "What are the ethical considerations when using Random Forest in sensitive domains, such as healthcare or finance?", "How can Random Forest be integrated with other machine learning techniques, such as feature selection, dimensionality reduction, or hyperparameter optimization?", "What are the challenges in deploying and maintaining a Random Forest model in a production environment, particularly in terms of scalability and resource utilization?", "How can Random Forest be adapted or modified to handle online learning scenarios where data arrives sequentially?", "What are the ongoing research directions and advancements in the field of Random Forest, such as exploring new tree diversity techniques or incorporating explainability features?", "What are the advantages and disadvantages of using Random Forest compared to deep learning models for various machine learning tasks?" ] if selected_model == "K-Nearest Neighbor": questions = [ "What is the core concept behind the K-Nearest Neighbors (KNN) algorithm and how does it use similarity metrics for classification or regression?", "Explain the process of classifying a new instance using KNN, including how the algorithm calculates distances to the K nearest neighbors.", "What are the different distance metrics used in KNN, such as Euclidean distance, Manhattan distance, or cosine similarity?", "How does the choice of K value affect the model's performance and generalization ability in KNN?", "What are the main advantages of using KNN, such as its simplicity, flexibility, and ability to handle non-linear decision boundaries?", "What are the limitations of KNN, such as its computational cost for large datasets, sensitivity to irrelevant features, and lack of interpretability?", "How does KNN compare to other classification models like Logistic Regression or Support Vector Machines in terms of accuracy and efficiency?", "How can KNN be used for both classification and regression tasks, and what are the differences in its implementation for each type of problem?", "What are the challenges in choosing an appropriate distance metric for KNN, especially when dealing with high-dimensional data or mixed data types?", "How does KNN handle categorical features and how are they incorporated into the model?", "What are the different strategies for handling missing values in the dataset when using KNN?", "How can KNN be used in applications like recommendation systems, anomaly detection, or clustering?", "What are the implications of imbalanced datasets in KNN and how can they be addressed?", "What are the ethical considerations when using KNN in sensitive domains like healthcare or finance?", "How can KNN be integrated with other machine learning techniques, such as feature selection, dimensionality reduction, or ensemble methods?", "What are the challenges in interpreting the results of KNN, particularly when dealing with a large number of features or instances?", "How can KNN be adapted or modified to handle online learning scenarios where data arrives sequentially?", "What are the advantages and disadvantages of using KNN compared to deep learning models for classification or regression tasks?", "How can KNN be used in real-time applications where quick predictions are required?", "What are the ongoing research directions and advancements in the field of KNN, such as exploring new distance metrics or improving the algorithm's scalability and efficiency?" ] if selected_model == "Gradient Boosting Manchines": questions = [ "What is the core concept behind Gradient Boosting Machines (GBM) and how does it use an ensemble of weak learners to create a strong predictive model?", "Explain the process of training a Gradient Boosting Machine, including how the algorithm sequentially adds trees to correct errors made by the previous trees.", "How does GBM handle the issue of overfitting by using techniques like shrinkage (learning rate) and tree pruning?", "What are the different loss functions used in GBM, such as mean squared error, log loss, or hinge loss, and how do they affect the model's performance?", "What are the main advantages of using GBM, such as its high predictive accuracy, robustness to outliers, and ability to capture complex relationships in the data?", "What are the limitations of GBM, such as its computational cost, sensitivity to hyperparameters, and potential overfitting on noisy data?", "How does GBM compare to other ensemble methods like Random Forest or AdaBoost in terms of accuracy, complexity, and interpretability?", "How can GBM be used for both classification and regression tasks, and what are the differences in its implementation for each type of problem?", "What are the challenges in tuning hyperparameters for GBM, such as the number of trees, tree depth, learning rate, and subsampling rate?", "How does GBM handle missing values in the dataset and what techniques can be used to impute or ignore them during training?", "What are the different feature importance metrics used in GBM, such as gain, cover, or frequency, and how do they help interpret the model's predictions?", "What are the different regularization techniques that can be applied to GBM, such as L1 or L2 regularization, to prevent overfitting?", "How can GBM be used in real-world applications like click-through rate prediction, customer churn analysis, or fraud detection?", "What are the implications of imbalanced datasets in GBM and how can they be addressed using techniques like class weights or resampling?", "What are the ethical considerations when using GBM in sensitive domains like healthcare or finance?", "How can GBM be integrated with other machine learning techniques, such as feature selection, dimensionality reduction, or hyperparameter optimization?", "What are the challenges in interpreting the results of GBM, particularly when dealing with a large number of features or complex interactions?", "How can GBM be adapted or modified to handle online learning scenarios where data arrives sequentially?", "What are the ongoing research directions and advancements in the field of GBM, such as exploring new loss functions or improving the algorithm's scalability and efficiency?", "What are the advantages and disadvantages of using GBM compared to deep learning models for various machine learning tasks?" ] if selected_model == "Neural Network": questions = [ "What is the core concept behind Neural Networks and how do they mimic the structure and function of the human brain?", "Explain the architecture of a basic feedforward Neural Network, including input layer, hidden layers, activation functions, and output layer.", "How does a Neural Network learn from data using techniques like backpropagation, gradient descent, and stochastic gradient descent?", "What are the different types of Neural Networks, such as Convolutional Neural Networks (CNNs), Recurrent Neural Networks (RNNs), or Generative Adversarial Networks (GANs)?", "What are the main advantages of using Neural Networks, such as their ability to learn complex patterns, generalize well to new data, and adapt to different tasks?", "What are the limitations of Neural Networks, such as their computational cost, sensitivity to hyperparameters, and potential overfitting on small datasets?", "How does a Neural Network compare to other machine learning models like Support Vector Machines or Decision Trees in terms of accuracy, complexity, and interpretability?", "How can Neural Networks be used for both classification and regression tasks, and what are the differences in their implementation for each type of problem?", "What are the challenges in designing the architecture of a Neural Network, such as choosing the number of layers, neurons per layer, and activation functions?", "How does a Neural Network handle categorical features and how are they encoded into the model?", "What are the different activation functions used in Neural Networks, such as sigmoid, tanh, ReLU, or softmax, and how do they affect the model's performance?", "What are the different regularization techniques that can be applied to Neural Networks, such as dropout, weight decay, or early stopping, to prevent overfitting?", "How can Neural Networks be used in real-world applications like image recognition, natural language processing, or reinforcement learning?", "What are the implications of imbalanced datasets in Neural Networks and how can they be addressed using techniques like class weights or oversampling?", "What are the ethical considerations when using Neural Networks in sensitive domains like healthcare or finance?", "How can Neural Networks be integrated with other machine learning techniques, such as transfer learning, feature extraction, or hyperparameter optimization?", "What are the challenges in interpreting the results of Neural Networks, particularly when dealing with deep architectures or complex data transformations?", "How can Neural Networks be adapted or modified to handle online learning scenarios where data arrives sequentially?", "What are the ongoing research directions and advancements in the field of Neural Networks, such as exploring new architectures, optimization algorithms, or explainability features?", "What are the advantages and disadvantages of using Neural Networks compared to traditional machine learning models for various tasks?" ] # Create a selection box selected_question = st.selectbox("Choose a question", questions) # Display a checkbox if st.checkbox('Check this box to ask a question not listed above'): # If the checkbox is checked, display a text box selected_question = st.text_input('Enter a question') if st.button("Ask AI"): with st.spinner("AI is thinking..."): if st.session_state.uploaded_pdf_path is None: st.session_state.uploaded_pdf_path = download_pdf() filepath = st.session_state.uploaded_pdf_path text_prompt = f"Use the provided document focus on rhe topic: {selected_model} to answer the following question: {selected_question}. Use your own knowledge as well as sources from the web and the provided document. Always cite your sourcss." response = multimodal_prompt(filepath, text_prompt) # Use the downloaded filepath st.markdown(f"**Question:** {selected_question}") st.markdown(f"**Response:** {response}") if st.session_state.uploaded_pdf_path: display_download_button(st.session_state.uploaded_pdf_path, "Supervised_Learning_Alogirthms.pdf") st.markdown("[Visit our Hugging Face Space!](https://huggingface.co/wvsuaidev)") st.markdown("© 2025 WVSU AI Dev Team 🤖 ✨")