File size: 1,644 Bytes
58627fa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9c6d7e1
a6da41e
9c6d7e1
 
 
 
58627fa
 
 
 
9c6d7e1
 
58627fa
 
 
8feed39
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import random
import gradio as gr
from colbert.data import Queries
from colbert.infra import Run, RunConfig, ColBERTConfig
from colbert import Searcher


# def init():
searcher = None
with Run().context(RunConfig(nranks=1, experiment="medqa")):
    config = ColBERTConfig(
        root="./experiments",
    )
    searcher = Searcher(index="medqa_idx", config=config)
    

        
def search(query):
    results = searcher.search(query, k=5)
    responses=[]
    # idx = 0 
    for passage_id, _, _ in zip(*results):
          responses.append(searcher.collection[passage_id])
        #   idx = idx+1
    return responses





def chat(question):
    # history = history or []
    # message = message.lower()

    # if message.startswith("how many"):
    #     response = random.randint(1, 10)
    # elif message.startswith("how"):
    #     response = random.choice(["Great", "Good", "Okay", "Bad"])
    # elif message.startswith("where"):
    #     response = random.choice(["Here", "There", "Somewhere"])
    # else:
    #     response = "I don't know"
    responses = search(question)
    # history.append((message, response))
    return responses


title = "基于ColBERT的中文健康问题QA模型"
description = "用中文输入健康问题,比如 '高血压吃什么药物?', 程序返回5条跟问题最相关的回答。"



chatbot = gr.Chatbot().style(color_map=("green", "pink"))
demo = gr.Interface(
    chat,
    inputs=gr.Textbox(lines=2, placeholder="输入你的问题"),
    title = title,
    description=description, 
    outputs =["text", "text","text","text","text"]
)
if __name__ == "__main__":
    demo.launch()