QA / app.py
huriacane33's picture
Update app.py
7b1acfd verified
raw
history blame
1.89 kB
import streamlit as st
from transformers import AutoModelForQuestionAnswering, AutoTokenizer, pipeline
import pandas as pd
# Memuat model dan tokenizer IndoBERT
@st.cache_resource
def load_indobert_model():
model_name = "indobenchmark/indobert-base-p1"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForQuestionAnswering.from_pretrained(model_name)
return pipeline("question-answering", model=model, tokenizer=tokenizer)
qa_pipeline = load_indobert_model()
# Memuat dataset SOP
@st.cache_data
def load_sop_dataset():
return pd.read_csv("dataset.csv")
dataset = load_sop_dataset()
# Fungsi untuk menemukan konteks terbaik
def find_best_context(question, dataset):
best_score = 0
best_context = None
for _, row in dataset.iterrows():
overlap = len(set(question.lower().split()) & set(row["text"].lower().split()))
if overlap > best_score:
best_score = overlap
best_context = row["text"]
return best_context
# Antarmuka Streamlit
st.title("Sistem Penjawab Pertanyaan SOP dengan IndoBERT")
st.markdown("Ajukan pertanyaan seputar Prosedur Operasional Standar:")
question = st.text_area("Masukkan pertanyaan Anda:", "")
if st.button("Dapatkan Jawaban"):
if question:
with st.spinner("Mencari konteks yang relevan..."):
context = find_best_context(question, dataset)
if context:
with st.spinner("Menjawab pertanyaan Anda..."):
result = qa_pipeline(question=question, context=context)
st.success("Jawaban:")
st.write(result["answer"])
st.write("Skor Keyakinan:", result["score"])
else:
st.warning("Konteks yang relevan tidak ditemukan. Silakan coba pertanyaan lain.")
else:
st.warning("Silakan masukkan pertanyaan.")