Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import torch
|
| 3 |
+
import numpy as np
|
| 4 |
+
from datasets import load_dataset
|
| 5 |
+
from transformers import AutoTokenizer, AutoModel, pipeline
|
| 6 |
+
import faiss
|
| 7 |
+
|
| 8 |
+
device = torch.device("cuda") if torch.cuda.is_available() else torch.device("cpu")
|
| 9 |
+
|
| 10 |
+
DATASET_NAME = 'squad_v2'
|
| 11 |
+
raw_datasets = load_dataset(DATASET_NAME, split="train+validation").shard(num_shards=40, index=0)
|
| 12 |
+
|
| 13 |
+
raw_datasets = raw_datasets.filter(lambda x: len(x["answers"]["text"]) > 0)
|
| 14 |
+
|
| 15 |
+
columns_to_keep = ['id', 'context', 'question', 'answers']
|
| 16 |
+
raw_datasets = raw_datasets.remove_columns(set(raw_datasets.column_names) - set(columns_to_keep))
|
| 17 |
+
|
| 18 |
+
MODEL_NAME = "distilbert-base-uncased"
|
| 19 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
| 20 |
+
model = AutoModel.from_pretrained(MODEL_NAME).to(device)
|
| 21 |
+
|
| 22 |
+
def get_embeddings(text_list):
|
| 23 |
+
encoded_input = tokenizer(text_list, padding=True, truncation=True, return_tensors="pt")
|
| 24 |
+
encoded_input = {k: v.to(device) for k, v in encoded_input.items()}
|
| 25 |
+
model_output = model(**encoded_input)
|
| 26 |
+
return model_output.last_hidden_state[:, 0]
|
| 27 |
+
|
| 28 |
+
EMBEDDING_COLUMN = "question_embedding"
|
| 29 |
+
embedding_dataset = raw_datasets.map(
|
| 30 |
+
lambda x: {EMBEDDING_COLUMN: get_embeddings(x["question"]).detach().cpu().numpy()[0]}
|
| 31 |
+
)
|
| 32 |
+
embedding_dataset.add_faiss_index(column=EMBEDDING_COLUMN)
|
| 33 |
+
|
| 34 |
+
PIPELINE_NAME = "question-answering"
|
| 35 |
+
QA_MODEL_NAME = "DoNotChoke/distilbert-finetuned-squadv2"
|
| 36 |
+
qa_pipeline = pipeline(PIPELINE_NAME, model=QA_MODEL_NAME)
|
| 37 |
+
|
| 38 |
+
st.title("Question Answering System")
|
| 39 |
+
st.write("Nhập câu hỏi của bạn và hệ thống sẽ tìm kiếm câu trả lời phù hợp.")
|
| 40 |
+
|
| 41 |
+
user_question = st.text_input("Nhập câu hỏi:", "When did Beyonce start becoming popular?")
|
| 42 |
+
|
| 43 |
+
if st.button("Tìm kiếm câu trả lời"):
|
| 44 |
+
if user_question:
|
| 45 |
+
input_question_embedding = get_embeddings([user_question]).cpu().detach().numpy()
|
| 46 |
+
TOP_K = 5
|
| 47 |
+
scores, samples = embedding_dataset.get_nearest_examples(EMBEDDING_COLUMN, input_question_embedding, k=TOP_K)
|
| 48 |
+
|
| 49 |
+
st.subheader("Kết quả tìm kiếm:")
|
| 50 |
+
for idx, score in enumerate(scores):
|
| 51 |
+
context = samples["context"][idx]
|
| 52 |
+
answer = qa_pipeline(question=user_question, context=context)
|
| 53 |
+
|
| 54 |
+
st.write(f"**Top {idx + 1} (Score: {score:.4f})**")
|
| 55 |
+
st.write(f"**Context:** {context}")
|
| 56 |
+
st.write(f"**Answer:** {answer['answer']}")
|
| 57 |
+
st.write("---")
|