svjack commited on
Commit
8fe04b6
1 Parent(s): 995e0ad

Upload with huggingface_hub

Browse files
Files changed (3) hide show
  1. ._requirements.txt +0 -0
  2. app.py +45 -0
  3. requirements.txt +5 -0
._requirements.txt ADDED
Binary file (212 Bytes). View file
 
app.py ADDED
@@ -0,0 +1,45 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from datasets import load_from_disk, load_dataset
2
+ import pandas as pd
3
+ import os
4
+ import gradio as gr
5
+
6
+ #ds_with_embeddings = load_dataset("svjack/bloom-dialogue-generate-ds-zh", split="train")
7
+ ds_with_embeddings = load_dataset("svjack/context-dialogue-generate-ds-zh", split="train")
8
+ ds_with_embeddings.add_faiss_index(column='embeddings')
9
+ from sentence_transformers import SentenceTransformer
10
+ #encoder = SentenceTransformer("sentence-transformers/LaBSE")
11
+ encoder = SentenceTransformer("sentence-transformers/clip-ViT-B-32-multilingual-v1")
12
+
13
+ def retrieve_search_df(question = "这座教堂建在山上", top_k = 10):
14
+ question_embedding = encoder.encode(question)
15
+ scores, retrieved_examples = ds_with_embeddings.get_nearest_examples('embeddings', question_embedding, k=top_k)
16
+ sdf = pd.DataFrame(retrieved_examples)
17
+ sdf["scores"] = scores
18
+ return sdf[["sent", "dialogue", "scores"]]
19
+
20
+ example_sample = [
21
+ ["这座教堂建在山上", 3],
22
+ #["第一次世界大战结束了", 5],
23
+ ]
24
+
25
+ def demo_func(prefix, max_length):
26
+ max_length = max(int(max_length), 3)
27
+ l = retrieve_search_df(prefix, max_length)[["dialogue"]].values.tolist()
28
+ assert type(l) == type([])
29
+ return {
30
+ "Dialogue Context": l
31
+ }
32
+
33
+ demo = gr.Interface(
34
+ fn=demo_func,
35
+ inputs=[gr.Text(label = "Prefix"),
36
+ gr.Number(label = "Top K", value = 10)
37
+ ],
38
+ outputs="json",
39
+ title=f"Chinese Context Dialogue Generator 🐰 sample search demonstration",
40
+ #description = 'This _example_ was **drive** from <br/><b><h4>[https://github.com/svjack/Daliy-Dialogue](https://github.com/svjack/Daliy-Dialogue)</h4></b>\n',
41
+ examples=example_sample if example_sample else None,
42
+ cache_examples = False
43
+ )
44
+
45
+ demo.launch(server_name=None, server_port=None)
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ torch
2
+ transformers==4.20.1
3
+ datasets
4
+ faiss-cpu
5
+ sentence-transformers