forgoogleweb / app.py
sanshizhang's picture
Update app.py
d43b643 verified
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()