Spaces:
Sleeping
Sleeping
Rocky080808
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -1,3 +1,6 @@
|
|
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
from transformers import pipeline
|
3 |
|
@@ -42,3 +45,92 @@ def main():
|
|
42 |
|
43 |
if __name__ == "__main__":
|
44 |
main()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Codes that passed test
|
2 |
+
|
3 |
+
'''
|
4 |
import streamlit as st
|
5 |
from transformers import pipeline
|
6 |
|
|
|
45 |
|
46 |
if __name__ == "__main__":
|
47 |
main()
|
48 |
+
'''
|
49 |
+
|
50 |
+
# New codes to be tested
|
51 |
+
|
52 |
+
import streamlit as st
|
53 |
+
from transformers import pipeline
|
54 |
+
from langdetect import detect
|
55 |
+
|
56 |
+
# Load translation pipeline for multiple languages
|
57 |
+
@st.cache_resource # Cache the model to avoid reloading it
|
58 |
+
def load_translation_pipeline():
|
59 |
+
return pipeline("translation", model="facebook/m2m100_418M")
|
60 |
+
|
61 |
+
# Load sentiment analysis pipeline
|
62 |
+
@st.cache_resource # Cache the sentiment analysis model
|
63 |
+
def load_sentiment_pipeline():
|
64 |
+
return pipeline("sentiment-analysis", model="Rocky080808/finetuned-roberta-base")
|
65 |
+
|
66 |
+
# Function to detect language and translate to English
|
67 |
+
def translate_to_english(text, translation_pipeline):
|
68 |
+
# Detect the language of the input text
|
69 |
+
detected_language = detect(text)
|
70 |
+
|
71 |
+
# Supported languages: Chinese, Japanese, German, Spanish, French
|
72 |
+
language_map = {
|
73 |
+
'zh': "zh", # Chinese
|
74 |
+
'ja': "ja", # Japanese
|
75 |
+
'de': "de", # German
|
76 |
+
'es': "es", # Spanish
|
77 |
+
'fr': "fr" # French
|
78 |
+
}
|
79 |
+
|
80 |
+
if detected_language not in language_map:
|
81 |
+
return None, "Unsupported language"
|
82 |
+
|
83 |
+
# Translate the text to English using the detected language
|
84 |
+
translated_text = translation_pipeline(text, src_lang=language_map[detected_language], tgt_lang="en")
|
85 |
+
|
86 |
+
return translated_text[0]['translation_text'], detected_language
|
87 |
+
|
88 |
+
# Main application logic
|
89 |
+
def main():
|
90 |
+
# Load the translation and sentiment pipelines
|
91 |
+
translation_pipeline = load_translation_pipeline()
|
92 |
+
sentiment_pipeline = load_sentiment_pipeline()
|
93 |
+
|
94 |
+
st.title("Final Project Demonstration for Group 8")
|
95 |
+
st.write("This application supports customer comments sentiment analysis for an e-commerce company.")
|
96 |
+
st.write("You can input text in Chinese, Japanese, German, Spanish, or French. The text will be translated to English for sentiment analysis.")
|
97 |
+
|
98 |
+
user_input = st.text_input("Enter customer comments in supported languages:")
|
99 |
+
|
100 |
+
# Define a mapping from label to English descriptions
|
101 |
+
label_to_text = {
|
102 |
+
0: "Very dissatisfied, immediate follow-up is required.",
|
103 |
+
1: "Dissatisfied, please arrange follow-up.",
|
104 |
+
2: "Neutral sentiment, further case analysis is needed.",
|
105 |
+
3: "Satisfied, the customer may return for a purchase.",
|
106 |
+
4: "Very satisfied, the customer is very likely to return and recommend."
|
107 |
+
}
|
108 |
+
|
109 |
+
if user_input:
|
110 |
+
# Step 1: Translate the input text to English
|
111 |
+
translated_text, detected_language = translate_to_english(user_input, translation_pipeline)
|
112 |
+
|
113 |
+
if detected_language == "Unsupported language":
|
114 |
+
st.write("The input language is not supported. Please use Chinese, Japanese, German, Spanish, or French.")
|
115 |
+
else:
|
116 |
+
# Display the translated text
|
117 |
+
st.write(f"Detected language: {detected_language}")
|
118 |
+
st.write(f"Translated Text: {translated_text}")
|
119 |
+
|
120 |
+
# Step 2: Perform sentiment analysis on the translated text
|
121 |
+
result = sentiment_pipeline(translated_text)
|
122 |
+
label_str = result[0]["label"] # Get the label as a string, e.g., "LABEL_0"
|
123 |
+
label = int(label_str.split("_")[-1]) # Extract the numeric part of the label
|
124 |
+
confidence = result[0]["score"]
|
125 |
+
|
126 |
+
# Get the corresponding text description based on the label
|
127 |
+
sentiment_text = label_to_text.get(label, "Unrecognized sentiment")
|
128 |
+
|
129 |
+
st.write(f"Sentiment Analysis Result: {sentiment_text}")
|
130 |
+
st.write(f"Confidence Score: {confidence:.2f}")
|
131 |
+
|
132 |
+
if __name__ == "__main__":
|
133 |
+
main()
|
134 |
+
|
135 |
+
|
136 |
+
|