Spaces:
Runtime error
Runtime error
File size: 6,853 Bytes
d67d72b 7804e79 d67d72b |
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 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 |
import streamlit as st
from doc_loading import get_article_text, read_pdf_text
from utils import get_topics
from llm_functions import generate_qa_pairs, evaluate_answer, get_conversational_chain, get_topics_from_chunk
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain_openai import OpenAIEmbeddings
from langchain.vectorstores import FAISS
from langchain_openai import ChatOpenAI
from dotenv import load_dotenv
load_dotenv()
# Setting up basics
st.set_page_config(page_title="LLM UC")
st.header("Let's check what you know")
# Greeting the user
st.write("Welcome!!")
# st.session_state
# Sidebar with selectbox
if "option" not in st.session_state:
option = st.selectbox("How are you going to input your document?",
("Upload PDF", "Blog link", "YouTube Link", "Paste copied article"))
# Conditionally show components based on user's choice
file_name = ""
main_text = ""
if option == "Upload PDF":
uploaded_file = st.file_uploader("Choose a PDF file", type=["pdf"], key="uploading_pdf")
if uploaded_file is not None:
# Process the uploaded file
st.write("PDF uploaded successfully!, we will read only first 5 pages")
main_text = read_pdf_text(uploaded_file)
elif option == "Paste copied article":
main_text = st.text_area("Paste your article here", key="article_key")
if st.button('Submit'):
# Process the pasted text
st.write("Text submitted successfully!")
else:
link_input = st.text_input("Paste your blog/youtube link here.", key='link_key')
if st.button('Submit'):
# Process the link
st.write("link submitted")
if option == "YouTube Link":
st.write("This functionality in under construction.")
main_text = ""
else:
# lets try now
article_text = get_article_text(link_input)
if article_text:
main_text = ""
st.write("This functionality in under construction.")
else:
st.write("Unable to fetch text from url. Can you please check the link?")
# Show a warning if the user hasn't selected an option or if the uploaded file is not a PDF
if option == "Upload PDF":
if uploaded_file is not None and uploaded_file.type != "application/pdf":
st.error("Please choose a PDF file only.")
if "total_text" not in st.session_state:
st.session_state['total_text'] = main_text
if len(main_text) > 0:
# creating chunks of the given article
text_splitter = RecursiveCharacterTextSplitter(
chunk_size=1000,
chunk_overlap=100,
length_function=len,
is_separator_regex=False
)
texts = text_splitter.create_documents([main_text])
# building vector database
embeddings = OpenAIEmbeddings()
# store in vector db
db = FAISS.from_documents(texts, embeddings)
hash_name = f"{option.replace(' ', '-')}"
db.save_local(f'faiss_{hash_name}_index')
# Create a toggle button
toggle_button_state = st.checkbox("check this, if i should quiz you")
# Display a message based on the toggle button state
if toggle_button_state:
# make the quiz
st.write("Quiz incoming...")
# give selection of toughness
toughness_selection = None
while "toughness_selection" not in st.session_state:
toughness_selection = st.selectbox("Select the question toughness",
("Easy", "Moderate", "Tough"))
st.session_state['toughness_selection'] = toughness_selection
top_topics = None
if "top_topics" not in st.session_state:
topics_chain = get_topics_from_chunk()
top_topics = get_topics(texts[:10], topics_chain)
top_topics.append("Any")
st.session_state['top_topics'] = top_topics
topic_selection = None
if "topic_selection" not in st.session_state:
top_topics = st.session_state['top_topics']
topic_selection = st.selectbox("Select the topic i should quiz from!!",
tuple(top_topics), key="topic_selection")
st.session_state['topics_selection'] = topic_selection
toughness_selection = st.session_state['toughness_selection']
if "response" not in st.session_state:
ques_chain = generate_qa_pairs()
topic_selection = st.session_state['topic_selection']
toughness_selection = st.session_state['toughness_selection']
# st.write(f"here we go, a {toughness_selection} level question from {topic_selection} topic.")
docs_for_questions = db.similarity_search(topic_selection, k=5)
response = ques_chain.invoke({"context": docs_for_questions,
"topic": topic_selection,
'toughness': toughness_selection})
st.session_state['response'] = response[0]
if "scoring" not in st.session_state:
eval_chain = evaluate_answer()
response = st.session_state['response']
st.write(f"\n Question: {response['question']}")
user_answer = st.text_input(f"Answer here: ", key="my_ans")
if st.button(f"Evaluate"):
score = eval_chain({"context": response['answer'],
"answer": user_answer})
st.write(f"You scored {score['score']}/10")
if int(score['score'])<6:
st.write(f"The correct answer would be: {response['answer']}")
else:
st.write("Good Job!!!")
st.session_state['scoring'] = score['score']
elif st.button("Don't know"):
st.write(f"The correct answer would be: {response['answer']}")
else:
st.write("What's your question?")
# let the user ask question.
if "input_question" not in st.session_state or st.session_state['input_question'] == "":
input_question = st.text_input("Here, input your question and click `Answer this`", key="Question")
st.session_state['input_question'] = input_question
answer_button = st.button('Answer this', key='my_question')
if answer_button:
input_question = st.session_state['input_question']
docs = db.similarity_search(input_question, k=5)
chain = get_conversational_chain()
response = chain({"input_documents" : docs,
"question": input_question,
})
st.write(response['output_text'])
del st.session_state['input_question']
|