import streamlit as st from transformers import pipeline from PyPDF2 import PdfReader import tempfile # Initialize the QA pipeline qa_pipeline = pipeline("question-answering", model="distilbert-base-uncased-distilled-squad") # Function to extract text from PDF def extract_text_from_pdf(file_path): with open(file_path, "rb") as f: reader = PdfReader(f) text = [page.extract_text() for page in reader.pages if page.extract_text() is not None] return " ".join(text) # Streamlit interface st.title('Car Repair Query System') uploaded_file = st.file_uploader("Choose a PDF file", type="pdf") if uploaded_file is not None: with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as tmp_file: tmp_file.write(uploaded_file.getvalue()) document_text = extract_text_from_pdf(tmp_file.name) st.success('Document uploaded and processed. You can now ask questions.') st.write('### Ask a Question') question = st.text_input("What is your question?") if st.button('Answer Question'): if question: answer = qa_pipeline(question=question, context=document_text) st.write("Answer:", answer['answer']) else: st.write("Please enter a question.") else: st.write("Please upload a PDF document to begin.") # Run this app with `streamlit run your_script_name.py`