awacke1 commited on
Commit
76df0cd
·
1 Parent(s): 0498318

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +69 -0
app.py ADDED
@@ -0,0 +1,69 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pandas as pd
2
+ import torch
3
+ import streamlit as st
4
+ from transformers import DistilBertForSequenceClassification, DistilBertTokenizerFast, Trainer, TrainingArguments
5
+
6
+ # Define the health care sentiment classification data
7
+ data = [
8
+ {"text": "The health care services were excellent and the staff was very friendly.", "label": 1},
9
+ {"text": "I had a bad experience with the health care services. The doctors were not knowledgeable and the staff was rude.", "label": 0},
10
+ {"text": "The health care services were okay, but the waiting time was too long.", "label": 1},
11
+ {"text": "I was very satisfied with the health care services. The doctors were very professional and the staff was helpful.", "label": 1},
12
+ {"text": "The health care services were average. The doctors were not exceptional and the staff was not very friendly.", "label": 0}
13
+ ]
14
+
15
+ # Convert the data to a pandas dataframe
16
+ df = pd.DataFrame(data)
17
+
18
+ # Load the pre-trained model and tokenizer
19
+ model = DistilBertForSequenceClassification.from_pretrained('distilbert-base-uncased-finetuned-sst-2-english')
20
+ tokenizer = DistilBertTokenizerFast.from_pretrained('distilbert-base-uncased')
21
+
22
+ # Tokenize the text and encode the labels
23
+ tokenized_inputs = tokenizer(list(df.text), padding=True, truncation=True, max_length=512)
24
+ tokenized_labels = torch.tensor(df.label)
25
+
26
+ # Define the training arguments
27
+ training_args = TrainingArguments(
28
+ output_dir='./results',
29
+ num_train_epochs=3,
30
+ per_device_train_batch_size=16,
31
+ per_device_eval_batch_size=64,
32
+ warmup_steps=500,
33
+ weight_decay=0.01,
34
+ logging_dir='./logs',
35
+ logging_steps=10,
36
+ load_best_model_at_end=True,
37
+ evaluation_strategy='steps',
38
+ eval_steps=100,
39
+ metric_for_best_model='accuracy'
40
+ )
41
+
42
+ # Define the trainer
43
+ trainer = Trainer(
44
+ model=model,
45
+ args=training_args,
46
+ train_dataset=tokenized_inputs,
47
+ train_labels=tokenized_labels
48
+ )
49
+
50
+ # Train the model
51
+ trainer.train()
52
+
53
+ # Evaluate the model on a sample text
54
+ sample_text = "I had a great experience with the health care services. The doctors were very knowledgeable and the staff was friendly."
55
+ encoded_sample_text = tokenizer.encode(sample_text, return_tensors='pt')
56
+ logits = model(encoded_sample_text)[0]
57
+ probabilities = logits.softmax(dim=1)
58
+ sentiment = 'positive' if probabilities[0][1] > probabilities[0][0] else 'negative'
59
+
60
+ # Create the Streamlit app
61
+ st.title("Health Care Sentiment Classifier")
62
+ text_input = st.text_input("Enter some text to classify:")
63
+ if st.button("Classify"):
64
+ encoded_text = tokenizer.encode(text_input, return_tensors='pt')
65
+ logits = model(encoded_text)[0]
66
+ probabilities = logits.softmax(dim=1)
67
+ sentiment = 'positive' if probabilities[0][1] > probabilities[0][0] else 'negative'
68
+ st.write(f"The sentiment of the text is {sentiment}.")
69
+ st.write(f"For example, the sentiment of '{sample_text}' is {sentiment}.")