|
from transformers import AutoModel |
|
import streamlit as st |
|
import tempfile |
|
from langchain.text_splitter import RecursiveCharacterTextSplitter |
|
from langchain.document_loaders import PyPDFLoader |
|
from langchain.docstore.document import Document |
|
from langchain.chains.summarize import load_summarize_chain |
|
from langchain.chains import RetrievalQA |
|
from langchain.embeddings import HuggingFaceEmbeddings |
|
from langchain.vectorstores import Chroma |
|
from langchain.llms import LlamaCpp |
|
from langchain.prompts import PromptTemplate |
|
import os |
|
import pandas as pd |
|
|
|
|
|
prompt_template_questions = """ |
|
You are an expert in creating practice questions based on study material. |
|
Your goal is to prepare a student for their exam. You do this by asking questions about the text below: |
|
|
|
------------ |
|
{text} |
|
------------ |
|
|
|
Create questions that will prepare the student for their exam. Make sure not to lose any important information. |
|
|
|
QUESTIONS: |
|
""" |
|
|
|
PROMPT_QUESTIONS = PromptTemplate(template=prompt_template_questions, input_variables=["text"]) |
|
|
|
refine_template_questions = """ |
|
You are an expert in creating practice questions based on study material. |
|
Your goal is to help a student prepare for an exam. |
|
We have received some practice questions to a certain extent: {existing_answer}. |
|
We have the option to refine the existing questions or add new ones. |
|
(only if necessary) with some more context below. |
|
------------ |
|
{text} |
|
------------ |
|
|
|
Given the new context, refine the original questions in English. |
|
If the context is not helpful, please provide the original questions. |
|
|
|
QUESTIONS: |
|
""" |
|
|
|
REFINE_PROMPT_QUESTIONS = PromptTemplate( |
|
input_variables=["existing_answer", "text"], |
|
template=refine_template_questions, |
|
) |
|
|
|
|
|
st.title('Question-Answer Pair Generator with Zephyr-7B') |
|
st.markdown('<style>h1{color: orange; text-align: center;}</style>', unsafe_allow_html=True) |
|
|
|
|
|
uploaded_file = st.sidebar.file_uploader("Upload a PDF file", type=["pdf"]) |
|
|
|
|
|
file_path = None |
|
|
|
|
|
if uploaded_file: |
|
|
|
with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as temp_file: |
|
temp_file.write(uploaded_file.read()) |
|
file_path = temp_file.name |
|
|
|
|
|
if file_path: |
|
|
|
loader = PyPDFLoader(file_path) |
|
data = loader.load() |
|
|
|
|
|
text_question_gen = '' |
|
for page in data: |
|
text_question_gen += page.page_content |
|
|
|
|
|
text_splitter_question_gen = RecursiveCharacterTextSplitter(chunk_size=10000, chunk_overlap=50) |
|
|
|
|
|
text_chunks_question_gen = text_splitter_question_gen.split_text(text_question_gen) |
|
|
|
|
|
docs_question_gen = [Document(page_content=t) for t in text_chunks_question_gen] |
|
|
|
|
|
llm_question_gen = LlamaCpp( |
|
streaming = True, |
|
model_path = AutoModel.from_pretrained("TheBloke/zephyr-7B-beta-GGUF"), |
|
temperature=0.75, |
|
top_p=1, |
|
verbose=True, |
|
n_ctx=4096 |
|
) |
|
|
|
question_gen_chain = load_summarize_chain(llm=llm_question_gen, chain_type="refine", verbose=True, |
|
question_prompt=PROMPT_QUESTIONS, refine_prompt=REFINE_PROMPT_QUESTIONS) |
|
|
|
questions = question_gen_chain.run(docs_question_gen) |
|
|
|
llm_question_gen = LlamaCpp( |
|
streaming = True, |
|
model_path = AutoModel.from_pretrained("TheBloke/zephyr-7B-beta-GGUF"), |
|
temperature=0.75, |
|
top_p=1, |
|
verbose=True, |
|
n_ctx=4096 |
|
) |
|
|
|
|
|
embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2", model_kwargs={"device": "cpu"}) |
|
|
|
|
|
vector_store = Chroma.from_documents(docs_question_gen, embeddings) |
|
|
|
|
|
answer_gen_chain = RetrievalQA.from_chain_type(llm=llm_answer_gen, chain_type="stuff", |
|
retriever=vector_store.as_retriever(k=2)) |
|
|
|
|
|
question_list = questions.split("\n") |
|
|
|
|
|
question_answer_pairs = [] |
|
|
|
for question in question_list: |
|
st.write("Question: ", question) |
|
answer = answer_gen_chain.run(question) |
|
question_answer_pairs.append([question, answer]) |
|
st.write("Answer: ", answer) |
|
st.write("--------------------------------------------------\n\n") |
|
|
|
|
|
answers_dir = os.path.join(tempfile.gettempdir(), "answers") |
|
os.makedirs(answers_dir, exist_ok=True) |
|
|
|
|
|
qa_df = pd.DataFrame(question_answer_pairs, columns=["Question", "Answer"]) |
|
|
|
|
|
csv_file_path = os.path.join(answers_dir, "questions_and_answers.csv") |
|
qa_df.to_csv(csv_file_path, index=False) |
|
|
|
|
|
st.markdown('### Download Questions and Answers in CSV') |
|
st.download_button("Download Questions and Answers (CSV)", csv_file_path) |
|
|
|
|
|
if file_path: |
|
os.remove(file_path) |
|
|
|
|
|
|
|
|
|
|
|
|