Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from transformers import AutoModelForQuestionAnswering, AutoTokenizer
|
| 3 |
+
import torch
|
| 4 |
+
|
| 5 |
+
# Load the saved model and tokenizer
|
| 6 |
+
@st.cache_resource
|
| 7 |
+
def load_model():
|
| 8 |
+
model_path = "./bert_qa_model"
|
| 9 |
+
model = AutoModelForQuestionAnswering.from_pretrained(model_path)
|
| 10 |
+
tokenizer = AutoTokenizer.from_pretrained(model_path)
|
| 11 |
+
return model, tokenizer
|
| 12 |
+
|
| 13 |
+
model, tokenizer = load_model()
|
| 14 |
+
|
| 15 |
+
def answer_question(context, question):
|
| 16 |
+
# Tokenize input
|
| 17 |
+
inputs = tokenizer(question, context, return_tensors="pt")
|
| 18 |
+
|
| 19 |
+
# Get model output
|
| 20 |
+
with torch.no_grad():
|
| 21 |
+
outputs = model(**inputs)
|
| 22 |
+
|
| 23 |
+
# Get start and end logits
|
| 24 |
+
answer_start = torch.argmax(outputs.start_logits)
|
| 25 |
+
answer_end = torch.argmax(outputs.end_logits) + 1
|
| 26 |
+
|
| 27 |
+
# Decode the answer
|
| 28 |
+
answer = tokenizer.decode(inputs["input_ids"][0][answer_start:answer_end])
|
| 29 |
+
|
| 30 |
+
return answer
|
| 31 |
+
|
| 32 |
+
# Streamlit app
|
| 33 |
+
st.title("Question Answering System")
|
| 34 |
+
|
| 35 |
+
st.write("Enter a context and a question, and the model will provide an answer based on the context.")
|
| 36 |
+
|
| 37 |
+
context = st.text_area("Context", height=200)
|
| 38 |
+
question = st.text_input("Question")
|
| 39 |
+
|
| 40 |
+
if st.button("Get Answer"):
|
| 41 |
+
if context and question:
|
| 42 |
+
answer = answer_question(context, question)
|
| 43 |
+
st.success(f"Answer: {answer}")
|
| 44 |
+
else:
|
| 45 |
+
st.error("Please provide both context and question.")
|
| 46 |
+
|
| 47 |
+
st.markdown("---")
|
| 48 |
+
st.write("Powered by Hugging Face Transformers and Streamlit")
|