Spaces:
Runtime error
Runtime error
| 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']]) | |