Spaces:
Runtime error
Runtime error
gfhayworth
commited on
Commit
•
8dd4ecb
1
Parent(s):
0e866c7
Delete app.py
Browse files
app.py
DELETED
@@ -1,109 +0,0 @@
|
|
1 |
-
# -*- coding: utf-8 -*-
|
2 |
-
"""wiki_chat.ipynb
|
3 |
-
|
4 |
-
Automatically generated by Colaboratory.
|
5 |
-
|
6 |
-
Original file is located at
|
7 |
-
https://colab.research.google.com/drive/1P5rJeCXRSsDJw_1ksnHmodH6ng2Ot5NW
|
8 |
-
"""
|
9 |
-
|
10 |
-
# !pip install gradio
|
11 |
-
|
12 |
-
# !pip install -U sentence-transformers
|
13 |
-
|
14 |
-
# !pip install datasets
|
15 |
-
|
16 |
-
|
17 |
-
import gradio as gr
|
18 |
-
from sentence_transformers import SentenceTransformer, CrossEncoder, util
|
19 |
-
from torch import tensor as torch_tensor
|
20 |
-
from datasets import load_dataset
|
21 |
-
|
22 |
-
"""# import models"""
|
23 |
-
|
24 |
-
bi_encoder = SentenceTransformer('multi-qa-MiniLM-L6-cos-v1')
|
25 |
-
bi_encoder.max_seq_length = 256 #Truncate long passages to 256 tokens
|
26 |
-
|
27 |
-
#The bi-encoder will retrieve top_k documents. We use a cross-encoder, to re-rank the results list to improve the quality
|
28 |
-
cross_encoder = CrossEncoder('cross-encoder/ms-marco-MiniLM-L-6-v2')
|
29 |
-
|
30 |
-
"""# import datasets"""
|
31 |
-
dataset = load_dataset("gfhayworth/wiki_mini", split='train')
|
32 |
-
mypassages = list(dataset.to_pandas()['psg'])
|
33 |
-
|
34 |
-
dataset_embed = load_dataset("gfhayworth/wiki_mini_embed", split='train')
|
35 |
-
dataset_embed_pd = dataset_embed.to_pandas()
|
36 |
-
mycorpus_embeddings = torch_tensor(dataset_embed_pd.values)
|
37 |
-
|
38 |
-
def search(query, top_k=20, top_n = 1):
|
39 |
-
question_embedding = bi_encoder.encode(query, convert_to_tensor=True)
|
40 |
-
question_embedding = question_embedding #.cuda()
|
41 |
-
hits = util.semantic_search(question_embedding, mycorpus_embeddings, top_k=top_k)
|
42 |
-
hits = hits[0] # Get the hits for the first query
|
43 |
-
|
44 |
-
##### Re-Ranking #####
|
45 |
-
cross_inp = [[query, mypassages[hit['corpus_id']]] for hit in hits]
|
46 |
-
cross_scores = cross_encoder.predict(cross_inp)
|
47 |
-
|
48 |
-
# Sort results by the cross-encoder scores
|
49 |
-
for idx in range(len(cross_scores)):
|
50 |
-
hits[idx]['cross-score'] = cross_scores[idx]
|
51 |
-
|
52 |
-
hits = sorted(hits, key=lambda x: x['cross-score'], reverse=True)
|
53 |
-
predictions = hits[:top_n]
|
54 |
-
return predictions
|
55 |
-
# for hit in hits[0:3]:
|
56 |
-
# print("\t{:.3f}\t{}".format(hit['cross-score'], mypassages[hit['corpus_id']].replace("\n", " ")))
|
57 |
-
|
58 |
-
def get_text(qry):
|
59 |
-
predictions = search(qry)
|
60 |
-
prediction_text = []
|
61 |
-
for hit in predictions:
|
62 |
-
prediction_text.append("{}".format(mypassages[hit['corpus_id']]))
|
63 |
-
return prediction_text
|
64 |
-
|
65 |
-
# def prt_rslt(qry):
|
66 |
-
# rslt = get_text(qry)
|
67 |
-
# for r in rslt:
|
68 |
-
# print(r)
|
69 |
-
|
70 |
-
# prt_rslt("who is the best rapper in the world?")
|
71 |
-
|
72 |
-
"""# chat example"""
|
73 |
-
|
74 |
-
def chat(message, history):
|
75 |
-
history = history or []
|
76 |
-
message = message.lower()
|
77 |
-
|
78 |
-
responses = get_text(message)
|
79 |
-
for response in responses:
|
80 |
-
history.append((message, response))
|
81 |
-
return history, history
|
82 |
-
|
83 |
-
css=".gradio-container {background-color: lightgray}"
|
84 |
-
|
85 |
-
with gr.Blocks(css=css) as demo:
|
86 |
-
history_state = gr.State()
|
87 |
-
gr.Markdown('# WikiBot')
|
88 |
-
title='Wikipedia Chatbot'
|
89 |
-
description='chatbot with search on Wikipedia'
|
90 |
-
with gr.Row():
|
91 |
-
chatbot = gr.Chatbot()
|
92 |
-
with gr.Row():
|
93 |
-
message = gr.Textbox(label='Input your question here:',
|
94 |
-
placeholder='How many countries are in Europe?',
|
95 |
-
lines=1)
|
96 |
-
submit = gr.Button(value='Send',
|
97 |
-
variant='secondary').style(full_width=False)
|
98 |
-
submit.click(chat,
|
99 |
-
inputs=[message, history_state],
|
100 |
-
outputs=[chatbot, history_state])
|
101 |
-
gr.Examples(
|
102 |
-
examples=["How many countries are in Europe?",
|
103 |
-
"Was Roman Emperor Constantine I a Christian?",
|
104 |
-
"Who is the best rapper in the world?"],
|
105 |
-
inputs=message
|
106 |
-
)
|
107 |
-
|
108 |
-
demo.launch()
|
109 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|