Spaces:
Sleeping
Sleeping
| # Import necessary libraries | |
| import streamlit as st | |
| import torch | |
| from transformers import GPT2LMHeadModel, GPT2Tokenizer, GPT2ForSequenceClassification, TrainingArguments, Trainer, DataCollatorWithPadding, DataCollatorForLanguageModeling | |
| # Step 1: Set Up Your Environment | |
| # Environment setup and package installations. | |
| # Step 2: Data Preparation | |
| # Load and preprocess your CSV dataset. | |
| df = pd.read_csv('stepkids_training_data.csv') | |
| # Filter out rows with missing label data | |
| df = df.dropna(subset=['Theme 1', 'Theme 2', 'Theme 3', 'Theme 4', 'Theme 5']) | |
| text_list = df['Post Text'].tolist() | |
| labels = df[['Theme 1', 'Theme 2', 'Theme 3', 'Theme 4', 'Theme 5']].values.tolist() | |
| # Step 3: Model Selection | |
| # Load your GPT-2 model for text generation. | |
| model_name = "gpt2" # Choose the appropriate GPT-2 model variant | |
| text_gen_model = GPT2LMHeadModel.from_pretrained(model_name) | |
| text_gen_tokenizer = GPT2Tokenizer.from_pretrained(model_name) | |
| text_gen_tokenizer.pad_token = text_gen_tokenizer.eos_token | |
| # Load your sequence classification model (e.g., BERT) | |
| seq_classifier_model = GPT2ForSequenceClassification.from_pretrained("fine_tuned_classifier_model") | |
| seq_classifier_tokenizer = GPT2Tokenizer.from_pretrained("fine_tuned_classifier_model") | |
| seq_classifier_tokenizer.pad_token = seq_classifier_tokenizer.eos_token | |
| # Create a title and a text input for the app | |
| st.title('Thematic Analysis with GPT-2 Large') | |
| text = st.text_area('Enter some text') | |
| # If the text is not empty, perform both text generation and sequence classification | |
| if text: | |
| # Perform text generation | |
| generated_text = generate_text(text, text_gen_model, text_gen_tokenizer) | |
| st.write('Generated Text:') | |
| st.write(generated_text) | |
| # Perform sequence classification | |
| labels = classify_text(text, seq_classifier_model, seq_classifier_tokenizer) | |
| st.write('Classified Labels:') | |
| st.write(labels) | |
| # Function for generating text based on input | |
| def generate_text(input_text, model, tokenizer): | |
| # Append the special token to the input | |
| input_text = input_text + ' [LABEL]' | |
| input_ids = tokenizer.encode(input_text, return_tensors='pt') | |
| attention_mask = torch.ones_like(input_ids) | |
| outputs = model.generate(input_ids, attention_mask=attention_mask, max_length=len(input_ids) + 5, do_sample=True, top_p=0.95) | |
| generated = tokenizer.decode(outputs[0], skip_special_tokens=False) | |
| labels = generated.split(',') | |
| labels = [label.replace('[LABEL]', '').strip() for label in labels] | |
| return generated | |
| # Function for sequence classification | |
| def classify_text(input_text, model, tokenizer): | |
| # Tokenize the input text | |
| input_ids = tokenizer.encode(input_text, return_tensors='pt') | |
| attention_mask = torch.ones_like(input_ids) | |
| # Perform sequence classification | |
| result = model(input_ids, attention_mask=attention_mask) | |
| # Post-process the results (e.g., select labels based on a threshold) | |
| labels = post_process_labels(result) | |
| return labels | |
| # Post-process labels based on a threshold or confidence score | |
| def post_process_labels(results): | |
| # Implement your logic to extract and filter labels | |
| # based on your sequence classification model's output | |
| # For example, you might use a threshold for each label's score | |
| # to determine whether it should be considered a valid theme. | |
| # Return the selected labels as a list. | |
| selected_labels = [] | |
| return selected_labels | |