File size: 3,111 Bytes
bc20a41
431d541
 
 
 
2b8aece
ebbd85b
2b8aece
 
 
ad0d754
8ca2f9f
431d541
 
 
2b8aece
 
 
 
 
 
 
431d541
2b8aece
 
431d541
2b8aece
 
 
 
431d541
2b8aece
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
431d541
2b8aece
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
import pytesseract
from PIL import Image
import fitz  # PyMuPDF
import io
import requests

# --- Config ---
API_KEY = "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"  
API_URL = "https://openrouter.ai/api/v1/chat/completions"
MODEL = "mistralai/mistral-7b-instruct"

st.set_page_config(page_title="πŸ§ͺ AI Science Lab Assistant", layout="centered")
st.title("πŸ§ͺ AI Science Lab Assistant")

st.markdown("""
This tool helps students evaluate their lab reports by:
- Extracting text from uploaded **PDFs** or **images**
- Automatically **checking for completeness** (Objective, Hypothesis, etc.)
- Giving **improvement tips** πŸ’‘
- Letting you **ask questions** about your report πŸ“˜
""")

# --- Upload ---
uploaded_file = st.file_uploader("πŸ“Έ Upload Your Lab Report Image or PDF", type=["jpg", "jpeg", "png", "pdf"])

lab_text = ""
if uploaded_file:
    file_bytes = uploaded_file.read()
    file_ext = uploaded_file.name.split(".")[-1].lower()

    if file_ext == "pdf":
        doc = fitz.open(stream=file_bytes, filetype="pdf")
        for page in doc:
            lab_text += page.get_text()
    else:
        image = Image.open(io.BytesIO(file_bytes))
        lab_text = pytesseract.image_to_string(image)

    st.subheader("πŸ“„ Extracted Lab Report Text:")
    st.text_area("", lab_text, height=300)

    # -- AI Evaluation Prompt --
    full_prompt = f"""Here is a science lab report:
{lab_text}

Please evaluate the report based on the following:
- Does it include sections like Objective, Hypothesis, Procedure, Observation, and Conclusion?
- Point out any missing or incomplete sections.
- Give feedback or improvement suggestions like: "Try writing a more detailed observation."
- Grade it roughly on a scale from 1 to 10 for completeness.

Respond clearly:
"""

    def query_ai(prompt):
        headers = {
            "Authorization": f"Bearer {API_KEY}",
            "Content-Type": "application/json"
        }
        payload = {
            "model": MODEL,
            "messages": [
                {"role": "system", "content": "You are a helpful science teacher."},
                {"role": "user", "content": prompt}
            ]
        }
        response = requests.post(API_URL, headers=headers, json=payload)
        return response.json()['choices'][0]['message']['content']

    if st.button("🧠 Evaluate My Lab Report"):
        with st.spinner("Analyzing report with AI..."):
            result = query_ai(full_prompt)
        st.success("βœ… Evaluation Complete")
        st.markdown("### πŸ“Š AI Feedback:")
        st.markdown(result)

    # Ask follow-up questions
    st.subheader("πŸ€” Ask AI About Your Report")
    user_question = st.text_input("Type your question")
    if st.button("πŸ” Ask") and user_question:
        with st.spinner("Thinking..."):
            followup_prompt = f"Here is the lab report:
{lab_text}

Now answer this question about it:
{user_question}"
            followup_response = query_ai(followup_prompt)
        st.markdown("### πŸ’¬ AI Answer:")
        st.markdown(followup_response)