File size: 2,631 Bytes
7964002 |
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 |
#!/usr/bin/env python3
"""
Example usage of screenplay salience features from Hugging Face.
"""
from datasets import load_dataset
import pandas as pd
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import classification_report
# ============================================================================
# Method 1: Load with datasets library
# ============================================================================
print("Loading training data...")
ds = load_dataset(
"YOUR_USERNAME/screenplay-features",
data_files={
"train": ["train/base.parquet", "train/gc_polarity.parquet"],
"test": ["test/base.parquet", "test/gc_polarity.parquet"]
}
)
train_df = ds['train'].to_pandas()
test_df = ds['test'].to_pandas()
# Separate features and labels
feature_cols = [c for c in train_df.columns if c not in ["movie_id", "scene_index", "label"]]
X_train = train_df[feature_cols]
y_train = train_df["label"]
X_test = test_df[feature_cols]
y_test = test_df["label"]
print(f"Train: {len(X_train)} samples, {len(feature_cols)} features")
print(f"Test: {len(X_test)} samples")
# ============================================================================
# Method 2: Use custom loader (easier)
# ============================================================================
# If you have the feature_cache module:
# from feature_cache.load_hf import load_groups
#
# X_train, y_train = load_groups(
# groups=["base", "gc_polarity", "emotional"],
# split="train",
# hf_repo="YOUR_USERNAME/screenplay-features"
# )
#
# X_test, y_test = load_groups(
# groups=["base", "gc_polarity", "emotional"],
# split="test",
# hf_repo="YOUR_USERNAME/screenplay-features"
# )
# ============================================================================
# Train a simple model
# ============================================================================
print("\nTraining logistic regression...")
clf = LogisticRegression(max_iter=1000, random_state=42)
clf.fit(X_train.fillna(0), y_train)
# Evaluate
y_pred = clf.predict(X_test.fillna(0))
print("\nTest Results:")
print(classification_report(y_test, y_pred, target_names=["Non-salient", "Salient"]))
# ============================================================================
# Explore features
# ============================================================================
print("\nTop 10 features by coefficient:")
feature_importance = pd.DataFrame({
'feature': feature_cols,
'coefficient': clf.coef_[0]
}).sort_values('coefficient', key=abs, ascending=False)
print(feature_importance.head(10))
|