KeviniveK commited on
Commit
bbd071f
·
verified ·
1 Parent(s): db0e387

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -17
app.py CHANGED
@@ -1,42 +1,50 @@
1
- # app.py
2
  import streamlit as st
3
  from transformers import pipeline
4
 
5
- # Initialize pipelines
6
  @st.cache_resource
7
  def load_pipelines():
8
  sentiment_pipeline = pipeline(model="KeviniveK/CustomModel_IMDB")
9
  translation_pipeline = pipeline(
10
- "translation_en_to_zh",
11
  model="Helsinki-NLP/opus-mt-en-zh"
12
  )
13
  return sentiment_pipeline, translation_pipeline
14
 
15
  sentiment_pipeline, translation_pipeline = load_pipelines()
16
 
17
- # Streamlit App
18
- st.title("IMDB Review Sentiment & Translation App")
19
- st.write("Enter an English movie review to analyze its sentiment and translate it into Chinese.")
 
 
 
 
 
 
 
 
 
 
20
 
21
- user_input = st.text_area("English Movie Review", height=150)
22
  if user_input:
23
- # Sentiment Analysis
24
  result = sentiment_pipeline(user_input)
25
- sentiment = result[0]["label"]
 
26
  confidence = result[0]["score"]
27
 
28
- # Translation
29
  translation = translation_pipeline(user_input)
30
  translated_text = translation[0]["translation_text"]
31
 
32
- # Display Results
33
- st.subheader("Sentiment Analysis Result")
34
- st.write(f"**Sentiment:** {sentiment}")
35
- st.write(f"**Confidence:** {confidence:.2f}")
36
 
37
- st.subheader("Chinese Translation")
38
  st.write(translated_text)
39
 
40
  if __name__ == "__main__":
41
- # Run the app with: streamlit run app.py
42
- pass
 
 
1
  import streamlit as st
2
  from transformers import pipeline
3
 
4
+ # 加载模型
5
  @st.cache_resource
6
  def load_pipelines():
7
  sentiment_pipeline = pipeline(model="KeviniveK/CustomModel_IMDB")
8
  translation_pipeline = pipeline(
9
+ "translation_en_to_zh",
10
  model="Helsinki-NLP/opus-mt-en-zh"
11
  )
12
  return sentiment_pipeline, translation_pipeline
13
 
14
  sentiment_pipeline, translation_pipeline = load_pipelines()
15
 
16
+ # 标签映射
17
+ label_map = {
18
+ "LABEL_0": "差评 / Negative",
19
+ "LABEL_1": "好评 / Positive"
20
+ }
21
+
22
+ # 页面标题
23
+ st.title("影评分析与翻译 | Sentiment Analysis & Translation")
24
+ st.write("请输入一段英文影评,我们将分析其情感并翻译成中文。")
25
+ st.write("Enter an English movie review below. The app will analyze its sentiment and translate it into Chinese.")
26
+
27
+ # 用户输入
28
+ user_input = st.text_area("英文影评输入 | Enter English Movie Review", height=150)
29
 
 
30
  if user_input:
31
+ # 情感分析
32
  result = sentiment_pipeline(user_input)
33
+ sentiment_raw = result[0]["label"]
34
+ sentiment = label_map.get(sentiment_raw, sentiment_raw)
35
  confidence = result[0]["score"]
36
 
37
+ # 翻译
38
  translation = translation_pipeline(user_input)
39
  translated_text = translation[0]["translation_text"]
40
 
41
+ # 显示结果
42
+ st.subheader("情感分析结果 | Sentiment Analysis Result")
43
+ st.write(f"**情感 (Sentiment):** {sentiment}")
44
+ st.write(f"**置信度 (Confidence):** {confidence:.2f}")
45
 
46
+ st.subheader("中文翻译结果 | Chinese Translation")
47
  st.write(translated_text)
48
 
49
  if __name__ == "__main__":
50
+ pass