demosec-app / app.py
Habieb143's picture
Update app.py
a626dd6 verified
raw
history blame
1.54 kB
import streamlit as st
import pandas as pd
from transformers import pipeline, AutoTokenizer, AutoModelForQuestionAnswering
@st.cache(allow_output_mutation=True)
def load_qa_model():
model_name = "google/mobilebert-uncased"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForQuestionAnswering.from_pretrained(model_name)
qa_pipeline = pipeline("question-answering", model=model, tokenizer=tokenizer)
return qa_pipeline
qa = load_qa_model()
def process_batch(data):
results = []
for index, row in data.iterrows():
answer = qa(question=row['Question'], context=row['Article'])
results.append({
'Question': row['Question'],
'Article': row['Article'],
'Answer': answer['answer'],
'Score': answer['score']
})
return results
st.title("Batch Question Answering App")
uploaded_file = st.file_uploader("Upload a CSV file", type="csv")
if uploaded_file is not None:
data = pd.read_csv(uploaded_file)
st.write("Uploaded file:")
st.write(data)
if st.button("Process Batch"):
with st.spinner("Processing Batch..."):
results = process_batch(data)
st.write("Batch Processing Results:")
for result in results:
st.write("Question:", result['Question'])
st.write("Article:", result['Article'])
st.write("Answer:", result['Answer'])
st.write("Score:", result['Score'])
st.write("------")