Spaces:
Build error
Build error
File size: 4,176 Bytes
de161f5 39620fb eb2cf26 de161f5 39620fb 98f174a e14a067 39620fb 0305cba 39620fb e14a067 39620fb 0305cba 39620fb e14a067 680755c eb2cf26 0918b7f eb2cf26 39620fb edb1e33 39620fb edb1e33 39620fb edb1e33 0a40299 edb1e33 39620fb e14a067 4fd044c 39620fb cc8684f de161f5 6f4bf29 de161f5 508039d 39620fb 508039d 68a85c3 508039d edb1e33 508039d dae3b38 21c8602 d8ea1da 142631d 39620fb 4fd044c de161f5 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
import gradio as gr
from transformers import pipeline
import numpy as np
import pandas as pd
from sentence_transformers import SentenceTransformer, util
import nltk
from nltk import sent_tokenize
nltk.download("punkt")
# Loading in dataframes
krishnamurti_df = pd.read_json("krishnamurti_df.json")
stoic_df = pd.read_json("stoic_df.json")
alan_df = pd.read_json("alan-watts_df.json")
# Loading in sentence_similarity model
sentence_similarity_model = "all-mpnet-base-v2"
model = SentenceTransformer(sentence_similarity_model)
# Loading in text-generation models
stoic_generator = pipeline("text-generation", model="eliwill/stoic-generator-10e")
krishnamurti_generator = pipeline("text-generation", model="eliwill/distilgpt2-finetuned-final-project")
alan_generator = pipeline("text-generation", model="eliwill/alan-watts-8e")
# Creating philosopher dictionary
philosopher_dictionary = {
"Marcus Aurelius": {
"generator": stoic_generator,
"dataframe": stoic_df,
"image": "marcus-aurelius.jpg"
},
"Krishnamurti": {
"generator": krishnamurti_generator,
"dataframe": krishnamurti_df,
"image": "krishnamurti.jpg"
},
"Alan Watts": {
"generator": alan_generator ,
"dataframe": alan_df ,
"image": "rsz_1alan_watts.jpg"
}
}
############### DEFINING FUNCTIONS ###########################
def ask_philosopher(philosopher, question):
""" Return first 5 sentences generated by question for the given philosopher model """
generator = philosopher_dictionary[philosopher]['generator']
answer = generator(question, min_length=100, max_length=120)[0]['generated_text'] # generate about 50 word tokens
answer = " ".join(sent_tokenize(answer)[:6]) # Get the first five sentences
return answer
def get_similar_quotes(philosopher, question):
""" Return top 5 most similar quotes to the question from a philosopher's dataframe """
df = philosopher_dictionary[philosopher]['dataframe']
question_embedding = model.encode(question)
sims = [util.dot_score(question_embedding, quote_embedding) for quote_embedding in df['Embedding']]
ind = np.argpartition(sims, -5)[-5:]
similar_sentences = [df['quote'][i] for i in ind]
top5quotes = pd.DataFrame(data = similar_sentences, columns=["Quotes"], index=range(1,6))
top5quotes['Quotes'] = top5quotes['Quotes'].str[:-1].str[:250] + "..."
return top5quotes
def main(question, philosopher):
out_image = philosopher_dictionary[philosopher]['image']
return ask_philosopher(philosopher, question), get_similar_quotes(philosopher, question), out_image
with gr.Blocks(css=".gradio-container {background-image: url('file=blue_mountains.jpg')} # title {color: #F0FFFF}") as demo:
gr.Markdown("""
# Ask a Philsopher
""",
elem_id="title"
)
with gr.Row():
with gr.Column():
inp1 = gr.Textbox(placeholder="Place your question here...", label="Ask a question", elem_id="title")
inp2 = gr.Dropdown(choices=["Alan Watts", "Marcus Aurelius", "Krishnamurti"], value="Marcus Aurelius", label="Choose a philosopher")
out1 = gr.Textbox(
lines=3,
max_lines=10,
label="Answer"
)
with gr.Row():
out_image = gr.Image(label="Picture", image_mode="L")
out2 = gr.DataFrame(
headers=["Quotes"],
max_rows=5,
interactive=False,
wrap=True,
value=[["When you arise in the morning, think of what a precious privilege it is to be alive – to breathe, to think, to enjoy, to love."],
["Each day provides its own gifts."],
["Only time can heal what reason cannot."],
["He who is brave is free."],
["First learn the meaning of what you say, and then speak."]]
)
btn = gr.Button("Run")
btn.click(fn=main, inputs=[inp1,inp2], outputs=[out1,out2,out_image])
demo.launch() |