huriacane33 commited on
Commit
7b1acfd
·
verified ·
1 Parent(s): 04a1c8b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -30
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
- # Load the Question Answering model
7
  @st.cache_resource
8
- def load_qa_pipeline():
9
- """Load the QA pipeline with deepset/roberta-base-squad2 model."""
10
- return pipeline("question-answering", model="deepset/roberta-base-squad2")
 
 
11
 
12
- qa_pipeline = load_qa_pipeline()
13
 
14
- # Load SOP Dataset
15
  @st.cache_data
16
  def load_sop_dataset():
17
- """Load SOP dataset from CSV."""
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
- # Utility function to find the most relevant context
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 UI
38
- st.title("SOP Question Answering AI")
39
- st.markdown("Ask any question about Standard Operating Procedures:")
40
 
41
- # User input
42
- question = st.text_area("Enter your question:", "")
43
 
44
- # Generate answer
45
- if st.button("Get Answer"):
46
  if question:
47
- with st.spinner("Finding the best context..."):
48
- # Automatically find the most relevant context
49
  context = find_best_context(question, dataset)
50
-
51
  if context:
52
- with st.spinner("Answering your question..."):
53
  result = qa_pipeline(question=question, context=context)
54
- st.success("Answer:")
55
  st.write(result["answer"])
56
- st.write("Confidence Score:", result["score"])
57
  else:
58
- st.warning("No relevant context found. Please try rephrasing your question.")
59
  else:
60
- st.warning("Please enter a question.")
 
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.")