huriacane33 commited on
Commit
2e8f79f
·
verified ·
1 Parent(s): ad74fad

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -29
app.py CHANGED
@@ -7,7 +7,7 @@ import re
7
  qa_pipeline = pipeline("question-answering", model="deepset/roberta-base-squad2")
8
 
9
  # Load SOP Dataset
10
- @st.cache
11
  def load_sop_dataset():
12
  """Load SOP dataset from CSV."""
13
  dataset = pd.read_csv("dataset.csv") # Ensure this file is uploaded to your Hugging Face Space
@@ -16,14 +16,20 @@ def load_sop_dataset():
16
  # Load the dataset
17
  dataset = load_sop_dataset()
18
 
19
- # Utility function to find relevant contexts
20
- def find_relevant_contexts(question, dataset):
21
- """Search for relevant contexts in the dataset."""
22
- relevant_contexts = []
 
 
23
  for index, row in dataset.iterrows():
24
- if re.search(question, row["text"], re.IGNORECASE):
25
- relevant_contexts.append(row["text"])
26
- return relevant_contexts
 
 
 
 
27
 
28
  # Streamlit UI
29
  st.title("SOP Question Answering AI")
@@ -31,28 +37,21 @@ st.markdown("Ask any question about Standard Operating Procedures:")
31
 
32
  # User input
33
  question = st.text_area("Enter your question:", "")
34
- specific_context = st.checkbox("Use specific SOP context?")
35
-
36
- context = None
37
- if specific_context:
38
- st.write("Choose a context:")
39
- context = st.selectbox("SOP Contexts", dataset["text"])
40
- else:
41
- if question:
42
- st.write("Searching for relevant contexts...")
43
- relevant_contexts = find_relevant_contexts(question, dataset)
44
- if relevant_contexts:
45
- context = st.selectbox("Relevant SOP Contexts", relevant_contexts)
46
- else:
47
- st.warning("No relevant contexts found. Try refining your question.")
48
 
49
  # Generate answer
50
  if st.button("Get Answer"):
51
- if context:
52
- with st.spinner("Finding the answer..."):
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("Please select a context or refine your question.")
 
7
  qa_pipeline = pipeline("question-answering", model="deepset/roberta-base-squad2")
8
 
9
  # Load SOP Dataset
10
+ @st.cache_data
11
  def load_sop_dataset():
12
  """Load SOP dataset from CSV."""
13
  dataset = pd.read_csv("dataset.csv") # Ensure this file is uploaded to your Hugging Face Space
 
16
  # Load the dataset
17
  dataset = load_sop_dataset()
18
 
19
+ # Utility function to find the most relevant context
20
+ def find_best_context(question, dataset):
21
+ """Find the single best context for a given question."""
22
+ best_score = 0
23
+ best_context = None
24
+
25
  for index, row in dataset.iterrows():
26
+ # Simple heuristic: Count the number of overlapping words
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
+
32
+ return best_context
33
 
34
  # Streamlit UI
35
  st.title("SOP Question Answering AI")
 
37
 
38
  # User input
39
  question = st.text_area("Enter your question:", "")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40
 
41
  # Generate answer
42
  if st.button("Get Answer"):
43
+ if question:
44
+ with st.spinner("Finding the best context..."):
45
+ # Automatically find the most relevant context
46
+ context = find_best_context(question, dataset)
47
+
48
+ if context:
49
+ with st.spinner("Answering your question..."):
50
+ result = qa_pipeline(question=question, context=context)
51
+ st.success("Answer:")
52
+ st.write(result["answer"])
53
+ st.write("Confidence Score:", result["score"])
54
+ else:
55
+ st.warning("No relevant context found. Please try rephrasing your question.")
56
  else:
57
+ st.warning("Please enter a question.")