Upload web_demo.py
Browse files- demo/web_demo.py +90 -0
demo/web_demo.py
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import sys
|
| 4 |
+
import os
|
| 5 |
+
|
| 6 |
+
# 将项目根目录加入路径,以便能以包的形式导入 src
|
| 7 |
+
current_dir = os.path.dirname(os.path.abspath(__file__))
|
| 8 |
+
project_root = os.path.dirname(current_dir)
|
| 9 |
+
sys.path.append(project_root)
|
| 10 |
+
|
| 11 |
+
from src.predict import SentimentPredictor
|
| 12 |
+
|
| 13 |
+
# 初始化预测器
|
| 14 |
+
try:
|
| 15 |
+
predictor = SentimentPredictor()
|
| 16 |
+
print("模型加载成功!")
|
| 17 |
+
except Exception as e:
|
| 18 |
+
print(f"模型加载失败 (可能需要先运行训练): {e}")
|
| 19 |
+
# Fallback mock for demo UI preview
|
| 20 |
+
class MockPredictor:
|
| 21 |
+
def predict(self, text):
|
| 22 |
+
return {'sentiment': 'neutral', 'confidence': 0.0}
|
| 23 |
+
predictor = MockPredictor()
|
| 24 |
+
|
| 25 |
+
def analyze_sentiment(text):
|
| 26 |
+
if not text.strip():
|
| 27 |
+
return "请输入只有效的文本。", "N/A"
|
| 28 |
+
|
| 29 |
+
result = predictor.predict(text)
|
| 30 |
+
|
| 31 |
+
# 转换为友好显示
|
| 32 |
+
label_map = {
|
| 33 |
+
'positive': '😊 积极 (Positive)',
|
| 34 |
+
'neutral': '😐 中性 (Neutral)',
|
| 35 |
+
'negative': '😡 消极 (Negative)'
|
| 36 |
+
}
|
| 37 |
+
|
| 38 |
+
friendly_label = label_map.get(result['sentiment'], result['sentiment'])
|
| 39 |
+
confidence_score = float(result['confidence'])
|
| 40 |
+
|
| 41 |
+
# 返回:
|
| 42 |
+
# 1. 标签概率字典 (用于 Label 组件)
|
| 43 |
+
# 2. 文本详细结果
|
| 44 |
+
return {
|
| 45 |
+
'积极': confidence_score if result['sentiment'] == 'positive' else 0.0,
|
| 46 |
+
'中性': confidence_score if result['sentiment'] == 'neutral' else 0.0,
|
| 47 |
+
'消极': confidence_score if result['sentiment'] == 'negative' else 0.0
|
| 48 |
+
}, f"预测结果: {friendly_label}\n置信度: {confidence_score:.4f}"
|
| 49 |
+
|
| 50 |
+
# 构建 Gradio 界面
|
| 51 |
+
with gr.Blocks(title="中文情感分析演示") as demo:
|
| 52 |
+
gr.Markdown("# 🎭 中文情感分析 AI")
|
| 53 |
+
gr.Markdown("输入一段中文文本,模型将判断其情感倾向 (积极/消极/中性)。")
|
| 54 |
+
|
| 55 |
+
with gr.Row():
|
| 56 |
+
with gr.Column():
|
| 57 |
+
input_text = gr.Textbox(
|
| 58 |
+
label="输入文本",
|
| 59 |
+
placeholder="例如:这家餐厅真的太好吃了,强烈推荐!",
|
| 60 |
+
lines=5
|
| 61 |
+
)
|
| 62 |
+
analyze_btn = gr.Button("开始分析", variant="primary")
|
| 63 |
+
|
| 64 |
+
with gr.Column():
|
| 65 |
+
res_label = gr.Label(label="情感概率", num_top_classes=3)
|
| 66 |
+
res_text = gr.Textbox(label="详细结果")
|
| 67 |
+
|
| 68 |
+
# 示例
|
| 69 |
+
gr.Examples(
|
| 70 |
+
examples=[
|
| 71 |
+
["这就去把差评改了!"],
|
| 72 |
+
["物流太慢了,而且东西也是坏的,非常失望。"],
|
| 73 |
+
["如果不看价格的话,确实是不错的产品。"],
|
| 74 |
+
["今天天气真不错。"]
|
| 75 |
+
],
|
| 76 |
+
inputs=input_text
|
| 77 |
+
)
|
| 78 |
+
|
| 79 |
+
analyze_btn.click(
|
| 80 |
+
fn=analyze_sentiment,
|
| 81 |
+
inputs=input_text,
|
| 82 |
+
outputs=[res_label, res_text]
|
| 83 |
+
)
|
| 84 |
+
|
| 85 |
+
if __name__ == "__main__":
|
| 86 |
+
# Gradio 6.0+ 建议将 theme 放在 launch 中,或者 Blocks 中(警告说 moved to launch? 通常是 Block 构造参数)
|
| 87 |
+
# 但实际 Gradio 版本不同可能有差异。
|
| 88 |
+
# 根据用户报错 "The parameters have been moved ... to the launch() method ...: theme"
|
| 89 |
+
# 我们听从报错建议。
|
| 90 |
+
demo.launch(theme=gr.themes.Soft())
|