Spaces:
Sleeping
Sleeping
huriacane33
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -1,29 +1,21 @@
|
|
1 |
import streamlit as st
|
2 |
-
from transformers import
|
3 |
import pandas as pd
|
4 |
import re
|
5 |
|
6 |
-
# Load the
|
7 |
@st.cache_resource
|
8 |
-
def
|
9 |
-
"""Load the
|
10 |
-
|
11 |
-
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
12 |
-
model = AutoModelForCausalLM.from_pretrained(
|
13 |
-
model_name,
|
14 |
-
torch_dtype="auto", # Use FP16 if supported
|
15 |
-
device_map="auto" # Automatically distributes across available devices
|
16 |
-
)
|
17 |
-
return pipeline("text-generation", model=model, tokenizer=tokenizer)
|
18 |
|
19 |
-
qa_pipeline =
|
20 |
|
21 |
# Load SOP Dataset
|
22 |
@st.cache_data
|
23 |
def load_sop_dataset():
|
24 |
"""Load SOP dataset from CSV."""
|
25 |
-
|
26 |
-
return dataset
|
27 |
|
28 |
dataset = load_sop_dataset()
|
29 |
|
@@ -33,17 +25,17 @@ def find_best_context(question, dataset):
|
|
33 |
best_score = 0
|
34 |
best_context = None
|
35 |
|
36 |
-
for
|
37 |
# Simple heuristic: Count the number of overlapping words
|
38 |
overlap = len(set(question.lower().split()) & set(row["text"].lower().split()))
|
39 |
if overlap > best_score:
|
40 |
best_score = overlap
|
41 |
best_context = row["text"]
|
42 |
-
|
43 |
return best_context
|
44 |
|
45 |
# Streamlit UI
|
46 |
-
st.title("SOP Question Answering AI
|
47 |
st.markdown("Ask any question about Standard Operating Procedures:")
|
48 |
|
49 |
# User input
|
@@ -58,10 +50,10 @@ if st.button("Get Answer"):
|
|
58 |
|
59 |
if context:
|
60 |
with st.spinner("Answering your question..."):
|
61 |
-
|
62 |
-
result = qa_pipeline(prompt, max_length=150, num_return_sequences=1)
|
63 |
st.success("Answer:")
|
64 |
-
st.write(result[
|
|
|
65 |
else:
|
66 |
st.warning("No relevant context found. Please try rephrasing your question.")
|
67 |
else:
|
|
|
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 |
|
|
|
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
|
|
|
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:
|