Rocky080808 commited on
Commit
b1f033a
·
verified ·
1 Parent(s): cb51f0d

Update app.py

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