Spaces:
Runtime error
Runtime error
File size: 1,413 Bytes
baa2f43 324dfac baa2f43 324dfac baa2f43 324dfac baa2f43 324dfac |
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 |
import pandas as pd
from transformers import pipeline
# Example data
data = {
'term': [
'Atmospheric Chemistry',
'Organic Chemistry',
'Business Ethics',
'Corporate Social Responsibility'
]
}
df = pd.DataFrame(data)
# Load the zero-shot classification pipeline
classifier = pipeline('zero-shot-classification', model='facebook/bart-large-mnli')
# Define your candidate labels
candidate_labels = ['Discipline', 'Subdiscipline']
# Function to classify term and recommend discipline
def classify_term(term):
result = classifier(term, candidate_labels)
label = result['labels'][0] # Get the highest scoring label
return label
# Classify all terms
df['classification'] = df['term'].apply(classify_term)
# Example mapping of subdisciplines to disciplines
subdiscipline_to_discipline = {
'Atmospheric Chemistry': 'Atmospheric Science',
'Organic Chemistry': 'Chemistry',
'Corporate Social Responsibility': 'Business Ethics'
# Add your mappings here
}
def recommend_discipline(term, classification):
if classification == 'Subdiscipline':
return subdiscipline_to_discipline.get(term, 'Unknown Discipline')
else:
return term
df['recommended_discipline'] = df.apply(lambda x: recommend_discipline(x['term'], x['classification']), axis=1)
# Display the results
print(df[['term', 'classification', 'recommended_discipline']])
|