不知不覺走到第十三天了,覺得自己真的在慢慢累積起一個生程式AI工具箱。之前試過摘要、翻譯、情感分析,每一個功能單看都滿有趣的,但如果能把它們串在一起,不就更實用了嗎?
所以今天想做一個小小應用,就當成是鐵人賽的期中作品的概念吧!
目標很單純:我輸入一段文字,這個小助手可以幫我快速做三件事:抓重點(摘要)、翻成中文(翻譯)、順便看看裡面情緒是偏正面還是負面(情感分析)。
實際操作
使用summarization、translation、sentiment-analysis pipelines,在Colab中輸入程式碼。
from transformers import pipeline
# 建立 pipeline
summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
translator = pipeline("translation_en_to_zh", model="Helsinki-NLP/opus-mt-en-zh")
sentiment = pipeline("sentiment-analysis")
text = """
Artificial intelligence is changing how we live and work.
It has applications in healthcare, education, and entertainment.
However, challenges such as ethics and data privacy remain.
"""
# 摘要
summary = summarizer(text, max_length=50, min_length=20, do_sample=False)[0]['summary_text']
# 翻譯
translation = translator(summary, max_length=60)[0]['translation_text']
# 情感分析
sentiment_result = sentiment(summary)[0]
print("摘要:", summary)
print("翻譯:", translation)
print("情感分析:", sentiment_result)
得出的結果為
可以看到不論是摘要、翻譯還是情感分析都蠻成功的。只是在情感分析部分,結果判定是正面,而且分數0.85代表模型信心蠻高的,雖然這段文字其實是中性偏正面,但也還算合理。
今天的整合小實驗雖然蠻簡單的,但我覺得這就是AI工具的價值所在,把一些原本要分開做的任務整合起來,一次完成。未來如果放到醫院客服或病歷系統裡,就能讓醫護人員更快處理資訊,把時間留給更重要的事。