Following this instruction : 1. Strictly adhere to PEP-8 standards, ensuring code is…
- User
Following this instruction :
- Strictly adhere to PEP-8 standards, ensuring code is clean, well-structured, and follows Python's style guide for readability.
- Use appropriate modules like
typingfor type hints, ensuring code clarity, maintainability, and leveraging Python's standard libraries effectively. - Focus on time, space, robustness, scalability; write optimized, efficient code that can handle large datasets and scale seamlessly.
- Implement comprehensive error handling, catching exceptions gracefully, providing meaningful error messages, and ensuring code robustness under unexpected conditions.
- Ensure the developer agent writes advanced, maintainable Python code, balancing performance and readability while adhering to best practices.
TASK : we have generalised , robustness , zero hard coding(mainly split, coulmns names case) generalised make professionally code , use advanced concepts of class and function , handing different task text classification , zero short , few shot, sentimental analysis, text generations from datasets import load_dataset
Load our data
data = load_dataset("My_dataset") data #output DatasetDict({ train: Dataset({ features: ['text', 'label'], num_rows: 8530 }) validation: Dataset({ features: ['text', 'label'], num_rows: 1066 }) test: Dataset({ features: ['text', 'label'], num_rows: 1066 }) })
data["train"][0, -1]
from transformers import pipeline
Path to our HF model
model_path = "cardiffnlp/twitter-roberta-base-sentiment-latest"
Load model into pipeline
pipe = pipeline( model=model_path, tokenizer=model_path, return_all_scores=True, device="cuda:0" )
import numpy as np from tqdm import tqdm from transformers.pipelines.pt_utils import KeyDataset
Run inference
y_pred = [] for output in tqdm(pipe(KeyDataset(data["test"], "text")), total=len(data["test"])): negative_score = output[0]["score"] positive_score = output[2]["score"] assignment = np.argmax([negative_score, positive_score]) y_pred.append(assignment)
from sklearn.metrics import classification_report
def evaluate_performance(y_true, y_pred): """Create and print the classification report""" performance = classification_report( y_true, y_pred, target_names=["Negative Review", "Positive Review"] ) print(performance)
from sentence_transformers import SentenceTransformer
Load model
model = SentenceTransformer('sentence-transformers/all-mpnet-base-v2')
Convert text to embeddings
train_embeddings = model.encode(data["train"]["text"], show_progress_bar=True) test_embeddings = model.encode(data["test"]["text"], show_progress_bar=True)
train_embeddings.shape
from sklearn.linear_model import LogisticRegression
- Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.