Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,50 +1,41 @@
|
|
| 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="
|
| 11 |
)
|
| 12 |
return sentiment_pipeline, translation_pipeline
|
| 13 |
|
| 14 |
sentiment_pipeline, translation_pipeline = load_pipelines()
|
| 15 |
|
| 16 |
-
# 标签映射
|
| 17 |
label_map = {
|
| 18 |
-
"LABEL_0": "差评
|
| 19 |
-
"LABEL_1": "好评
|
| 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 |
-
|
| 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
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
from transformers import pipeline
|
| 3 |
|
|
|
|
| 4 |
@st.cache_resource
|
| 5 |
def load_pipelines():
|
| 6 |
sentiment_pipeline = pipeline(model="KeviniveK/CustomModel_IMDB")
|
| 7 |
+
# 轻量化翻译模型
|
| 8 |
translation_pipeline = pipeline(
|
| 9 |
"translation_en_to_zh",
|
| 10 |
+
model="google/t5-small"
|
| 11 |
)
|
| 12 |
return sentiment_pipeline, translation_pipeline
|
| 13 |
|
| 14 |
sentiment_pipeline, translation_pipeline = load_pipelines()
|
| 15 |
|
|
|
|
| 16 |
label_map = {
|
| 17 |
+
"LABEL_0": "差评 (Negative)",
|
| 18 |
+
"LABEL_1": "好评 (Positive)"
|
| 19 |
}
|
| 20 |
|
|
|
|
| 21 |
st.title("影评分析与翻译 | Sentiment Analysis & Translation")
|
| 22 |
st.write("请输入一段英文影评,我们将分析其情感并翻译成中文。")
|
| 23 |
st.write("Enter an English movie review below. The app will analyze its sentiment and translate it into Chinese.")
|
| 24 |
|
|
|
|
| 25 |
user_input = st.text_area("英文影评输入 | Enter English Movie Review", height=150)
|
| 26 |
|
| 27 |
if user_input:
|
|
|
|
| 28 |
result = sentiment_pipeline(user_input)
|
| 29 |
+
sentiment = label_map[result[0]["label"]]
|
|
|
|
| 30 |
confidence = result[0]["score"]
|
| 31 |
|
| 32 |
+
# 直接调用翻译 pipeline
|
| 33 |
translation = translation_pipeline(user_input)
|
| 34 |
translated_text = translation[0]["translation_text"]
|
| 35 |
|
|
|
|
| 36 |
st.subheader("情感分析结果 | Sentiment Analysis Result")
|
| 37 |
st.write(f"**情感 (Sentiment):** {sentiment}")
|
| 38 |
st.write(f"**置信度 (Confidence):** {confidence:.2f}")
|
| 39 |
|
| 40 |
st.subheader("中文翻译结果 | Chinese Translation")
|
| 41 |
+
st.write(translated_text)
|
|
|
|
|
|
|
|
|