Spaces:
Sleeping
Sleeping
Update app.py
Browse files1234567894 unmodified lines1415161718192021222324257 unmodified lines333435363738394 unmodified lines44454647484950import os# 1. 强制设置环境变量,解决 transformers 在新版环境下的 Task 识别问题os.environ["TF_USE_LEGACY_KERAS"] = "1"# 2. 显式导入 tf_keras,确保环境加载import tf_kerasfrom transformers import pipelineimport gradio as gr4 unmodified lines# 1. 完形填空功能# ----------------------------------------------------------------try:unmasker = pipeline('fill-mask', model='bert-base-chinese')except Exception as e:print(f"模型加载失败: {e}")exit()def fill_mask_ai(text):if "[MASK]" not in text:return "⚠️ 错误:请在句子中包含 [MASK] 符号"results = unmasker(text)7 unmodified lines# ----------------------------------------------------------------# 2. 阅读理解功能# ----------------------------------------------------------------qa_pipeline = pipeline('question-answering', model='uer/roberta-base-chinese-extractive-qa')def reading_comprehension(context, question):if not context or not question:4 unmodified lines# ----------------------------------------------------------------# 3. 情感分析功能# ----------------------------------------------------------------sentiment_pipeline = pipeline('text-classification', model='uer/roberta-base-finetuned-dianping-chinese')def sentiment_analysis(text):result = sentiment_pipeline(text)[0]12345678910114 unmodified lines1617181920212223242526272829307 unmodified lines38394041424344454 unmodified lines5051525354555657import os# 即使强制指定 pt,这行也建议保留以防万一os.environ["TF_USE_LEGACY_KERAS"] = "1"try:import tf_kerasexcept ImportError:passfrom transformers import pipelineimport gradio as gr4 unmodified lines# 1. 完形填空功能# ----------------------------------------------------------------try:# 强制指定 framework='pt' 使用 PyTorchunmasker = pipeline('fill-mask', model='bert-base-chinese', framework='pt')except Exception as e:print(f"模型加载失败: {e}")unmasker = Nonedef fill_mask_ai(text):if unmasker is None:return "模型加载失败,请检查 Logs"if "[MASK]" not in text:return "⚠️ 错误:请在句子中包含 [MASK] 符号"results = unmasker(text)7 unmodified lines# ----------------------------------------------------------------# 2. 阅读理解功能# ----------------------------------------------------------------# 强制指定 framework='pt' 使用 PyTorchqa_pipeline = pipeline('question-answering', model='uer/roberta-base-chinese-extractive-qa', framework='pt')def reading_comprehension(context, question):if not context or not question:4 unmodified lines# ----------------------------------------------------------------# 3. 情感分析功能# ----------------------------------------------------------------# 强制指定 framework='pt' 使用 PyTorchsentiment_pipeline = pipeline('text-classification', model='uer/roberta-base-finetuned-dianping-chinese', framework='pt')def sentiment_analysis(text):result = sentiment_pipeline(text)[0]
app.py+14-7