Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, T5ForConditionalGeneration, T5Tokenizer
|
| 3 |
+
import nltk
|
| 4 |
+
import torch
|
| 5 |
+
from textblob import TextBlob
|
| 6 |
+
from nltk.corpus import stopwords
|
| 7 |
+
from nltk.tokenize import word_tokenize
|
| 8 |
+
|
| 9 |
+
nltk.download('punkt')
|
| 10 |
+
nltk.download('averaged_perceptron_tagger')
|
| 11 |
+
nltk.download('stopwords')
|
| 12 |
+
|
| 13 |
+
# Load models and tokenizers
|
| 14 |
+
tag_tokenizer = AutoTokenizer.from_pretrained("fabiochiu/t5-base-tag-generation")
|
| 15 |
+
tag_model = AutoModelForSeq2SeqLM.from_pretrained("fabiochiu/t5-base-tag-generation")
|
| 16 |
+
|
| 17 |
+
summary_model_name = 'utrobinmv/t5_summary_en_ru_zh_base_2048'
|
| 18 |
+
summary_model = T5ForConditionalGeneration.from_pretrained(summary_model_name)
|
| 19 |
+
summary_tokenizer = T5Tokenizer.from_pretrained(summary_model_name)
|
| 20 |
+
|
| 21 |
+
# Function to generate tags
|
| 22 |
+
def generate_tags(text):
|
| 23 |
+
with torch.no_grad():
|
| 24 |
+
inputs = tag_tokenizer(text, max_length=256, truncation=True, return_tensors="pt")
|
| 25 |
+
output = tag_model.generate(**inputs, num_beams=8, do_sample=True, min_length=10, max_length=64, num_return_sequences=1)
|
| 26 |
+
decoded_output = tag_tokenizer.batch_decode(output, skip_special_tokens=True)[0]
|
| 27 |
+
tags = list(set(decoded_output.strip().split(", ")))
|
| 28 |
+
return tags
|
| 29 |
+
|
| 30 |
+
# Function to generate summaries
|
| 31 |
+
def generate_summary(text, prefix):
|
| 32 |
+
src_text = prefix + text
|
| 33 |
+
input_ids = summary_tokenizer(src_text, return_tensors="pt")
|
| 34 |
+
generated_tokens = summary_model.generate(**input_ids)
|
| 35 |
+
result = summary_tokenizer.batch_decode(generated_tokens, skip_special_tokens=True)
|
| 36 |
+
return result[0]
|
| 37 |
+
|
| 38 |
+
# Function to extract keywords and generate hashtags
|
| 39 |
+
def extract_keywords(content):
|
| 40 |
+
text = content.lower()
|
| 41 |
+
sentences = nltk.sent_tokenize(text)
|
| 42 |
+
keywords = []
|
| 43 |
+
for sentence in sentences:
|
| 44 |
+
words = nltk.word_tokenize(sentence)
|
| 45 |
+
tags = nltk.pos_tag(words)
|
| 46 |
+
for word, tag in tags:
|
| 47 |
+
if tag.startswith('NN'):
|
| 48 |
+
keywords.append(word)
|
| 49 |
+
return keywords
|
| 50 |
+
|
| 51 |
+
def generate_hashtags(content, max_hashtags=10):
|
| 52 |
+
keywords = extract_keywords(content)
|
| 53 |
+
hashtags = []
|
| 54 |
+
for keyword in keywords:
|
| 55 |
+
hashtag = "#" + keyword
|
| 56 |
+
if len(hashtag) <= 20:
|
| 57 |
+
hashtags.append(hashtag)
|
| 58 |
+
return hashtags[:max_hashtags]
|
| 59 |
+
|
| 60 |
+
# Function to extract point of view
|
| 61 |
+
def extract_point_of_view(text):
|
| 62 |
+
stop_words = set(stopwords.words('english'))
|
| 63 |
+
words = word_tokenize(str(text))
|
| 64 |
+
filtered_words = [word for word in words if word.casefold() not in stop_words]
|
| 65 |
+
text = ' '.join(filtered_words)
|
| 66 |
+
|
| 67 |
+
blob = TextBlob(text)
|
| 68 |
+
polarity = blob.sentiment.polarity
|
| 69 |
+
subjectivity = blob.sentiment.subjectivity
|
| 70 |
+
|
| 71 |
+
if polarity > 0.5:
|
| 72 |
+
point_of_view = "Positive"
|
| 73 |
+
elif polarity < -0.5:
|
| 74 |
+
point_of_view = "Negative"
|
| 75 |
+
else:
|
| 76 |
+
point_of_view = "Neutral"
|
| 77 |
+
|
| 78 |
+
return point_of_view
|
| 79 |
+
|
| 80 |
+
# Streamlit application
|
| 81 |
+
st.title("Text Analysis Application")
|
| 82 |
+
|
| 83 |
+
text = st.text_area("Enter your text here:")
|
| 84 |
+
|
| 85 |
+
if st.button("Analyze"):
|
| 86 |
+
if text:
|
| 87 |
+
# Generate tags
|
| 88 |
+
tags = generate_tags(text)
|
| 89 |
+
st.subheader("Generated Tags")
|
| 90 |
+
st.write(tags)
|
| 91 |
+
|
| 92 |
+
# Generate summaries
|
| 93 |
+
summary1 = generate_summary(text, 'summary: ')
|
| 94 |
+
summary2 = generate_summary(text, 'summary brief: ')
|
| 95 |
+
st.subheader("Summary 1")
|
| 96 |
+
st.write(summary1)
|
| 97 |
+
st.subheader("Summary 2")
|
| 98 |
+
st.write(summary2)
|
| 99 |
+
|
| 100 |
+
# Generate hashtags
|
| 101 |
+
hashtags = generate_hashtags(text)
|
| 102 |
+
st.subheader("Generated Hashtags")
|
| 103 |
+
st.write(hashtags)
|
| 104 |
+
|
| 105 |
+
# Extract point of view
|
| 106 |
+
point_of_view = extract_point_of_view(text)
|
| 107 |
+
st.subheader("Point of View")
|
| 108 |
+
st.write(point_of_view)
|
| 109 |
+
else:
|
| 110 |
+
st.warning("Please enter text to analyze.")
|