import gradio as gr from transformers import pipeline, AutoTokenizer, AutoModelForSeq2SeqLM # 加载中文摘要模型 summarizer = pipeline("summarization", model="IDEA-CCNL/Randeng-Pegasus-523M-Summary-Chinese") # 加载中文情感分析模型 sentiment_analyzer = pipeline("sentiment-analysis", model="uer/roberta-base-finetuned-jd-binary-chinese") 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'] == 'positive' else "消极" sentiment_score = sentiment['score'] # 格式化输出 result = f"摘要:\n{summary}\n\n情感倾向:{sentiment_label}\n情感得分:{sentiment_score:.2f}" return result # 创建 Gradio 接口 iface = gr.Interface( fn=analyze_text, inputs=gr.Textbox(lines=10, label="输入中文文章"), outputs=gr.Textbox(label="分析结果"), title="中文文章摘要与情感分析", description="输入中文文章,获取摘要和情感分析结果。" ) # 启动接口 iface.launch()