File size: 3,422 Bytes
c3da8c6
fa93b6f
 
c3da8c6
fa93b6f
c3da8c6
 
fa93b6f
c3da8c6
 
 
fa93b6f
c3da8c6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fa93b6f
 
 
c3da8c6
fa93b6f
c3da8c6
fa93b6f
c3da8c6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# 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