File size: 1,577 Bytes
63edb66
5fd301a
 
12159c1
5fd301a
 
 
 
bbd071f
12159c1
63edb66
5fd301a
63edb66
5fd301a
63edb66
12159c1
bbd071f
12159c1
 
bbd071f
 
12159c1
bbd071f
 
12159c1
bbd071f
12159c1
bbd071f
63edb66
5fd301a
aeefcf0
5fd301a
12159c1
 
5fd301a
97644cf
aeefcf0
5fd301a
 
63edb66
12159c1
bbd071f
 
 
63edb66
bbd071f
12159c1
 
 
 
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
import streamlit as st
from transformers import pipeline

# 加载模型
@st.cache_resource
def load_pipelines():
    sentiment_pipeline = pipeline(model="KeviniveK/CustomModel_IMDB")
    translation_pipeline = pipeline(
        "translation_en_to_zh",
        model="Helsinki-NLP/opus-mt-en-zh"
    )
    return sentiment_pipeline, translation_pipeline

sentiment_pipeline, translation_pipeline = load_pipelines()

# 标签映射
label_map = {
    "LABEL_0": "差评 | Negative",
    "LABEL_1": "好评 | Positive"
}

# 页面标题
st.title("影评分析与翻译 | Sentiment Analysis & Translation")
st.write("请输入一段英文影评,我们将分析其情感并翻译成中文。")
st.write("Enter an English movie review below. The app will analyze its sentiment and translate it into Chinese.")

# 用户输入
user_input = st.text_area("英文影评输入 | Enter English Movie Review", height=150)

if user_input:
    # 情感分析
    result = sentiment_pipeline(user_input)
    sentiment_raw = result[0]["label"]
    sentiment = label_map.get(sentiment_raw, sentiment_raw)
    confidence = result[0]["score"]

    # 翻译
    translation = translation_pipeline(user_input)
    translated_text = translation[0]["translation_text"]

    # 显示结果
    st.subheader("情感分析结果 | Sentiment Analysis Result")
    st.write(f"**情感 (Sentiment):** {sentiment}")
    st.write(f"**置信度 (Confidence):** {confidence:.2f}")

    st.subheader("中文翻译结果 | Chinese Translation")
    st.write(translated_text)

if __name__ == "__main__":
    pass