Spaces:
Sleeping
Sleeping
# Codes that passed test | |
''' | |
import streamlit as st | |
from transformers import pipeline | |
# Load model only once using caching | |
@st.cache_resource # Use Streamlit's caching to avoid reloading the model | |
def load_sentiment_pipeline(): | |
return pipeline("sentiment-analysis", model="Rocky080808/finetuned-roberta-base") | |
# Main application logic | |
def main(): | |
# Load the sentiment analysis pipeline only once | |
sentiment_pipeline = load_sentiment_pipeline() | |
st.title("Final Project Demonstration for Group 8") | |
st.write("This is an application for customer comments sentiment analysis for an e-commerce company.") | |
st.write("Please input the customer comments for analysis below:") | |
user_input = st.text_input("Enter customer comments here:") | |
# Define a mapping from label to English descriptions | |
label_to_text = { | |
0: "Very dissatisfied, immediate follow-up is required.", | |
1: "Dissatisfied, please arrange follow-up.", | |
2: "Neutral sentiment, further case analysis is needed.", | |
3: "Satisfied, the customer may return for a purchase.", | |
4: "Very satisfied, the customer is very likely to return and recommend." | |
} | |
if user_input: | |
# Call the preloaded pipeline to analyze sentiment | |
result = sentiment_pipeline(user_input) | |
label_str = result[0]["label"] # Get the label as a string, e.g., "LABEL_0" | |
label = int(label_str.split("_")[-1]) # Extract the numeric part of the label | |
confidence = result[0]["score"] | |
# Get the corresponding text description based on the label | |
sentiment_text = label_to_text.get(label, "Unrecognized sentiment") | |
st.write(f"Sentiment Analysis Result: {sentiment_text}") | |
# Hide the confidence score, no need to show to the users | |
# st.write(f"Confidence Score: {confidence:.2f}") | |
if __name__ == "__main__": | |
main() | |
''' | |
# New codes to be tested | |
import streamlit as st | |
from transformers import pipeline | |
from langdetect import detect | |
# Load translation pipeline for multiple languages | |
# Cache the model to avoid reloading it | |
def load_translation_pipeline(): | |
return pipeline("translation", model="facebook/m2m100_418M") | |
# Load sentiment analysis pipeline | |
# Cache the sentiment analysis model | |
def load_sentiment_pipeline(): | |
return pipeline("sentiment-analysis", model="Rocky080808/finetuned-roberta-base") | |
# Function to detect language and translate to English | |
def translate_to_english(text, translation_pipeline): | |
# Detect the language of the input text | |
detected_language = detect(text) | |
# Supported languages: Chinese, Japanese, German, Spanish, French | |
language_map = { | |
'zh': "zh", # Chinese | |
'ja': "ja", # Japanese | |
'de': "de", # German | |
'es': "es", # Spanish | |
'fr': "fr" # French | |
} | |
if detected_language not in language_map: | |
return None, "Unsupported language" | |
# Translate the text to English using the detected language | |
translated_text = translation_pipeline(text, src_lang=language_map[detected_language], tgt_lang="en") | |
return translated_text[0]['translation_text'], detected_language | |
# Main application logic | |
def main(): | |
# Load the translation and sentiment pipelines | |
translation_pipeline = load_translation_pipeline() | |
sentiment_pipeline = load_sentiment_pipeline() | |
st.title("Final Project Demonstration for Group 8") | |
st.write("This application supports customer comments sentiment analysis for an e-commerce company.") | |
st.write("You can input text in Chinese, Japanese, German, Spanish, or French. The text will be translated to English for sentiment analysis.") | |
user_input = st.text_input("Enter customer comments in supported languages:") | |
# Define a mapping from label to English descriptions | |
label_to_text = { | |
0: "Very dissatisfied, immediate follow-up is required.", | |
1: "Dissatisfied, please arrange follow-up.", | |
2: "Neutral sentiment, further case analysis is needed.", | |
3: "Satisfied, the customer may return for a purchase.", | |
4: "Very satisfied, the customer is very likely to return and recommend." | |
} | |
if user_input: | |
# Step 1: Translate the input text to English | |
translated_text, detected_language = translate_to_english(user_input, translation_pipeline) | |
if detected_language == "Unsupported language": | |
st.write("The input language is not supported. Please use Chinese, Japanese, German, Spanish, or French.") | |
else: | |
# Display the translated text | |
st.write(f"Detected language: {detected_language}") | |
st.write(f"Translated Text: {translated_text}") | |
# Step 2: Perform sentiment analysis on the translated text | |
result = sentiment_pipeline(translated_text) | |
label_str = result[0]["label"] # Get the label as a string, e.g., "LABEL_0" | |
label = int(label_str.split("_")[-1]) # Extract the numeric part of the label | |
confidence = result[0]["score"] | |
# Get the corresponding text description based on the label | |
sentiment_text = label_to_text.get(label, "Unrecognized sentiment") | |
st.write(f"Sentiment Analysis Result: {sentiment_text}") | |
st.write(f"Confidence Score: {confidence:.2f}") | |
if __name__ == "__main__": | |
main() | |