eliwill commited on
Commit
de161f5
·
1 Parent(s): 97ffa23

Add main application

Browse files
Files changed (1) hide show
  1. app.py +85 -0
app.py ADDED
@@ -0,0 +1,85 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ import nltk
7
+ from nltk import sent_tokenize
8
+ nltk.download("punkt")
9
+
10
+ # Loading in dataframes
11
+ krishnamurti_df = pd.read_json("krishnamurti_df.json")
12
+ stoic_df = pd.read_json("stoic_df.json")
13
+
14
+ # Loading in sentence_similarity model
15
+ sentence_similarity_model = "all-mpnet-base-v2"
16
+ model = SentenceTransformer(sentence_similarity_model)
17
+
18
+ # Loading in text-generation models
19
+ stoic_generator = pipeline("text-generation", model="eliwill/stoic-generator-10e")
20
+ krishnamurti_generator = pipeline("text-generation", model="distilgpt2")
21
+
22
+ # Creating philosopher dictionary
23
+ philosopher_dictionary = {
24
+ "stoic": {
25
+ "generator": stoic_generator,
26
+ "dataframe": stoic_df
27
+ },
28
+
29
+ "krishnamurti": {
30
+ "generator": krishnamurti_generator,
31
+ "dataframe": krishnamurti_df
32
+ }
33
+ }
34
+
35
+ ############### DEFINING FUNCTIONS ###########################
36
+
37
+ def ask_philosopher(philosopher, question):
38
+ """ Return first 5 sentences generated by question for the given philosopher model """
39
+
40
+ generator = philosopher_dictionary[philosopher]['generator']
41
+ answer = generator(question, min_length=100, max_length=120)[0]['generated_text'] # generate about 50 word tokens
42
+ answer = " ".join(sent_tokenize(answer)[:6]) # Get the first five sentences
43
+ return answer
44
+
45
+ def get_similar_quotes(philosopher, question):
46
+ """ Return top 5 most similar quotes to the question from a philosopher's dataframe """
47
+ df = philosopher_dictionary[philosopher]['dataframe']
48
+ question_embedding = model.encode(question)
49
+ sims = [util.dot_score(question_embedding, quote_embedding) for quote_embedding in df['Embedding']]
50
+ ind = np.argpartition(sims, -5)[-5:]
51
+ similar_sentences = [df['Quotes'][i] for i in ind]
52
+ top5quotes = pd.DataFrame(data = similar_sentences, columns=["Quotes"], index=range(1,6))
53
+ top5quotes['Quotes'] = top5quotes['Quotes'].str[:-1].str[:250] + "..."
54
+ return top5quotes
55
+
56
+ def main(question, philosopher):
57
+ return ask_philosopher(philosopher, question), get_similar_quotes(philosopher, question)
58
+
59
+
60
+ ############### BUILDING DEMO ################################
61
+ with gr.Blocks() as demo:
62
+ gr.Markdown("""
63
+ # Ask a Philsopher
64
+ """
65
+ )
66
+ with gr.Row():
67
+ with gr.Column():
68
+ inp1 = gr.Textbox(placeholder="Place your question here...", label="Ask a question")
69
+ inp2 = gr.Dropdown(choices=["stoic", "krishnamurti"], value="stoic", label="Choose a philosopher")
70
+
71
+ with gr.Column():
72
+ out1 = gr.Textbox(
73
+ lines=3,
74
+ max_lines=10,
75
+ label="Answer"
76
+ )
77
+ out2 = gr.DataFrame(
78
+ headers=["Quotes"],
79
+ max_rows=5,
80
+ interactive=False,
81
+ wrap=True)
82
+ btn = gr.Button("Run")
83
+ btn.click(fn=main, inputs=[inp1,inp2], outputs=[out1,out2])
84
+
85
+ demo.launch()