Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,16 +1,21 @@
|
|
1 |
import streamlit as st
|
2 |
from transformers import pipeline
|
3 |
from translate import Translator
|
4 |
-
import
|
5 |
|
6 |
# Load models
|
7 |
def load_models():
|
|
|
8 |
sentiment_analyzer = pipeline("text-classification", model="miltonc/distilbert-base-uncased_ft_5")
|
9 |
summarizer = pipeline("summarization", model="FelixChao/T5-Chinese-Summarization")
|
10 |
return sentiment_analyzer, summarizer
|
11 |
|
|
|
|
|
|
|
12 |
|
13 |
def sentiment_analysis(text, sentiment_analyzer):
|
|
|
14 |
try:
|
15 |
result = sentiment_analyzer(text)[0]
|
16 |
return result
|
@@ -18,9 +23,8 @@ def sentiment_analysis(text, sentiment_analyzer):
|
|
18 |
print(f"sentiment_analysis error for '{text}': {e}. Returning 'sentiment_analysis Failed'")
|
19 |
return {"label": "sentiment_analysis Failed", "score": 0.0}
|
20 |
|
21 |
-
|
22 |
-
# Generate a narrative story using the GPT-2 genre-based story generator
|
23 |
def summarize_news(text, summarizer):
|
|
|
24 |
try:
|
25 |
summary = summarizer(text, max_length=30, min_length=10)[0]['summary_text']
|
26 |
return summary
|
@@ -28,8 +32,8 @@ def summarize_news(text, summarizer):
|
|
28 |
print(f"Summarization error for '{text}': {e}. Returning 'Summarization Failed'")
|
29 |
return "Summarization Failed"
|
30 |
|
31 |
-
|
32 |
def translate_text(text, target_language='en', source_language='zh-TW'):
|
|
|
33 |
translator = Translator(to_lang=target_language, from_lang=source_language)
|
34 |
try:
|
35 |
translation = translator.translate(text)
|
@@ -38,21 +42,38 @@ def translate_text(text, target_language='en', source_language='zh-TW'):
|
|
38 |
print(f"Translation error for '{text}': {e}. Returning 'Translation Failed'")
|
39 |
return "Translation Failed"
|
40 |
|
41 |
-
|
|
|
|
|
|
|
|
|
42 |
# Main Streamlit app
|
43 |
def main():
|
|
|
44 |
st.title("AI-Powered Sentiment Analysis and Summarization")
|
45 |
|
46 |
sentiment_analyzer, summarizer = load_models()
|
47 |
|
48 |
-
text = st.text_area("Enter the Chinese text here.....", height=200)
|
49 |
|
50 |
-
if st.button("Analyze"):
|
51 |
if text:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
52 |
with st.spinner("Analyzing sentiment..."):
|
53 |
text_en = translate_text(text, target_language='en', source_language='zh-TW')
|
54 |
sentiment_result = sentiment_analysis(text_en, sentiment_analyzer)
|
55 |
-
label_map = {"LABEL_0": 'negative', "LABEL_1": 'neutral', "LABEL_2": 'positive'}
|
56 |
try:
|
57 |
sentiment_label = label_map.get(sentiment_result['label'], 'Unknown')
|
58 |
sentiment_score = sentiment_result['score']
|
@@ -68,4 +89,4 @@ def main():
|
|
68 |
st.write(story)
|
69 |
|
70 |
if __name__ == "__main__":
|
71 |
-
main()
|
|
|
1 |
import streamlit as st
|
2 |
from transformers import pipeline
|
3 |
from translate import Translator
|
4 |
+
import re
|
5 |
|
6 |
# Load models
|
7 |
def load_models():
|
8 |
+
"""Loads the sentiment analysis and summarization models."""
|
9 |
sentiment_analyzer = pipeline("text-classification", model="miltonc/distilbert-base-uncased_ft_5")
|
10 |
summarizer = pipeline("summarization", model="FelixChao/T5-Chinese-Summarization")
|
11 |
return sentiment_analyzer, summarizer
|
12 |
|
13 |
+
# Max length for models
|
14 |
+
SENTIMENT_MAX_LENGTH = 512 # Maximum input length for DistilBERT sentiment analysis
|
15 |
+
SUMMARIZER_MAX_LENGTH = 512 # Maximum input length for T5 summarization (approximate)
|
16 |
|
17 |
def sentiment_analysis(text, sentiment_analyzer):
|
18 |
+
"""Analyzes the sentiment of the input text."""
|
19 |
try:
|
20 |
result = sentiment_analyzer(text)[0]
|
21 |
return result
|
|
|
23 |
print(f"sentiment_analysis error for '{text}': {e}. Returning 'sentiment_analysis Failed'")
|
24 |
return {"label": "sentiment_analysis Failed", "score": 0.0}
|
25 |
|
|
|
|
|
26 |
def summarize_news(text, summarizer):
|
27 |
+
"""Summarizes the input news text."""
|
28 |
try:
|
29 |
summary = summarizer(text, max_length=30, min_length=10)[0]['summary_text']
|
30 |
return summary
|
|
|
32 |
print(f"Summarization error for '{text}': {e}. Returning 'Summarization Failed'")
|
33 |
return "Summarization Failed"
|
34 |
|
|
|
35 |
def translate_text(text, target_language='en', source_language='zh-TW'):
|
36 |
+
"""Translates Chinese text to English."""
|
37 |
translator = Translator(to_lang=target_language, from_lang=source_language)
|
38 |
try:
|
39 |
translation = translator.translate(text)
|
|
|
42 |
print(f"Translation error for '{text}': {e}. Returning 'Translation Failed'")
|
43 |
return "Translation Failed"
|
44 |
|
45 |
+
# Check if the input text is Chinese
|
46 |
+
def is_chinese(text):
|
47 |
+
"""Checks if the input text contains Chinese characters."""
|
48 |
+
return bool(re.search(r'[\u4e00-\u9fff]', text))
|
49 |
+
|
50 |
# Main Streamlit app
|
51 |
def main():
|
52 |
+
"""Main Streamlit application."""
|
53 |
st.title("AI-Powered Sentiment Analysis and Summarization")
|
54 |
|
55 |
sentiment_analyzer, summarizer = load_models()
|
56 |
|
57 |
+
text = st.text_area(f"Enter the Chinese text here (Max {max(SENTIMENT_MAX_LENGTH, SUMMARIZER_MAX_LENGTH)} characters).....", height=200)
|
58 |
|
59 |
+
if st.button("Analyze"):
|
60 |
if text:
|
61 |
+
if not is_chinese(text):
|
62 |
+
st.error("Error: Please enter Chinese text.")
|
63 |
+
return
|
64 |
+
|
65 |
+
if len(text) > SENTIMENT_MAX_LENGTH:
|
66 |
+
st.error(f"Error: Input text for sentiment analysis exceeds the maximum length of {SENTIMENT_MAX_LENGTH} characters. Please shorten your text.")
|
67 |
+
return
|
68 |
+
|
69 |
+
if len(text) > SUMMARIZER_MAX_LENGTH:
|
70 |
+
st.error(f"Error: Input text for summarization exceeds the maximum length of {SUMMARIZER_MAX_LENGTH} characters. Please shorten your text.")
|
71 |
+
return
|
72 |
+
|
73 |
with st.spinner("Analyzing sentiment..."):
|
74 |
text_en = translate_text(text, target_language='en', source_language='zh-TW')
|
75 |
sentiment_result = sentiment_analysis(text_en, sentiment_analyzer)
|
76 |
+
label_map = {"LABEL_0": 'negative', "LABEL_1": 'neutral', "LABEL_2": 'positive'}
|
77 |
try:
|
78 |
sentiment_label = label_map.get(sentiment_result['label'], 'Unknown')
|
79 |
sentiment_score = sentiment_result['score']
|
|
|
89 |
st.write(story)
|
90 |
|
91 |
if __name__ == "__main__":
|
92 |
+
main()
|