File size: 2,026 Bytes
8fe04b6
 
 
 
 
 
98a91a3
 
8fe04b6
98a91a3
 
8fe04b6
7064c3c
8fe04b6
98a91a3
8fe04b6
 
 
 
 
43e415d
7064c3c
8fe04b6
 
 
 
 
b9d17c4
8fe04b6
 
 
 
 
 
 
 
 
 
 
 
 
299a219
8fe04b6
 
 
 
 
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
from datasets import load_from_disk, load_dataset
import pandas as pd
import os
import gradio as gr

#ds_with_embeddings = load_dataset("svjack/bloom-dialogue-generate-ds-zh", split="train")
ds_with_embeddings = load_dataset("svjack/context-dialogue-generate-ds-zh-v1", split="train")
ds_with_embeddings.add_faiss_index(column='L_emb')
from sentence_transformers import SentenceTransformer
encoder = SentenceTransformer("sentence-transformers/LaBSE")
#encoder = SentenceTransformer("sentence-transformers/clip-ViT-B-32-multilingual-v1")

def retrieve_search_df(question = "今天天气怎么样?", top_k = 10):
    question_embedding = encoder.encode(question)
    scores, retrieved_examples = ds_with_embeddings.get_nearest_examples('L_emb', question_embedding, k=top_k)
    sdf = pd.DataFrame(retrieved_examples)
    sdf["scores"] = scores
    return sdf[["sent", "dialogue", "scores"]]

example_sample = [
    ["有哪些有名的显卡?", 3],
    ["今天天气怎么样?", 3],
    #["第一次世界大战结束了", 5],
]

def demo_func(prefix, max_length):
    max_length = max(int(max_length), 3)
    l = retrieve_search_df(prefix, max_length)[["sent" ,"dialogue"]].values.tolist()
    assert type(l) == type([])
    return {
        "Dialogue Context": l
    }

demo = gr.Interface(
        fn=demo_func,
        inputs=[gr.Text(label = "Prefix"),
                gr.Number(label = "Top K", value = 10)
        ],
        outputs="json",
        title=f"Chinese Context Dialogue Generator 🐰 sample search demonstration",
        #description = 'This _example_ was **drive** from <br/><b><h4>[https://github.com/svjack/Daliy-Dialogue](https://github.com/svjack/Daliy-Dialogue)</h4></b>\n',
         description = 'This _example_ was **drive** from <br/><b><h4>[https://github.com/svjack/Context2Dialogue](https://github.com/svjack/Context2Dialogue)</h4></b>\n',
        examples=example_sample if example_sample else None,
        cache_examples = False
    )

demo.launch(server_name=None, server_port=None)