eliwill commited on
Commit
39620fb
·
1 Parent(s): 95dc22a

Add main application

Browse files
Files changed (1) hide show
  1. app.py +66 -3
app.py CHANGED
@@ -7,14 +7,77 @@ import nltk
7
  from nltk import sent_tokenize
8
  nltk.download("punkt")
9
 
10
- krishnamurti_generator = pipeline("text-generation", model="eliwill/stoic-generator-10e")
11
- # stoic_generator = pipeline("text-generation", model="eliwill/stoic-generator-10e")
 
12
 
13
- ############### BUILDING DEMO ################################
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
  with gr.Blocks() as demo:
15
  gr.Markdown("""
16
  # Ask a Philsopher
17
  """
18
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
 
20
  demo.launch()
 
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['quote'][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
  with gr.Blocks() as demo:
60
  gr.Markdown("""
61
  # Ask a Philsopher
62
  """
63
  )
64
+ with gr.Row():
65
+ with gr.Column():
66
+ inp1 = gr.Textbox(placeholder="Place your question here...", label="Ask a question")
67
+ inp2 = gr.Dropdown(choices=["stoic", "krishnamurti"], value="stoic", label="Choose a philosopher")
68
+
69
+ with gr.Column():
70
+ out1 = gr.Textbox(
71
+ lines=3,
72
+ max_lines=10,
73
+ label="Answer"
74
+ )
75
+ out2 = gr.DataFrame(
76
+ headers=["Quotes"],
77
+ max_rows=5,
78
+ interactive=False,
79
+ wrap=True)
80
+ btn = gr.Button("Run")
81
+ btn.click(fn=main, inputs=[inp1,inp2], outputs=[out1,out2])
82
 
83
  demo.launch()