Spaces:
Runtime error
Runtime error
lqqqqqqqqq
commited on
Commit
•
ccc3eca
1
Parent(s):
84b6cc7
Update app.py
Browse files
app.py
CHANGED
@@ -1,33 +1,67 @@
|
|
1 |
import streamlit as st
|
2 |
-
from transformers import pipeline
|
3 |
|
4 |
-
|
5 |
-
|
|
|
|
|
|
|
|
|
6 |
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
|
11 |
-
#
|
12 |
-
|
13 |
-
|
14 |
-
# Perform text classification when the user clicks the "Classify" button
|
15 |
-
if st.button("Classify"):
|
16 |
-
if text.strip(): # Check if text is not empty
|
17 |
-
# Perform text classification on the input text
|
18 |
-
results = classifier(text)[0]
|
19 |
|
20 |
-
#
|
21 |
max_score = float('-inf')
|
22 |
max_label = ''
|
23 |
-
|
24 |
-
for result in results:
|
25 |
if result['score'] > max_score:
|
26 |
max_score = result['score']
|
27 |
max_label = result['label']
|
28 |
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
+
from transformers import pipeline, AutoModelForSequenceClassification, AutoModelForSeq2SeqLM, AutoTokenizer
|
3 |
|
4 |
+
class CombinedModel:
|
5 |
+
def __init__(self, classifier_model, classifier_tokenizer, summarizer_model, summarizer_tokenizer):
|
6 |
+
self.classifier_model = classifier_model
|
7 |
+
self.classifier_tokenizer = classifier_tokenizer
|
8 |
+
self.summarizer_model = summarizer_model
|
9 |
+
self.summarizer_tokenizer = summarizer_tokenizer
|
10 |
|
11 |
+
def classify_and_summarize(self, text):
|
12 |
+
classifier = pipeline("text-classification", model=self.classifier_model, tokenizer=self.classifier_tokenizer, return_all_scores=True)
|
13 |
+
summarizer = pipeline("summarization", model=self.summarizer_model, tokenizer=self.summarizer_tokenizer)
|
14 |
|
15 |
+
# Classify the text
|
16 |
+
classification_results = classifier(text)[0]
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
|
18 |
+
# Determine the label with the highest score
|
19 |
max_score = float('-inf')
|
20 |
max_label = ''
|
21 |
+
for result in classification_results:
|
|
|
22 |
if result['score'] > max_score:
|
23 |
max_score = result['score']
|
24 |
max_label = result['label']
|
25 |
|
26 |
+
# Summarize the text
|
27 |
+
summary_results = summarizer(text, max_length=50, min_length=25, do_sample=False)
|
28 |
+
|
29 |
+
return max_label, max_score, summary_results[0]['summary_text']
|
30 |
+
|
31 |
+
@classmethod
|
32 |
+
def from_pretrained(cls, classifier_path, summarizer_path):
|
33 |
+
classifier_model = AutoModelForSequenceClassification.from_pretrained(classifier_path)
|
34 |
+
classifier_tokenizer = AutoTokenizer.from_pretrained(classifier_path)
|
35 |
+
summarizer_model = AutoModelForSeq2SeqLM.from_pretrained(summarizer_path)
|
36 |
+
summarizer_tokenizer = AutoTokenizer.from_pretrained(summarizer_path)
|
37 |
+
return cls(classifier_model, classifier_tokenizer, summarizer_model, summarizer_tokenizer)
|
38 |
+
|
39 |
+
# Load the combined model
|
40 |
+
classifier_path = "lqqqqqqqqq/FinetunedModelGr9"
|
41 |
+
summarizer_path = "lqqqqqqqqq/SummarizeModelGr9"
|
42 |
+
combined_model = CombinedModel.from_pretrained(classifier_path, summarizer_path)
|
43 |
+
|
44 |
+
# Streamlit application title
|
45 |
+
st.title("Text Classification and Summarization")
|
46 |
+
st.write("Classification for 3 labels: negative, neutral, positive")
|
47 |
+
|
48 |
+
# Text input for user to enter the text to classify
|
49 |
+
texts_input = st.text_area("Enter the texts to classify and summarize (one text per line)", "")
|
50 |
+
|
51 |
+
# Perform text classification and summarization when the user clicks the "Classify" button
|
52 |
+
if st.button("Classify"):
|
53 |
+
texts = texts_input.split('\n')
|
54 |
+
for text in texts:
|
55 |
+
text = text.strip()
|
56 |
+
if text: # Check if text is not empty
|
57 |
+
# Classify and summarize the input text
|
58 |
+
label, score, summary = combined_model.classify_and_summarize(text)
|
59 |
+
|
60 |
+
# Display the results
|
61 |
+
st.write("Text:", text)
|
62 |
+
st.write("Label:", label)
|
63 |
+
st.write("Score:", score)
|
64 |
+
st.write("Summary:", summary)
|
65 |
+
st.write("---")
|
66 |
+
else:
|
67 |
+
st.write("Please enter some text to classify and summarize.")
|