Spaces:
Sleeping
Sleeping
Commit ·
c2d79c4
1
Parent(s): 5071afd
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Import necessary libraries
|
| 2 |
+
import streamlit as st
|
| 3 |
+
from transformers import AutoModelForSequenceClassification, AutoTokenizer, AutoModelForSeq2SeqLM
|
| 4 |
+
import torch
|
| 5 |
+
|
| 6 |
+
# Define function to load models
|
| 7 |
+
@st.cache(allow_output_mutation=True)
|
| 8 |
+
def load_models():
|
| 9 |
+
classification_model_name = 'distilbert-base-uncased-finetuned-sst-2-english'
|
| 10 |
+
classification_model = AutoModelForSequenceClassification.from_pretrained(classification_model_name)
|
| 11 |
+
classification_tokenizer = AutoTokenizer.from_pretrained(classification_model_name, model_max_length=512)
|
| 12 |
+
|
| 13 |
+
summarization_model_name = 't5-base'
|
| 14 |
+
summarization_model = AutoModelForSeq2SeqLM.from_pretrained(summarization_model_name)
|
| 15 |
+
summarization_tokenizer = AutoTokenizer.from_pretrained(summarization_model_name, model_max_length=512)
|
| 16 |
+
|
| 17 |
+
return classification_model, classification_tokenizer, summarization_model, summarization_tokenizer
|
| 18 |
+
|
| 19 |
+
classification_model, classification_tokenizer, summarization_model, summarization_tokenizer = load_models()
|
| 20 |
+
|
| 21 |
+
# Title of the app
|
| 22 |
+
st.title('Text Classification and Summarization with Hugging Face')
|
| 23 |
+
|
| 24 |
+
# Take user input
|
| 25 |
+
text = st.text_area("Enter text:", "")
|
| 26 |
+
submit_button = st.button("Analyze Text")
|
| 27 |
+
|
| 28 |
+
# Predict function for sentiment analysis
|
| 29 |
+
def predict_sentiment(text):
|
| 30 |
+
tokenized_text = classification_tokenizer.tokenize(text)
|
| 31 |
+
results = []
|
| 32 |
+
|
| 33 |
+
# Break text into chunks of max_model_length tokens
|
| 34 |
+
for i in range(0, len(tokenized_text), classification_tokenizer.model_max_length):
|
| 35 |
+
chunk = tokenized_text[i:i+classification_tokenizer.model_max_length]
|
| 36 |
+
chunk = classification_tokenizer.convert_tokens_to_string(chunk)
|
| 37 |
+
|
| 38 |
+
inputs = classification_tokenizer(chunk, return_tensors="pt", truncation=True, padding='max_length')
|
| 39 |
+
outputs = classification_model(**inputs)
|
| 40 |
+
probs = torch.nn.functional.softmax(outputs[0], dim=-1)
|
| 41 |
+
results.append(probs.detach().numpy())
|
| 42 |
+
return results
|
| 43 |
+
|
| 44 |
+
# Predict function for text summarization
|
| 45 |
+
def summarize_text(text):
|
| 46 |
+
tokenized_text = summarization_tokenizer.tokenize(text)
|
| 47 |
+
summaries = []
|
| 48 |
+
|
| 49 |
+
# Break text into chunks of max_model_length tokens
|
| 50 |
+
for i in range(0, len(tokenized_text), summarization_tokenizer.model_max_length):
|
| 51 |
+
chunk = tokenized_text[i:i+summarization_tokenizer.model_max_length]
|
| 52 |
+
chunk = summarization_tokenizer.convert_tokens_to_string(chunk)
|
| 53 |
+
|
| 54 |
+
inputs = summarization_tokenizer.encode("summarize: " + chunk, return_tensors="pt", truncation=True, padding='max_length')
|
| 55 |
+
outputs = summarization_model.generate(inputs, max_length=150, min_length=40, length_penalty=2.0, num_beams=4, early_stopping=True)
|
| 56 |
+
summary = summarization_tokenizer.decode(outputs[0]).replace('<pad>', '').replace('</s>', '')
|
| 57 |
+
summaries.append(summary)
|
| 58 |
+
return summaries
|
| 59 |
+
|
| 60 |
+
if submit_button:
|
| 61 |
+
if text:
|
| 62 |
+
with st.spinner("Analyzing..."):
|
| 63 |
+
# Sentiment analysis
|
| 64 |
+
results = predict_sentiment(text)
|
| 65 |
+
for i, probs in enumerate(results):
|
| 66 |
+
st.markdown(f"**Result {i+1}:**")
|
| 67 |
+
st.markdown(f"**Positive sentiment:** `{probs[0][1]:.2f}`")
|
| 68 |
+
st.markdown(f"**Negative sentiment:** `{probs[0][0]:.2f}`")
|
| 69 |
+
|
| 70 |
+
# Text summarization
|
| 71 |
+
summaries = summarize_text(text)
|
| 72 |
+
for i, summary in enumerate(summaries):
|
| 73 |
+
st.markdown(f"**Summary {i+1}:** `{summary}`")
|
| 74 |
+
else:
|
| 75 |
+
st.warning("Please enter text to analyze.")
|