eliwill commited on
Commit
74de3c2
·
1 Parent(s): 5cb129d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -6
app.py CHANGED
@@ -1,14 +1,54 @@
1
  import gradio as gr
2
- def update(name):
3
- return f"Welcome to Gradio, {name}!", "hello_world"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
 
5
  with gr.Blocks() as demo:
6
- gr.Markdown("Start typing below and then click **Run** to see the output.")
 
 
 
7
  with gr.Row():
8
- inp = gr.Textbox(placeholder="What is your name?")
9
  with gr.Column():
10
- out1 = gr.Textbox()
11
- out2 = gr.Textbox()
 
 
 
 
 
 
 
 
 
12
  btn = gr.Button("Run")
13
  btn.click(fn=update, inputs=inp, outputs=[out1,out2])
14
 
 
1
  import gradio as gr
2
+ from transformers import pipeline
3
+ import numpy as np
4
+ import pandas as pd
5
+ from sentence_transformers import SentenceTransformer, util
6
+
7
+ # Loading in quotes dataset
8
+ df = pd.read_json("krishnamurti_quotes.json")
9
+
10
+ # Loading back in our sentence similarity and language model
11
+ model = SentenceTransformer("msmarco-roberta-base-v3") # best performing model
12
+
13
+ krishnamurti_generator = pipeline("text-generation", model="distilgpt2")
14
+
15
+ ############### DEFINING FUNCTIONS ###########################
16
+
17
+ def ask_krishnamurti(question):
18
+ answer = krishnamurti_generator(question)[0]['generated_text']
19
+ list_of_quotes = get_similar_quotes(question)
20
+ return answer, list_of_quotes
21
+
22
+ def get_similar_quotes(question):
23
+ question_embedding = model.encode(question)
24
+ sims = [util.dot_score(question_embedding, quote_embedding) for quote_embedding in df['Embedding']]
25
+ ind = np.argpartition(sims, -5)[-5:]
26
+ similar_sentences = [df['Quotes'][i] for i in ind]
27
+ top5quotes = pd.DataFrame(data = similar_sentences, columns=["Quotes"], index=range(1,6))
28
+ return top5quotes
29
+
30
+ def main(question):
31
+ return ask_krishnamurti(question), get_similar_quotes(question)
32
 
33
  with gr.Blocks() as demo:
34
+ gr.Markdown("""
35
+ # Ask Krishanmurti
36
+ """
37
+ )
38
  with gr.Row():
39
+ inp = gr.Textbox(placeholder="Place your question here...")
40
  with gr.Column():
41
+ out1 = gr.Textbox(
42
+ lines=3,
43
+ max_lines=10,
44
+ label="Answer"
45
+ )
46
+ out2 = gr.DataFrame(
47
+ headers=["Quotes"],
48
+ max_rows=5,
49
+ interactive=False,
50
+ wrap=True)]
51
+ )
52
  btn = gr.Button("Run")
53
  btn.click(fn=update, inputs=inp, outputs=[out1,out2])
54