Instructions to use isroych/prompt-analyzer with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Scikit-learn
How to use isroych/prompt-analyzer with Scikit-learn:
from huggingface_hub import hf_hub_download import joblib model = joblib.load( hf_hub_download("isroych/prompt-analyzer", "sklearn_model.joblib") ) # only load pickle files from sources you trust # read more about it here https://skops.readthedocs.io/en/stable/persistence.html - Notebooks
- Google Colab
- Kaggle
Download classifier.py from isroych/prompt-analyzer: direct link, hf CLI and curl.
- Browser
- Download file 2.17 kB
-
https://huggingface.co/isroych/prompt-analyzer/resolve/main/classifier.py
- Command line
-
hf download hf://isroych/prompt-analyzer/classifier.py
-
curl -L -o classifier.py https://huggingface.co/isroych/prompt-analyzer/resolve/main/classifier.py
2.17 kB
| from sklearn.feature_extraction.text import CountVectorizer | |
| from sklearn.model_selection import train_test_split | |
| from sklearn.naive_bayes import MultinomialNB | |
| from sklearn.metrics import accuracy_score, classification_report | |
| from pandas import * | |
| import pickle | |
| import sys | |
| # Step 1: Data Preparation | |
| # Assuming you have 'text' and 'label' columns in your dataset. | |
| # Load your data into a pandas DataFrame or use any other data loading method. | |
| # For this example, we'll use a simple list of reviews and labels. | |
| filename = 'classifier.bin' | |
| def train(): | |
| data = read_csv("train.csv") | |
| text = data['text'].tolist() | |
| label = data['label'].tolist() | |
| # Step 2: Text Preprocessing and Feature Extraction | |
| vectorizer = CountVectorizer() | |
| X = vectorizer.fit_transform(text) | |
| # Step 4: Split Data | |
| X_train, X_test, y_train, y_test = train_test_split(X, label, test_size=0.2, random_state=42) | |
| # Step 5: Create and Train the Classifier | |
| classifier = MultinomialNB() | |
| classifier.fit(X_train, y_train) | |
| # Step 6: Evaluate the Classifier | |
| y_pred = classifier.predict(X_test) | |
| accuracy = accuracy_score(y_test, y_pred) | |
| report = classification_report(y_test, y_pred) | |
| print(f"Accuracy: {accuracy}") | |
| print("Classification Report:\n", report) | |
| # save the model to disk | |
| pickle.dump(classifier, open(filename, 'wb')) | |
| pickle.dump(vectorizer, open('vectorizer.pkl', 'wb')) | |
| def classify(text): | |
| classifier = pickle.load(open(filename, 'rb')) | |
| vectorizer = pickle.load(open('vectorizer.pkl', 'rb')) | |
| # New text you want to classify | |
| new_text = [text] | |
| # Preprocess and convert new text into numerical features using the same vectorizer | |
| new_text_features = vectorizer.transform(new_text) | |
| # Use the trained classifier to predict the label | |
| predicted_label = classifier.predict(new_text_features) | |
| return predicted_label[0] | |
| #print(f"Predicted Label: {predicted_label[0]}") | |
| if __name__ == '__main__': | |
| train() | |
| classification = input("would you like to classify(Y/n)?\n") | |
| if classification.upper() == "Y": | |
| print(classify("I like turtles, do you?")) | |
| else: | |
| pass | |