File size: 9,932 Bytes
314fac4
bc20a41
431d541
 
2b8aece
314fac4
ab70efd
4ad4653
 
f4ab78b
 
 
ebbd85b
2b8aece
 
ad0d754
8ca2f9f
f4ab78b
 
b23e8d5
314fac4
 
4ad4653
314fac4
4ad4653
 
314fac4
431d541
314fac4
2b8aece
314fac4
f4ab78b
314fac4
 
431d541
314fac4
4ad4653
314fac4
 
 
 
4ad4653
 
314fac4
 
 
f4ab78b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
06d7e14
 
 
 
 
 
 
 
 
 
 
 
 
4ad4653
06d7e14
2b8aece
4ad4653
06d7e14
4ad4653
f4ab78b
4ad4653
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2b8aece
06d7e14
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2b8aece
4ad4653
 
f4ab78b
 
4ad4653
 
 
 
 
f4ab78b
 
4ad4653
 
 
 
 
 
 
 
b23e8d5
 
 
 
 
 
 
4ad4653
f4ab78b
 
4ad4653
 
b23e8d5
 
 
 
4ad4653
 
06d7e14
4ad4653
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
06d7e14
4ad4653
 
 
 
 
f4ab78b
 
4ad4653
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
06d7e14
314fac4
4ad4653
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
314fac4
4ad4653
 
06d7e14
4ad4653
06d7e14
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
# 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("""
    <style>
    /* (Keep all your existing CSS styles here) */
    </style>
    """, unsafe_allow_html=True)

# Header
st.markdown('<p class="header">πŸ”¬ Science Lab Assistant</p>', unsafe_allow_html=True)

# Introduction
st.markdown("""
<div style="text-align: center; margin-bottom: 30px;">
    <p style="font-size: 18px;">Your all-in-one science companion! Design experiments, generate reports, 
    and get AI-powered feedback on your lab work.</p>
</div>
""", unsafe_allow_html=True)

# API Key Check
if not OPENROUTER_API_KEY:
    st.error("""
    **API Key Not Configured!**
    
    Please add your OpenRouter API key to Hugging Face Spaces secrets:
    1. Go to your Space settings
    2. Select "Variables and secrets" 
    3. Add a secret named: `OPENROUTER_API_KEY`
    4. Set its value to your actual API key
    5. Redeploy the space
    
    Without this key, the AI features won't work.
    """)
    st.stop()

# Experiment templates
experiments = {
    "Vinegar + Baking Soda": {
        "hypothesis": "Mixing vinegar and baking soda will produce bubbles due to a chemical reaction.",
        "concept": "Acid-base reaction producing carbon dioxide."
    },
    "Floating Egg": {
        "hypothesis": "An egg will float in salt water but sink in plain water.",
        "concept": "Density difference between saltwater and freshwater."
    },
    "Lemon Battery": {
        "hypothesis": "A lemon can produce electricity to power a small LED.",
        "concept": "Chemical energy conversion to electrical energy."
    }
}

# AI Query Function
def query_ai(prompt):
    headers = {
        "Authorization": f"Bearer {OPENROUTER_API_KEY}",
        "Content-Type": "application/json"
    }
    payload = {
        "model": MODEL,
        "messages": [
            {"role": "system", "content": "You are a helpful science teacher providing detailed explanations."},
            {"role": "user", "content": prompt}
        ],
        "temperature": 0.7
    }
    try:
        response = requests.post(API_URL, headers=headers, json=payload, timeout=120)
        response.raise_for_status()
        return response.json()['choices'][0]['message']['content']
    except requests.exceptions.HTTPError as err:
        st.error(f"API Error: {err.response.status_code} - {err.response.text}")
        return None
    except Exception as e:
        st.error(f"Error connecting to AI service: {str(e)}")
        return None

# Navigation
app_mode = st.radio("Choose Mode:", ["πŸ§ͺ Experiment Assistant", "πŸ“ Lab Report Analyzer"], 
                    horizontal=True, label_visibility="collapsed")

# Sidebar
with st.sidebar:
    st.markdown("### πŸ§ͺ Experiment Templates")
    st.caption("Quickly start with these pre-defined experiments:")
    
    selected_exp = st.selectbox("Choose an experiment template:", 
                               list(experiments.keys()) + ["Custom Experiment"])
    
    st.markdown("---")
    st.markdown("### πŸ“˜ Science Glossary Helper")
    term = st.text_input("Enter a science term (e.g., osmosis, catalyst)")
    if term:
        with st.spinner("Looking up term..."):
            ai_response = query_ai(f"Explain the term '{term}' in simple words for a student.")
        if ai_response:
            st.markdown(f"<div class='concept-box'>{ai_response}</div>", unsafe_allow_html=True)

# --- Experiment Assistant Section ---
if app_mode == "πŸ§ͺ Experiment Assistant":
    # (Keep all your experiment assistant code here unchanged)
    pass

# --- Lab Report Analyzer Section ---
else:
    # --- File Upload ---
    st.markdown('<p class="subheader">πŸ“€ Upload Your Lab Report</p>', 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('<p class="subheader">✍️ Extracted Text</p>', 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('<p class="subheader">❓ Ask About Your Report</p>', 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("<div style='height: 28px;'></div>", 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'<div class="tip-box">{followup_response}</div>', unsafe_allow_html=True)
    else:
        # Show sample report if no file uploaded
        st.markdown("---")
        st.markdown('<p class="subheader">πŸ“ Sample Lab Report</p>', 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('<div class="footer">πŸ”¬ Science Lab Assistant | Made for Students & Educators</div>', unsafe_allow_html=True)