Spaces:
Build error
Build error
| import streamlit as st | |
| from PyPDF2 import PdfReader | |
| import requests | |
| import re | |
| # Set page config first (only needed once) | |
| st.set_page_config( | |
| page_title="Smart Study Assistant", | |
| page_icon="π", | |
| layout="wide" | |
| ) | |
| # Initialize session state (if needed) | |
| if 'generated' not in st.session_state: | |
| st.session_state.generated = False | |
| def extract_text(pdf_file): | |
| """Simplified PDF text extraction""" | |
| if not pdf_file: | |
| st.warning("Please upload a PDF file") | |
| return None | |
| try: | |
| return "\n\n".join([page.extract_text() for page in PdfReader(pdf_file).pages])[:3000] | |
| except Exception as e: | |
| st.error(f"PDF Error: {str(e)}") | |
| return None | |
| def call_ai(prompt): | |
| """Streamlined API call""" | |
| try: | |
| response = requests.post( | |
| "https://api-inference.huggingface.co/models/mistralai/Mixtral-8x7B-Instruct-v0.1", | |
| headers={"Authorization": f"Bearer {st.secrets.HF_TOKEN}"}, | |
| json={"inputs": prompt}, | |
| timeout=30 | |
| ) | |
| return response.json()[0]['generated_text'] if response.status_code == 200 else f"Error: {response.text}" | |
| except Exception as e: | |
| return f"Connection Error: {str(e)}" | |
| def generate_content(text): | |
| """Direct content generation without separate materials dict""" | |
| return { | |
| "summary": call_ai(f"Create structured markdown summary:\n{text}"), | |
| "bullets": call_ai(f"Convert to hierarchical bullets:\n{text}"), | |
| "mindmap": call_ai(f"Generate Mermaid mindmap syntax:\n{text}"), | |
| "flashcards": call_ai(f"Create 5 flashcards:\n{text}"), | |
| "mcq": call_ai(f"Generate 5 MCQs:\n{text}") | |
| } | |
| # UI Components | |
| st.title("π Smart Study Assistant") | |
| uploaded_file = st.file_uploader("Upload PDF Lecture Notes", type="pdf") | |
| if uploaded_file: | |
| if st.button("Generate Study Materials") or st.session_state.generated: | |
| st.session_state.generated = True | |
| text = extract_text(uploaded_file) | |
| if text and len(text) > 100: | |
| with st.spinner("Generating materials..."): | |
| content = generate_content(text) | |
| # Display results in tabs | |
| tab1, tab2, tab3, tab4, tab5 = st.tabs([ | |
| "Summary", "Bullet Points", "Mind Map", | |
| "Flashcards", "MCQ Quiz" | |
| ]) | |
| with tab1: | |
| st.markdown(content["summary"]) | |
| with tab2: | |
| st.markdown(content["bullets"]) | |
| with tab3: | |
| st.code(content["mindmap"], language="mermaid") | |
| st.components.v1.html(f""" | |
| <div class="mermaid">{content['mindmap']}</div> | |
| <script src="https://cdn.jsdelivr.net/npm/mermaid/dist/mermaid.min.js"></script> | |
| <script>mermaid.initialize({{startOnLoad:true}});</script> | |
| """, height=400) | |
| with tab4: | |
| st.markdown(content["flashcards"]) | |
| with tab5: | |
| st.markdown(content["mcq"]) | |
| else: | |
| st.warning("PDF text too short - try another file") |