Spaces:
Sleeping
Sleeping
huriacane33
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -1,60 +1,52 @@
|
|
1 |
import streamlit as st
|
2 |
-
from transformers import pipeline
|
3 |
import pandas as pd
|
4 |
-
import re
|
5 |
|
6 |
-
#
|
7 |
@st.cache_resource
|
8 |
-
def
|
9 |
-
|
10 |
-
|
|
|
|
|
11 |
|
12 |
-
qa_pipeline =
|
13 |
|
14 |
-
#
|
15 |
@st.cache_data
|
16 |
def load_sop_dataset():
|
17 |
-
"
|
18 |
-
return pd.read_csv("dataset.csv") # Ensure this file is uploaded to your Hugging Face Space
|
19 |
|
20 |
dataset = load_sop_dataset()
|
21 |
|
22 |
-
#
|
23 |
def find_best_context(question, dataset):
|
24 |
-
"""Find the single best context for a given question."""
|
25 |
best_score = 0
|
26 |
best_context = None
|
27 |
-
|
28 |
for _, row in dataset.iterrows():
|
29 |
-
# Simple heuristic: Count the number of overlapping words
|
30 |
overlap = len(set(question.lower().split()) & set(row["text"].lower().split()))
|
31 |
if overlap > best_score:
|
32 |
best_score = overlap
|
33 |
best_context = row["text"]
|
34 |
-
|
35 |
return best_context
|
36 |
|
37 |
-
# Streamlit
|
38 |
-
st.title("SOP
|
39 |
-
st.markdown("
|
40 |
|
41 |
-
|
42 |
-
question = st.text_area("Enter your question:", "")
|
43 |
|
44 |
-
|
45 |
-
if st.button("Get Answer"):
|
46 |
if question:
|
47 |
-
with st.spinner("
|
48 |
-
# Automatically find the most relevant context
|
49 |
context = find_best_context(question, dataset)
|
50 |
-
|
51 |
if context:
|
52 |
-
with st.spinner("
|
53 |
result = qa_pipeline(question=question, context=context)
|
54 |
-
st.success("
|
55 |
st.write(result["answer"])
|
56 |
-
st.write("
|
57 |
else:
|
58 |
-
st.warning("
|
59 |
else:
|
60 |
-
st.warning("
|
|
|
1 |
import streamlit as st
|
2 |
+
from transformers import AutoModelForQuestionAnswering, AutoTokenizer, pipeline
|
3 |
import pandas as pd
|
|
|
4 |
|
5 |
+
# Memuat model dan tokenizer IndoBERT
|
6 |
@st.cache_resource
|
7 |
+
def load_indobert_model():
|
8 |
+
model_name = "indobenchmark/indobert-base-p1"
|
9 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
10 |
+
model = AutoModelForQuestionAnswering.from_pretrained(model_name)
|
11 |
+
return pipeline("question-answering", model=model, tokenizer=tokenizer)
|
12 |
|
13 |
+
qa_pipeline = load_indobert_model()
|
14 |
|
15 |
+
# Memuat dataset SOP
|
16 |
@st.cache_data
|
17 |
def load_sop_dataset():
|
18 |
+
return pd.read_csv("dataset.csv")
|
|
|
19 |
|
20 |
dataset = load_sop_dataset()
|
21 |
|
22 |
+
# Fungsi untuk menemukan konteks terbaik
|
23 |
def find_best_context(question, dataset):
|
|
|
24 |
best_score = 0
|
25 |
best_context = None
|
|
|
26 |
for _, row in dataset.iterrows():
|
|
|
27 |
overlap = len(set(question.lower().split()) & set(row["text"].lower().split()))
|
28 |
if overlap > best_score:
|
29 |
best_score = overlap
|
30 |
best_context = row["text"]
|
|
|
31 |
return best_context
|
32 |
|
33 |
+
# Antarmuka Streamlit
|
34 |
+
st.title("Sistem Penjawab Pertanyaan SOP dengan IndoBERT")
|
35 |
+
st.markdown("Ajukan pertanyaan seputar Prosedur Operasional Standar:")
|
36 |
|
37 |
+
question = st.text_area("Masukkan pertanyaan Anda:", "")
|
|
|
38 |
|
39 |
+
if st.button("Dapatkan Jawaban"):
|
|
|
40 |
if question:
|
41 |
+
with st.spinner("Mencari konteks yang relevan..."):
|
|
|
42 |
context = find_best_context(question, dataset)
|
|
|
43 |
if context:
|
44 |
+
with st.spinner("Menjawab pertanyaan Anda..."):
|
45 |
result = qa_pipeline(question=question, context=context)
|
46 |
+
st.success("Jawaban:")
|
47 |
st.write(result["answer"])
|
48 |
+
st.write("Skor Keyakinan:", result["score"])
|
49 |
else:
|
50 |
+
st.warning("Konteks yang relevan tidak ditemukan. Silakan coba pertanyaan lain.")
|
51 |
else:
|
52 |
+
st.warning("Silakan masukkan pertanyaan.")
|