Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,50 +1,78 @@
|
|
1 |
-
# Import
|
2 |
import streamlit as st
|
3 |
-
from transformers import GPT2Tokenizer, GPT2LMHeadModel, pipeline
|
4 |
import torch
|
|
|
5 |
|
6 |
-
#
|
7 |
-
|
8 |
-
gen_tokenizer = GPT2Tokenizer.from_pretrained('gpt2-large')
|
9 |
|
10 |
-
#
|
11 |
-
|
|
|
12 |
|
13 |
-
#
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
# Return the labels as a list
|
31 |
-
return labels
|
32 |
|
33 |
# Create a title and a text input for the app
|
34 |
st.title('Thematic Analysis with GPT-2 Large')
|
35 |
-
text = st.
|
36 |
|
37 |
-
# If the text is not empty,
|
38 |
if text:
|
39 |
-
#
|
40 |
-
|
41 |
-
|
42 |
-
st.write(
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Import necessary libraries
|
2 |
import streamlit as st
|
|
|
3 |
import torch
|
4 |
+
from transformers import GPT2LMHeadModel, GPT2Tokenizer, GPT2ForSequenceClassification, TrainingArguments, Trainer, DataCollatorWithPadding, DataCollatorForLanguageModeling
|
5 |
|
6 |
+
# Step 1: Set Up Your Environment
|
7 |
+
# Environment setup and package installations.
|
|
|
8 |
|
9 |
+
# Step 2: Data Preparation
|
10 |
+
# Load and preprocess your CSV dataset.
|
11 |
+
df = pd.read_csv('stepkids_training_data.csv')
|
12 |
|
13 |
+
# Filter out rows with missing label data
|
14 |
+
df = df.dropna(subset=['Theme 1', 'Theme 2', 'Theme 3', 'Theme 4', 'Theme 5'])
|
15 |
+
|
16 |
+
text_list = df['Post Text'].tolist()
|
17 |
+
labels = df[['Theme 1', 'Theme 2', 'Theme 3', 'Theme 4', 'Theme 5']].values.tolist()
|
18 |
+
|
19 |
+
# Step 3: Model Selection
|
20 |
+
# Load your GPT-2 model for text generation.
|
21 |
+
model_name = "gpt2" # Choose the appropriate GPT-2 model variant
|
22 |
+
text_gen_model = GPT2LMHeadModel.from_pretrained(model_name)
|
23 |
+
text_gen_tokenizer = GPT2Tokenizer.from_pretrained(model_name)
|
24 |
+
text_gen_tokenizer.pad_token = text_gen_tokenizer.eos_token
|
25 |
+
|
26 |
+
# Load your sequence classification model (e.g., BERT)
|
27 |
+
seq_classifier_model = GPT2ForSequenceClassification.from_pretrained("fine_tuned_classifier_model")
|
28 |
+
seq_classifier_tokenizer = GPT2Tokenizer.from_pretrained("fine_tuned_classifier_model")
|
29 |
+
seq_classifier_tokenizer.pad_token = seq_classifier_tokenizer.eos_token
|
|
|
|
|
30 |
|
31 |
# Create a title and a text input for the app
|
32 |
st.title('Thematic Analysis with GPT-2 Large')
|
33 |
+
text = st.text_area('Enter some text')
|
34 |
|
35 |
+
# If the text is not empty, perform both text generation and sequence classification
|
36 |
if text:
|
37 |
+
# Perform text generation
|
38 |
+
generated_text = generate_text(text, text_gen_model, text_gen_tokenizer)
|
39 |
+
st.write('Generated Text:')
|
40 |
+
st.write(generated_text)
|
41 |
+
|
42 |
+
# Perform sequence classification
|
43 |
+
labels = classify_text(text, seq_classifier_model, seq_classifier_tokenizer)
|
44 |
+
st.write('Classified Labels:')
|
45 |
+
st.write(labels)
|
46 |
+
|
47 |
+
# Function for generating text based on input
|
48 |
+
def generate_text(input_text, model, tokenizer):
|
49 |
+
# Append the special token to the input
|
50 |
+
input_text = input_text + ' [LABEL]'
|
51 |
+
input_ids = tokenizer.encode(input_text, return_tensors='pt')
|
52 |
+
attention_mask = torch.ones_like(input_ids)
|
53 |
+
outputs = model.generate(input_ids, attention_mask=attention_mask, max_length=len(input_ids) + 5, do_sample=True, top_p=0.95)
|
54 |
+
generated = tokenizer.decode(outputs[0], skip_special_tokens=False)
|
55 |
+
labels = generated.split(',')
|
56 |
+
labels = [label.replace('[LABEL]', '').strip() for label in labels]
|
57 |
+
return generated
|
58 |
+
|
59 |
+
# Function for sequence classification
|
60 |
+
def classify_text(input_text, model, tokenizer):
|
61 |
+
# Tokenize the input text
|
62 |
+
input_ids = tokenizer.encode(input_text, return_tensors='pt')
|
63 |
+
attention_mask = torch.ones_like(input_ids)
|
64 |
+
# Perform sequence classification
|
65 |
+
result = model(input_ids, attention_mask=attention_mask)
|
66 |
+
# Post-process the results (e.g., select labels based on a threshold)
|
67 |
+
labels = post_process_labels(result)
|
68 |
+
return labels
|
69 |
+
|
70 |
+
# Post-process labels based on a threshold or confidence score
|
71 |
+
def post_process_labels(results):
|
72 |
+
# Implement your logic to extract and filter labels
|
73 |
+
# based on your sequence classification model's output
|
74 |
+
# For example, you might use a threshold for each label's score
|
75 |
+
# to determine whether it should be considered a valid theme.
|
76 |
+
# Return the selected labels as a list.
|
77 |
+
selected_labels = []
|
78 |
+
return selected_labels
|