File size: 1,182 Bytes
9c91424
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import gradio as gr
from Levenshtein import ratio
import json

data_path = "./data.json"

with open(data_path, "r") as f:
    documents_data = json.load(f)

def search_similar_texts(query, top_n=5):
    # クエリをベクトル化
    results = []

    for doc in documents_data:

        score = ratio(query, doc["text"])
        results.append({
            "vol": doc["vol"],
            "page": doc["page"],
            "score": score,
            "text": doc["text"]
        })
        # print(score)

    results.sort(key=lambda x: x["score"], reverse=True)
    
    return results[:top_n]

# Gradioインターフェースの作成
demo = gr.Interface(
    fn=search_similar_texts,
    inputs=[
        gr.Textbox(label="検索テキスト", placeholder="検索したいテキストを入力してください"),
        gr.Slider(minimum=1, maximum=10, value=5, step=1, label="表示件数")
    ],
    outputs=gr.JSON(),
    title="校異源氏物語 類似テキスト検索",
    description="テキストを入力すると、校異源氏物語の類似する箇所を検索します。",
    allow_flagging="never",
)

# インターフェースの起動
demo.launch()