File size: 1,417 Bytes
58627fa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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

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