import streamlit as st import fitz # PyMuPDF for PDF extraction import openai from dotenv import load_dotenv import os from openai import OpenAI # Streamlit wide mode configuration st.set_page_config(layout="wide", page_title="Eduhub Profile Dashboard") # Set session states for question type and selected chapter if 'question_type' not in st.session_state: st.session_state['question_type'] = None if 'selected_chapter' not in st.session_state: st.session_state['selected_chapter'] = None if 'extracted_text' not in st.session_state: st.session_state['extracted_text'] = "" if 'questions' not in st.session_state: st.session_state['questions'] = "" # Load environment variables load_dotenv() openai.api_key = os.getenv("OPENAI_API_KEY") # Assuming you've loaded the OpenAI API key as shown above client = OpenAI() def extract_text_from_pdf(uploaded_file): with fitz.open(stream=uploaded_file.read(), filetype="pdf") as doc: text = "" for page in doc: text += page.get_text() return text def generate_questions(text): response = client.chat.completions.create( model="gpt-4-vision-preview", messages=[ {"role": "system", "content": "You are a teacher and you are going to prepare quiz for the students to prepare for examination for each subject. You help student by creating a quiz based on the PDF. Generate Quiz with 4 options based on the content of the PDF delimited by "". Follow rules while generating schedule. Rules: 1. Do not generate any generic statements at the starting and end of the response. 2. Generate only fill in the blanks questions 3. Do not add your topics which are not present in PDF content from your side. 4. I want the questions in one line and each option below the question. 5. The questions should be related to the subject and the options should be related to the question. 6. Make the options as a radio button to make it appealing"}, {"role": "user", "content": text} ], max_tokens=4096, n=1 ) return response.choices[0].message.content # Use custom CSS to make the UI similar to the provided design def custom_css(): st.markdown( """ """, unsafe_allow_html=True ) def create_header(): st.markdown("# Eduhub Profile Dashboard", unsafe_allow_html=True) st.markdown('---') cols = st.columns([2, 2, 2, 2]) options = ["Subject name", "Time Table", "% Completed", "Overall Score"] for i, col in enumerate(cols): with col: st.button(options[i], key=options[i]) def create_sidebar(): with st.sidebar: st.markdown("## Chapters") chapter_names = ["Chapter 1", "Chapter 2", "Chapter 3", "Chapter 4"] for chapter_name in chapter_names: if st.sidebar.button(chapter_name): st.session_state['selected_chapter'] = chapter_name st.markdown("## Upload Chapter PDF") uploaded_pdf = st.sidebar.file_uploader("Choose a PDF file", type=["pdf"]) if uploaded_pdf: st.session_state['extracted_text'] = extract_text_from_pdf(uploaded_pdf) def create_main_content(): st.markdown("## Question Type") question_types = ["Single Choice", "Multiple Choice", "Input Type"] question_type = st.radio("", question_types) st.session_state['question_type'] = question_type if st.session_state['selected_chapter'] and st.session_state['question_type'] and st.session_state['extracted_text']: if st.button("Generate Questions"): questions = generate_questions(st.session_state['extracted_text']) st.session_state['questions'] = questions if st.session_state['questions']: st.markdown(f"## Questions for {st.session_state['selected_chapter']}") st.write(st.session_state['questions']) def create_question_type_buttons(): question_types = ["Single Choice", "Multiple Choice", "Input Type"] for question_type in question_types: if st.button(question_type, key=f"question_type_{question_type}"): st.session_state.question_type = question_type def main(): custom_css() create_header() create_sidebar() create_main_content() if __name__ == "__main__": main()