File size: 2,933 Bytes
403507a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
import pdfplumber
from langchain import PromptTemplate, LLMChain
from langchain.chat_models import HuggingFaceHub
from langchain.chains import ConversationalRetrievalChain
from langchain.vectorstores import Chroma
from langchain.document_loaders import TextLoader
from langchain.embeddings import HuggingFaceEmbeddings
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain.memory import ConversationBufferMemory
from transformers import AutoTokenizer, AutoModelForQuestionAnswering

# โหลดโมเดล ThaiBERT จาก Hugging Face
tokenizer = AutoTokenizer.from_pretrained("airesearch/wangchanberta-base-att-spm-uncased")
model = AutoModelForQuestionAnswering.from_pretrained("airesearch/wangchanberta-base-att-spm-uncased")

# ฟังก์ชันสำหรับอ่านเนื้อหาจาก PDF
def extract_text_from_pdf(pdf_file):
    with pdfplumber.open(pdf_file) as pdf:
        text = ""
        for page in pdf.pages:
            text += page.extract_text()
    return text

# ฟังก์ชันสำหรับการตอบคำถามด้วย ThaiBERT
def answer_question(question, context):
    inputs = tokenizer.encode_plus(question, context, return_tensors="pt")
    answer_start_scores, answer_end_scores = model(**inputs)
    answer_start = torch.argmax(answer_start_scores)
    answer_end = torch.argmax(answer_end_scores) + 1
    answer = tokenizer.convert_tokens_to_string(tokenizer.convert_ids_to_tokens(inputs['input_ids'][0][answer_start:answer_end]))
    return answer

# ตั้งค่าอินเตอร์เฟสของหน้าเว็บด้วย Streamlit
st.title("ThaiBERT PDF QA System")
uploaded_file = st.file_uploader("Upload a PDF", type="pdf")

if uploaded_file:
    # อ่านเนื้อหาจาก PDF
    pdf_text = extract_text_from_pdf(uploaded_file)

    # สร้าง chain สำหรับถามตอบ
    text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=0)
    docs = text_splitter.create_documents([pdf_text])

    embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/paraphrase-xlm-r-multilingual-v1")
    vector_store = Chroma.from_documents(documents=docs, embedding=embeddings)

    retriever = vector_store.as_retriever()
    memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True)

    qa_chain = ConversationalRetrievalChain(
        retriever=retriever,
        llm=HuggingFaceHub(repo_id="airesearch/wangchanberta-base-att-spm-uncased", model_kwargs={"temperature": 0}),
        memory=memory
    )

    # หน้าต่างสำหรับใส่คำถาม
    user_question = st.text_input("Ask a question about the PDF content")

    if user_question:
        response = qa_chain.run(user_question)
        st.write("Answer:", response)