science-lab / app.py
LawalAfeez's picture
Update app.py
aa9211d
import gradio as gr
from data_library import embedded_form
import pandas as pd
from embed import sample_embedding
import faiss
embedded_form=embedded_form["train"]
embedded_form.add_faiss_index("embedding")
# gradio function
title="""<center>
<H3 style="background-color:powderblue;">SEARCH FOR SCIENCE RELATED(BIO,PHY AND CHEM)</H3></center>"""
description="""<center><h4>This app is created to help give answers to high school science related questions</h4></center>"""
def input_text1(text):
question_embedding =sample_embedding([text])
question_embedding=question_embedding["embedding"]
scores, samples = embedded_form.get_nearest_examples(
"embedding", question_embedding, k=5
)
dataframe=pd.DataFrame(samples)
dataframe["scores"]=scores
dataframe=dataframe.sort_values("scores",ascending=False).reset_index(drop=True)
return dataframe.loc[0,"support"]
def input_text2(text):
question_embedding =sample_embedding([text])
question_embedding=question_embedding["embedding"]
scores, samples = embedded_form.get_nearest_examples(
"embedding", question_embedding, k=5
)
dataframe=pd.DataFrame(samples)
dataframe["scores"]=scores
dataframe=dataframe.sort_values("scores",ascending=False).reset_index(drop=True)
return dataframe.loc[1,"support"]
def input_text3(text):
question_embedding =sample_embedding([text])
question_embedding=question_embedding["embedding"]
scores, samples = embedded_form.get_nearest_examples(
"embedding", question_embedding, k=5
)
dataframe=pd.DataFrame(samples)
dataframe["scores"]=scores
dataframe=dataframe.sort_values("scores",ascending=False).reset_index(drop=True)
return dataframe.loc[2,"support"]
def input_text4(text):
question_embedding =sample_embedding([text])
question_embedding=question_embedding["embedding"]
scores, samples = embedded_form.get_nearest_examples(
"embedding", question_embedding, k=5
)
dataframe=pd.DataFrame(samples)
dataframe["scores"]=scores
dataframe=dataframe.sort_values("scores",ascending=False).reset_index(drop=True)
return dataframe.loc[3,"support"]
def input_text5(text):
question_embedding =sample_embedding([text])
question_embedding=question_embedding["embedding"]
scores, samples = embedded_form.get_nearest_examples(
"embedding", question_embedding, k=5
)
dataframe=pd.DataFrame(samples)
dataframe["scores"]=scores
dataframe=dataframe.sort_values("scores",ascending=False).reset_index(drop=True)
return dataframe.loc[4,"support"]
answer1=gr.Interface(input_text1,inputs=gr.Textbox(label="Search"),outputs=gr.Textbox(label="Support 1"))
answer2=gr.Interface(input_text2,inputs=gr.Textbox(label="Search"),outputs=gr.Textbox(label="Support 2"))
answer3=gr.Interface(input_text3,inputs=gr.Textbox(label="Search"),outputs=gr.Textbox(label="Support 3"))
answer4=gr.Interface(input_text4,inputs=gr.Textbox(label="Search"),outputs=gr.Textbox(label="Support 4"))
answer5=gr.Interface(input_text5,inputs=gr.Textbox(label="Search"),outputs=gr.Textbox(label="Support 5"))
demo=gr.Parallel(answer1,answer2,answer3,answer4,answer5,description=description,title=title)
if __name__ == "__main__":
demo.launch(debug=True)