# app.py import streamlit as st import requests from datetime import datetime from fpdf import FPDF # Replace with your actual OpenRouter or Hugging Face endpoint and key API_URL = "https://openrouter.ai/api/v1/chat/completions" API_KEY = "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" MODEL = "mistralai/mistral-7b-instruct" # ------------------------- AI QUERY FUNCTION ------------------------- def query_ai(prompt): headers = { "Authorization": f"Bearer {API_KEY}", "Content-Type": "application/json" } data = { "model": MODEL, "messages": [{"role": "user", "content": prompt}], "temperature": 0.7 } try: response = requests.post(API_URL, headers=headers, json=data) response.raise_for_status() return response.json()['choices'][0]['message']['content'] except Exception as e: return f"โŒ API Error: {str(e)}" # ---------------------- 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." } } # -------------------------- PDF REPORT ------------------------------ def generate_pdf_report(exp_name, hypo, explanation, result): pdf = FPDF() pdf.add_page() pdf.set_font("Arial", size=12) pdf.cell(200, 10, txt="Science Lab Report", ln=True, align='C') pdf.ln(10) pdf.multi_cell(0, 10, txt=f"Experiment: {exp_name}\n\nHypothesis: {hypo}\n\nAI Explanation: {explanation}\n\nExpected Result: {result}") filename = f"report_{datetime.now().strftime('%Y%m%d%H%M%S')}.pdf" pdf.output(filename) return filename # --------------------------- STREAMLIT UI --------------------------- st.set_page_config(page_title="๐Ÿงช Science Lab Assistant", layout="centered") st.title("๐Ÿงช Science Lab Assistant") st.markdown("Helping students understand, hypothesize, and explain science experiments.") exp_choice = st.selectbox("Choose an experiment or enter your own:", list(experiments.keys()) + ["Custom Experiment"]) if exp_choice != "Custom Experiment": default_hypo = experiments[exp_choice]["hypothesis"] concept = experiments[exp_choice]["concept"] else: default_hypo = "" concept = "" with st.form("experiment_form"): exp_name = st.text_input("Experiment Name", value=exp_choice) hypo = st.text_area("Your Hypothesis", value=default_hypo) submit = st.form_submit_button("๐Ÿ” Explain Experiment") if submit: with st.spinner("Thinking like a scientist..."): prompt = f"Explain the following school science experiment clearly for a student.\n\nExperiment: {exp_name}\nHypothesis: {hypo}\nExplain what happens, why it happens, and what the expected result is." explanation = query_ai(prompt) st.success("AI Explanation") st.write(explanation) # Generate expected result with st.spinner("Predicting result..."): result = query_ai(f"What is the expected result of this experiment: {exp_name}?") st.info(f"**Expected Result:** {result}") # Conceptual Science if concept: st.caption(f"๐Ÿง  Science Concept: {concept}") # PDF Download pdf_file = generate_pdf_report(exp_name, hypo, explanation, result) with open(pdf_file, "rb") as file: st.download_button("๐Ÿ“„ Download Lab Report (PDF)", file, file_name=pdf_file) # --------------------- OPTIONAL FILE UPLOAD ------------------------- st.markdown("---") st.subheader("๐Ÿ“ธ Upload Your Lab Report Image (optional)") uploaded_file = st.file_uploader("Upload an image for feedback", type=["jpg", "png", "jpeg"]) if uploaded_file: st.image(uploaded_file, caption="Uploaded Report", use_column_width=True) st.markdown("โœ… Received. AI feedback coming soon (future feature).") # ------------------------ GLOSSARY HELPER --------------------------- st.markdown("---") st.subheader("๐Ÿ“˜ Ask About a Science Term") term = st.text_input("Enter a science term (e.g., osmosis, catalyst)") if term: st.write(query_ai(f"Explain the term '{term}' in simple words for a student.")) # ---------------------- FEEDBACK SECTION ---------------------------- st.markdown("---") st.subheader("๐Ÿ’ฌ Feedback") user_feedback = st.text_area("What can we improve in this app?") if user_feedback: st.success("Thanks! Your feedback was recorded (simulated). ๐Ÿ“จ")