File size: 1,583 Bytes
bafd7db
d43b643
bafd7db
 
7442775
 
43bb98f
bafd7db
 
43bb98f
 
 
bafd7db
 
 
 
 
 
 
43bb98f
bafd7db
 
43bb98f
 
bafd7db
 
 
 
43bb98f
 
 
 
 
 
 
bafd7db
 
 
 
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
import gradio as gr
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, AutoModelForSequenceClassification, pipeline

# 加载中文摘要模型
summarizer_tokenizer = AutoTokenizer.from_pretrained("fnlp/bart-base-chinese")
summarizer_model = AutoModelForSeq2SeqLM.from_pretrained("fnlp/bart-base-chinese")
summarizer = pipeline("summarization", model=summarizer_model, tokenizer=summarizer_tokenizer)

# 加载中文情感分析模型
sentiment_tokenizer = AutoTokenizer.from_pretrained("uer/roberta-base-finetuned-jd-binary-chinese")
sentiment_model = AutoModelForSequenceClassification.from_pretrained("uer/roberta-base-finetuned-jd-binary-chinese")
sentiment_analyzer = pipeline("sentiment-analysis", model=sentiment_model, tokenizer=sentiment_tokenizer)

def analyze_text(text):
    # 生成摘要
    summary = summarizer(text, max_length=150, min_length=50, do_sample=False)[0]['summary_text']
    
    # 分析情感
    sentiment = sentiment_analyzer(text)[0]
    sentiment_label = "积极" if sentiment['label'] == 'LABEL_1' else "消极"
    sentiment_score = sentiment['score']
    
    # 返回结果
    return summary, f"{sentiment_label} (置信度: {sentiment_score:.2f})"

# 创建 Gradio 接口
iface = gr.Interface(
    fn=analyze_text,
    inputs=gr.Textbox(lines=10, label="输入中文文本"),
    outputs=[
        gr.Textbox(label="摘要"),
        gr.Textbox(label="情感分析")
    ],
    title="中文文章摘要和情感分析",
    description="输入中文文本,获取摘要和情感分析结果。"
)

# 启动接口
iface.launch()