# app.py import streamlit as st import fitz # PyMuPDF import io import requests import re import os from fpdf import FPDF from datetime import datetime from PIL import Image import base64 import json # --- Config --- API_URL = "https://openrouter.ai/api/v1/chat/completions" MODEL = "mistralai/mistral-7b-instruct" # Retrieve API key from environment variable (set in Hugging Face secrets) OPENROUTER_API_KEY = os.getenv("OPENROUTER_API_KEY") # Set page config st.set_page_config( page_title="๐ฌ Science Lab Assistant", layout="centered", page_icon="๐ฌ", initial_sidebar_state="expanded" ) # Custom CSS for styling st.markdown(""" """, unsafe_allow_html=True) # Header st.markdown('
๐ฌ Science Lab Assistant
', unsafe_allow_html=True) # Introduction st.markdown("""Your all-in-one science companion! Design experiments, generate reports, and get AI-powered feedback on your lab work.
๐ค Upload Your Lab Report
', unsafe_allow_html=True) uploaded_file = st.file_uploader("Upload PDF only (image support coming soon)", type=["pdf"], label_visibility="collapsed") lab_text = "" if uploaded_file: file_bytes = uploaded_file.read() file_ext = uploaded_file.name.split(".")[-1].lower() if file_ext == "pdf": try: doc = fitz.open(stream=file_bytes, filetype="pdf") for page in doc: lab_text += page.get_text() st.success("โ PDF text extracted successfully!") except Exception as e: st.error(f"Error reading PDF: {str(e)}") else: st.warning("Image upload is temporarily disabled. Please upload PDF files only.") st.info("Tip: Convert images to PDF using free online tools") # Allow text editing if lab_text: st.markdown('โ๏ธ Extracted Text
', unsafe_allow_html=True) st.caption("Review and edit the extracted text if needed before analysis") lab_text = st.text_area("", lab_text, height=300, label_visibility="collapsed") # --- AI Evaluation --- if lab_text.strip(): # -- AI Evaluation Prompt -- full_prompt = f"""You are a science teacher evaluating a student's lab report. Please provide a comprehensive analysis: Lab Report: {lab_text} Evaluation Guidelines: 1. **Section Check**: Identify which of these sections are present and which are missing: - Title - Objective - Hypothesis - Materials - Procedure - Observations - Results - Conclusion - References 2. **Completeness Score**: - Assign a numerical score from 1-10 based on completeness - Justify the score based on missing sections and content quality 3. **Improvement Tips**: - For each missing section, explain why it's important - Provide specific suggestions for improvement (e.g., "Try writing a more detailed observation section by including quantitative data") - Highlight any sections that need more detail or clarity 4. **Structure Response**: - Start with: "### Missing Sections:" - Then: "### Completeness Score: X/10" - Then: "### Improvement Tips:" - Finally: "### Detailed Feedback:" Be concise but thorough in your analysis. """ if st.button("๐งช Analyze Report", use_container_width=True): with st.spinner("๐ Analyzing report with AI. This may take 20-30 seconds..."): result = query_ai(full_prompt) if result: st.success("โ Analysis Complete!") st.balloons() # (Keep all your analysis result display code here unchanged) pass # --- Question Answering Section --- st.markdown("---") st.markdown('โ Ask About Your Report
', unsafe_allow_html=True) col1, col2 = st.columns([3, 1]) with col1: user_question = st.text_input("Ask a question about your lab report", placeholder="e.g., How can I improve my hypothesis?") with col2: st.markdown("", unsafe_allow_html=True) ask_button = st.button("๐ Ask Question", use_container_width=True) if (ask_button or user_question) and user_question.strip(): with st.spinner("Thinking..."): followup_prompt = f"""Lab Report: {lab_text} Question: {user_question} Answer the question based on the lab report. If the question can't be answered from the report, suggest what information the student should add to answer it. """ followup_response = query_ai(followup_prompt) if followup_response: st.markdown("### ๐ฌ AI Response") st.markdown(f'๐ Sample Lab Report
', unsafe_allow_html=True) st.markdown(""" **Title:** Effect of Temperature on Enzyme Activity **Objective:** To investigate how temperature affects catalase enzyme activity **Hypothesis:** Enzyme activity will increase with temperature up to 37ยฐC, then decrease **Materials:** Test tubes, hydrogen peroxide, liver extract, thermometer **Procedure:** 1. Prepare test tubes at 5 different temperatures 2. Add equal amounts of hydrogen peroxide and liver extract 3. Measure oxygen production **Observations:** More bubbles at 37ยฐC compared to lower or higher temperatures **Conclusion:** Enzyme activity peaks at body temperature """) st.info("๐ Upload your own lab report to get a personalized analysis!") # Footer st.markdown("---") st.markdown('', unsafe_allow_html=True)