Spaces:
Build error
Build error
| import streamlit as st | |
| from PyPDF2 import PdfReader | |
| import random | |
| import re | |
| # App Title | |
| st.set_page_config(page_title="Procurement Guide App", page_icon="π", layout="wide") | |
| st.markdown("# π Procurement Guide App by") | |
| st.markdown(" Jephone Torre guwapo") | |
| # Sidebar Configuration | |
| st.sidebar.header("Select Your Role") | |
| role = st.sidebar.selectbox("Role:", ["End-user", "Admin", "Procurement Officer"]) | |
| st.sidebar.header("Select Procurement Type") | |
| procurement_type = st.sidebar.selectbox( | |
| "Procurement Type:", ["School notes", "Work", ""] | |
| ) | |
| # File Uploader | |
| st.markdown("### Upload Your Procurement Document") | |
| uploaded_file = st.file_uploader("Choose a PDF file", type="pdf") | |
| # Function to extract glossary terms | |
| def extract_glossary(text): | |
| glossary_terms = re.findall(r"\b[A-Z][a-z]+\b", text) | |
| return list(set(glossary_terms)) | |
| # Function to extract checklist items | |
| def extract_checklist(text): | |
| checklist_items = re.findall(r"(\d+\.\s.*|\β’\s.*)", text) | |
| return checklist_items | |
| if uploaded_file is not None: | |
| try: | |
| # Read PDF content | |
| pdf_reader = PdfReader(uploaded_file) | |
| pdf_text = "".join(page.extract_text() for page in pdf_reader.pages) | |
| # Extract glossary terms and checklist items | |
| glossary = extract_glossary(pdf_text) | |
| checklist = extract_checklist(pdf_text) | |
| # Display selected options | |
| st.markdown(f"### Selected Role: `{role}`") | |
| st.markdown(f"### Selected Procurement Type: `{procurement_type}`") | |
| # Display PDF content | |
| st.markdown("### Document Content:") | |
| st.text_area("PDF Content", pdf_text, height=500) | |
| # Selection box for content to display | |
| content_choice = st.selectbox("Choose what to view:", ["Questions", "Glossary", "Checklist"]) | |
| if content_choice == "Questions": | |
| # Generate Questions and Check Answers | |
| st.markdown("### Generated Questions:") | |
| sentences = pdf_text.split(".") | |
| if len(sentences) >= 3: | |
| questions = random.sample(sentences, 3) | |
| for i, question in enumerate(questions, 1): | |
| st.markdown(f"**Question {i}:** {question.strip()}?") | |
| user_answer = st.text_input(f"Your Answer to Question {i}:", key=f"answer_{i}") | |
| # Check correctness if answer is provided | |
| if user_answer: | |
| correct_answer = question.strip() | |
| if user_answer.lower() == correct_answer.lower(): | |
| st.success(f"Correct! Your answer matches the expected answer.") | |
| else: | |
| st.error(f"Incorrect. The correct answer is: {correct_answer}") | |
| else: | |
| st.warning("Not enough content to generate questions.") | |
| elif content_choice == "Glossary": | |
| # Display Glossary | |
| st.markdown("### Glossary Terms Extracted:") | |
| if glossary: | |
| for term in glossary: | |
| st.markdown(f"- **{term}**") | |
| else: | |
| st.markdown("No glossary terms found.") | |
| elif content_choice == "Checklist": | |
| # Display Checklist | |
| st.markdown("### Checklist Items Extracted:") | |
| if checklist: | |
| for item in checklist: | |
| st.markdown(f"- {item}") | |
| else: | |
| st.markdown("No checklist items found.") | |
| except Exception as e: | |
| st.error(f"Error reading the PDF: {e}") | |
| else: | |
| st.info("Please upload a PDF file to view its content.") | |
| # Footer | |
| st.markdown("---") | |
| st.markdown("Β© 2025 Procurement Guide App - Powered by Streamlit") | |