Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,32 +1,56 @@
|
|
| 1 |
-
|
|
|
|
| 2 |
from transformers import pipeline
|
| 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 |
if __name__ == "__main__":
|
| 32 |
-
|
|
|
|
| 1 |
+
# app.py
|
| 2 |
+
import gradio as gr
|
| 3 |
from transformers import pipeline
|
| 4 |
|
| 5 |
+
# 初始化模型pipeline
|
| 6 |
+
# 情感分析模型(假设输出格式为 {'label': 'POSITIVE/NEGATIVE', 'score': float})
|
| 7 |
+
sentiment_pipeline = pipeline(
|
| 8 |
+
"text-classification",
|
| 9 |
+
model="KeviniveK/CustomModel_IMDB"
|
| 10 |
+
)
|
| 11 |
|
| 12 |
+
# 翻译模型(英文->中文)
|
| 13 |
+
translation_pipeline = pipeline(
|
| 14 |
+
"translation",
|
| 15 |
+
model="Helsinki-NLP/opus-mt-en-zh"
|
| 16 |
+
)
|
| 17 |
|
| 18 |
+
def analyze_and_translate(text):
|
| 19 |
+
# 情感分析
|
| 20 |
+
sentiment_result = sentiment_pipeline(text)[0]
|
| 21 |
+
|
| 22 |
+
# 翻译处理
|
| 23 |
+
translation_result = translation_pipeline(text)[0]['translation_text']
|
| 24 |
+
|
| 25 |
+
# 格式化置信度为百分比
|
| 26 |
+
confidence = f"{sentiment_result['score']*100:.2f}%"
|
| 27 |
+
|
| 28 |
+
return {
|
| 29 |
+
"情感分类": sentiment_result['label'],
|
| 30 |
+
"置信度": confidence,
|
| 31 |
+
"中文翻译": translation_result
|
| 32 |
+
}
|
| 33 |
|
| 34 |
+
# 创建Gradio界面
|
| 35 |
+
demo = gr.Interface(
|
| 36 |
+
fn=analyze_and_translate,
|
| 37 |
+
inputs=gr.Textbox(
|
| 38 |
+
label="英文评价输入",
|
| 39 |
+
placeholder="Enter your English review here...",
|
| 40 |
+
lines=3
|
| 41 |
+
),
|
| 42 |
+
outputs=[
|
| 43 |
+
gr.Textbox(label="情感分类"),
|
| 44 |
+
gr.Textbox(label="置信度"),
|
| 45 |
+
gr.Textbox(label="中文翻译")
|
| 46 |
+
],
|
| 47 |
+
title="🎬 IMDB评价情感分析+翻译系统",
|
| 48 |
+
description="输入英文影评,获取情感分析结果(分类+置信度)和中文翻译",
|
| 49 |
+
examples=[
|
| 50 |
+
["This movie is absolutely fantastic! The acting was superb and the plot kept me engaged throughout."],
|
| 51 |
+
["A terrible waste of time. The storyline was confusing and the acting felt forced."]
|
| 52 |
+
]
|
| 53 |
+
)
|
| 54 |
|
| 55 |
if __name__ == "__main__":
|
| 56 |
+
demo.launch()
|