Spaces:
Sleeping
Sleeping
File size: 1,652 Bytes
6fb1ccb |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
import streamlit as st
import torch
from transformers import BartForConditionalGeneration, BartTokenizer
# Load the model and tokenizer
model_repo_path = 'AbdurRehman313/hotpotQA_BART_Finetuned_E5'
model = BartForConditionalGeneration.from_pretrained(model_repo_path)
tokenizer = BartTokenizer.from_pretrained(model_repo_path)
# Ensure the model is in evaluation mode
model.eval()
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
model.to(device)
# Streamlit app layout
st.title("Multi-Hop Question Answering Application")
# User input for context and question
context_input = st.text_area("Enter context", height=200)
question_input = st.text_area("Enter question")
# Generate the answer
if st.button("Get Answer"):
if context_input and question_input:
with st.spinner("Generating answer..."):
try:
# Prepare the input for the model
input_text = f"context: {context_input} question: {question_input}"
inputs = tokenizer(input_text, return_tensors='pt')
inputs = {key: value.to(device) for key, value in inputs.items()}
# Perform inference
with torch.no_grad():
outputs = model.generate(inputs['input_ids'], max_length=50)
# Decode the output
answer = tokenizer.decode(outputs[0], skip_special_tokens=True)
st.subheader("Answer")
st.write(answer)
except Exception as e:
st.error(f"Error during question answering: {e}")
else:
st.warning("Please enter both context and question.") |