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