標題和故事文字的語義搜索
tldrstory 是一個語義搜尋應用程序,用於搜尋與故事相關的標題和文字內容。 tldrstory 在文字上套用零樣本標籤,從而允許對內容進行動態分類。該框架還建立了一個 txtai 索引,可實現文字相似性搜尋。可自訂的 Streamlit 應用程式和 FastAPI 後端服務可讓使用者查看和分析處理的資料。
tldrstory 有一篇相應的 Medium 文章,涵蓋了本自述文件中的概念以及更多內容。一探究竟!
以下連結是使用 tldrstory 建立的範例應用程式。
最簡單的安裝方法是透過 pip 和 PyPI
pip install tldrstory
您也可以直接從 GitHub 安裝 tldrstory。建議使用 Python 虛擬環境。
pip install git+https://github.com/neuml/tldrstory
支援Python 3.8+
請參閱此連結以協助解決特定於環境的安裝問題。
安裝後,必須配置應用程式才能運作。 tldrstory 應用程式由三個獨立的進程組成:
本節將介紹如何啟動「體育新聞」應用程式。
mkdir sports
wget https://raw.githubusercontent.com/neuml/tldrstory/master/apps/sports/app.yml -O sports/app.yml
wget https://raw.githubusercontent.com/neuml/tldrstory/master/apps/sports/api.yml -O sports/api.yml
wget https://raw.githubusercontent.com/neuml/tldrstory/master/apps/sports/index-simple.yml -O sports/index.yml
wget https://raw.githubusercontent.com/neuml/tldrstory/master/src/python/tldrstory/app.py -O sports/app.py
python -m tldrstory.index sports/index.yml
CONFIG=sports/api.yml API_CLASS=tldrstory.api.API uvicorn " txtai.api:app " &
streamlit run sports/app.py sports/app.yml " Sports " " ? "
tldrstory 開箱即用,支援從 RSS 和 Reddit API 讀取資料。可以定義和配置其他資料來源。
下面顯示了自訂資料來源定義的範例。 neuspo 是一款即時體育賽事和新聞應用程式。此資料來源將 4 個預定義條目載入到文章資料庫中。
from tldrstory . source . source import Source
class Neuspo ( Source ):
"""
Articles have the following schema:
uid - unique id
source - source name
date - article date
title - article title
url - reference url for data
entry - entry date
"""
def run ( self ):
# List of articles created
articles = []
articles . append ( self . article ( "0" , "Neuspo" , self . now (), "Eagles defeat the Giants 22 - 21" ,
"https://neuspo.com/stream/34952e3919d685982c17735018b0197f" , self . now ()))
articles . append ( self . article ( "1" , "Neuspo" , self . now (), "Giants lose to the Eagles 22 - 21" ,
"https://t.co/e9FFgo0wgR?amp=1" , self . now ()))
articles . append ( self . article ( "2" , "Neuspo" , self . now (), "Rays beat Dodgers 6 to 4" ,
"https://neuspo.com/stream/6cb820b3ebadc086aa36b5cc4a0f575d" , self . now ()))
articles . append ( self . article ( "3" , "Neuspo" , self . now (), "Dodgers drop Game 2, 6-4" ,
"https://t.co/1hEQAShVnP?amp=1" , self . now ()))
return articles
讓我們使用 neuspo 作為資料來源重新運行上述步驟。首先刪除sports/data目錄,以確保我們建立一個新的資料庫。然後我們可以將上面的要點下載到 sports 目錄中。
# Delete the sports/data directory before running
wget https://gist.githubusercontent.com/davidmezzetti/9a6064d9a741acb89bd46eba9f906c26/raw/7058e97da82571005b2654b4ab908f25b9a04fe2/neuspo.py -O sports/neuspo.py
編輯 sports/index.yml 並刪除 rss 部分。將其替換為以下內容。
# Custom data source for neuspo
source : sports.neuspo.Neuspo
現在重新執行上述說明中的步驟 2-4。
以下部分定義屬於 tldrstory 應用程式一部分的每個程序的配置參數。
配置內容的索引。目前支援透過 Reddit API、RSS 和自訂用戶定義來源提取資料。
name : string
應用程式名稱
schedule : string
Cron 風格的字串,支援索引作業的計畫運行。有關 cron 字串的更多信息,請參閱此連結。
資料來源配置。
reddit.subreddit : name of subreddit to pull from
reddit.sort : sort type
reddit.time : time range
reddit.queries : list of text queries to run
執行一系列 Reddit API 查詢。需要建立並配置 Reddit API 金鑰才能使此方法發揮作用。身份驗證參數可以在環境中或在 praw.ini 檔案中設定。有關設定 Reddit API 帳戶的更多信息,請參閱此鏈接,只需只讀訪問權限即可。
有關如何配置查詢設定的更多詳細信息,請參閱 PRAW 文件。
rss : list of RSS urls
讀取一系列 RSS 來源並為找到的每個文章連結建立文章。
source : string
配置自訂來源。此參數採用完整的類別路徑作為字串,例如“tldrstory.source.rss.RSS”
自訂來源可以使用任何具有日期、文字字串和參考 URL 的資料。有關如何建立自訂來源的信息,請參閱 source.py 中的文件。 rss.py 和 reddit.py 是範例實作。
ignore : list of url patterns
要忽略的 url 模式列表。支援字串和正規表示式。
labels : dict
零樣本分類器的標籤配置。此組態設定一個類別以及主題值清單。
例子:
labels :
topic :
values : [Label 1, Label 2]
上面的範例使用兩個可能的標籤「標籤 1」和「標籤 2」配置類別「主題」。可以在此處設定任何標籤,並將使用大規模 NLP 模型將輸入文字分類到這些標籤中。
path : string
在哪裡儲存模型輸出,如果路徑尚不存在,則將建立路徑。
embeddings : dict
配置用於搜尋主題的 txtai 索引。有關詳細信息,請參閱 txtai 配置。
配置 FastAPI 支援的介面以提取索引資料。
path : string
模型索引的路徑。
預設應用程式由 Streamlit 支援並由 YAML 設定檔驅動。設定檔設定應用程式名稱、用於拉取內容的 API 端點以及元件配置。可以使用自訂 Streamlit 應用程式或任何其他應用程式來取代它,直接從 API 端點提取內容。
name : string
應用程式名稱
api : url
用於拉取內容的 API 端點。
description : string
用於建立側邊欄描述的 Markdown 字串。
queries.name : Queries drop down header
queries.values : List of values to use for queries drop down
配置查詢下拉框。這應該是要使用的預製查詢的清單。如果存在「Latest」值,它將查詢最後 N 篇文章。如果存在「--Search--」值,它將顯示另一個文字方塊以允許輸入自訂查詢。
filters : list
滑塊過濾器列表。這應該會對應到索引部分中配置的零樣本標籤。
chart.name : Chart name
chart.x : Chart x-axis column
chart.y : Chart y-axis column
chart.scale : Color scale for list of colors
chart.colors : List of colors
允許配置繪製兩個標籤點的散佈圖。此圖表可用於繪製標籤並將顏色套用於所套用的標籤。
" column name " : dynamic range of coloring
顯示結果詳細資訊的資料表。除了預設列之外,此部分還允許根據應用程式的零樣本標籤添加其他列。預設模式是顯示標籤的數值,但也可以套用一系列文字標籤。
例如:
對於 0 到 5 之間的值,上面將以紅色輸出文字「標籤 1」。