sunbal7 commited on
Commit
06d7e14
Β·
verified Β·
1 Parent(s): a973d41

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -71
app.py CHANGED
@@ -6,13 +6,12 @@ import fitz # PyMuPDF
6
  import io
7
  import requests
8
  import re
9
- import os
10
  from fpdf import FPDF
11
  from datetime import datetime
12
 
13
  # --- Config ---
14
- DEFAULT_API_KEY = "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"
15
  API_URL = "https://openrouter.ai/api/v1/chat/completions"
 
16
  MODEL = "mistralai/mistral-7b-instruct"
17
 
18
  # Set page config
@@ -77,12 +76,6 @@ st.markdown("""
77
  margin: 15px 0;
78
  border-radius: 0 8px 8px 0;
79
  }
80
- .api-container {
81
- background-color: #eaf2f8;
82
- border-radius: 10px;
83
- padding: 15px;
84
- margin: 15px 0;
85
- }
86
  .experiment-card {
87
  background: linear-gradient(135deg, #f0f7ff, #e1effe);
88
  border-radius: 15px;
@@ -105,6 +98,12 @@ st.markdown("""
105
  [data-testid="stSidebar"] {
106
  background: linear-gradient(180deg, #e8f8f5, #d1f2eb) !important;
107
  }
 
 
 
 
 
 
108
  </style>
109
  """, unsafe_allow_html=True)
110
 
@@ -119,58 +118,26 @@ st.markdown("""
119
  </div>
120
  """, unsafe_allow_html=True)
121
 
122
- # API Key Setup
123
- with st.sidebar:
124
- st.markdown("## πŸ”‘ API Configuration")
125
- st.info("You need an API key from [OpenRouter](https://openrouter.ai/) to use this tool. Get a free key and paste it below.")
126
-
127
- api_key = st.text_input("Enter your OpenRouter API Key:", type="password",
128
- help="Get your API key from https://openrouter.ai/keys",
129
- value=os.getenv("OPENROUTER_API_KEY", DEFAULT_API_KEY))
130
-
131
- st.markdown("---")
132
- st.markdown("### πŸ§ͺ Experiment Templates")
133
- st.caption("Quickly start with these pre-defined experiments:")
134
-
135
- # Experiment templates
136
- experiments = {
137
- "Vinegar + Baking Soda": {
138
- "hypothesis": "Mixing vinegar and baking soda will produce bubbles due to a chemical reaction.",
139
- "concept": "Acid-base reaction producing carbon dioxide."
140
- },
141
- "Floating Egg": {
142
- "hypothesis": "An egg will float in salt water but sink in plain water.",
143
- "concept": "Density difference between saltwater and freshwater."
144
- },
145
- "Lemon Battery": {
146
- "hypothesis": "A lemon can produce electricity to power a small LED.",
147
- "concept": "Chemical energy conversion to electrical energy."
148
- }
149
  }
150
-
151
- selected_exp = st.selectbox("Choose an experiment template:",
152
- list(experiments.keys()) + ["Custom Experiment"])
153
-
154
- st.markdown("---")
155
- st.markdown("### πŸ“˜ Science Glossary Helper")
156
- term = st.text_input("Enter a science term (e.g., osmosis, catalyst)")
157
- if term:
158
- with st.spinner("Looking up term..."):
159
- ai_response = st.session_state.get('query_ai', lambda p: f"Explain '{term}'")(f"Explain the term '{term}' in simple words for a student.")
160
- st.markdown(f"<div class='concept-box'>{ai_response}</div>", unsafe_allow_html=True)
161
-
162
- # Navigation
163
- app_mode = st.radio("Choose Mode:", ["πŸ§ͺ Experiment Assistant", "πŸ“ Lab Report Analyzer"],
164
- horizontal=True, label_visibility="collapsed")
165
 
166
  # AI Query Function
167
- def query_ai(prompt, api_key):
168
- if not api_key:
169
- st.error("⚠️ Please enter a valid API key")
170
- return None
171
-
172
  headers = {
173
- "Authorization": f"Bearer {api_key}",
174
  "Content-Type": "application/json"
175
  }
176
  payload = {
@@ -192,9 +159,26 @@ def query_ai(prompt, api_key):
192
  st.error(f"Error connecting to AI service: {str(e)}")
193
  return None
194
 
195
- # Store query_ai in session state for glossary helper
196
- if 'query_ai' not in st.session_state:
197
- st.session_state.query_ai = query_ai
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
198
 
199
  # --- Experiment Assistant Section ---
200
  if app_mode == "πŸ§ͺ Experiment Assistant":
@@ -244,7 +228,7 @@ if app_mode == "πŸ§ͺ Experiment Assistant":
244
  5. How to interpret the results
245
  """
246
 
247
- explanation = query_ai(prompt, api_key)
248
 
249
  if explanation:
250
  st.success("βœ… Experiment Guide Generated!")
@@ -377,7 +361,7 @@ else:
377
  lab_text = st.text_area("", lab_text, height=300, label_visibility="collapsed")
378
 
379
  # --- AI Evaluation ---
380
- if lab_text.strip() and api_key:
381
  # -- AI Evaluation Prompt --
382
  full_prompt = f"""You are a science teacher evaluating a student's lab report. Please provide a comprehensive analysis:
383
 
@@ -416,7 +400,7 @@ else:
416
 
417
  if st.button("πŸ§ͺ Analyze Report", use_container_width=True):
418
  with st.spinner("πŸ” Analyzing report with AI. This may take 20-30 seconds..."):
419
- result = query_ai(full_prompt, api_key)
420
 
421
  if result:
422
  st.success("βœ… Analysis Complete!")
@@ -528,13 +512,11 @@ else:
528
  Answer the question based on the lab report. If the question can't be answered from the report,
529
  suggest what information the student should add to answer it.
530
  """
531
- followup_response = query_ai(followup_prompt, api_key)
532
 
533
  if followup_response:
534
  st.markdown("### πŸ’¬ AI Response")
535
  st.markdown(f'<div class="tip-box">{followup_response}</div>', unsafe_allow_html=True)
536
- elif lab_text.strip() and not api_key:
537
- st.error("⚠️ Please enter a valid OpenRouter API key to analyze your report")
538
  else:
539
  # Show sample report if no file uploaded
540
  st.markdown("---")
@@ -554,10 +536,6 @@ else:
554
 
555
  st.info("πŸ‘† Upload your own lab report to get a personalized analysis!")
556
 
557
- # --- Feedback Section ---
558
  st.markdown("---")
559
- with st.expander("πŸ’¬ Send Feedback"):
560
- st.markdown("We'd love to hear your thoughts to improve this tool!")
561
- feedback = st.text_area("What can we do better?")
562
- if st.button("Submit Feedback", use_container_width=True):
563
- st.success("Thank you for your feedback! We'll use it to improve the Science Lab Assistant.")
 
6
  import io
7
  import requests
8
  import re
 
9
  from fpdf import FPDF
10
  from datetime import datetime
11
 
12
  # --- Config ---
 
13
  API_URL = "https://openrouter.ai/api/v1/chat/completions"
14
+ API_KEY = "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"
15
  MODEL = "mistralai/mistral-7b-instruct"
16
 
17
  # Set page config
 
76
  margin: 15px 0;
77
  border-radius: 0 8px 8px 0;
78
  }
 
 
 
 
 
 
79
  .experiment-card {
80
  background: linear-gradient(135deg, #f0f7ff, #e1effe);
81
  border-radius: 15px;
 
98
  [data-testid="stSidebar"] {
99
  background: linear-gradient(180deg, #e8f8f5, #d1f2eb) !important;
100
  }
101
+ .footer {
102
+ text-align: center;
103
+ padding: 20px;
104
+ color: #7f8c8d;
105
+ font-size: 14px;
106
+ }
107
  </style>
108
  """, unsafe_allow_html=True)
109
 
 
118
  </div>
119
  """, unsafe_allow_html=True)
120
 
121
+ # Experiment templates
122
+ experiments = {
123
+ "Vinegar + Baking Soda": {
124
+ "hypothesis": "Mixing vinegar and baking soda will produce bubbles due to a chemical reaction.",
125
+ "concept": "Acid-base reaction producing carbon dioxide."
126
+ },
127
+ "Floating Egg": {
128
+ "hypothesis": "An egg will float in salt water but sink in plain water.",
129
+ "concept": "Density difference between saltwater and freshwater."
130
+ },
131
+ "Lemon Battery": {
132
+ "hypothesis": "A lemon can produce electricity to power a small LED.",
133
+ "concept": "Chemical energy conversion to electrical energy."
 
 
 
 
 
 
 
 
 
 
 
 
 
 
134
  }
135
+ }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
136
 
137
  # AI Query Function
138
+ def query_ai(prompt):
 
 
 
 
139
  headers = {
140
+ "Authorization": f"Bearer {API_KEY}",
141
  "Content-Type": "application/json"
142
  }
143
  payload = {
 
159
  st.error(f"Error connecting to AI service: {str(e)}")
160
  return None
161
 
162
+ # Navigation
163
+ app_mode = st.radio("Choose Mode:", ["πŸ§ͺ Experiment Assistant", "πŸ“ Lab Report Analyzer"],
164
+ horizontal=True, label_visibility="collapsed")
165
+
166
+ # Sidebar
167
+ with st.sidebar:
168
+ st.markdown("### πŸ§ͺ Experiment Templates")
169
+ st.caption("Quickly start with these pre-defined experiments:")
170
+
171
+ selected_exp = st.selectbox("Choose an experiment template:",
172
+ list(experiments.keys()) + ["Custom Experiment"])
173
+
174
+ st.markdown("---")
175
+ st.markdown("### πŸ“˜ Science Glossary Helper")
176
+ term = st.text_input("Enter a science term (e.g., osmosis, catalyst)")
177
+ if term:
178
+ with st.spinner("Looking up term..."):
179
+ ai_response = query_ai(f"Explain the term '{term}' in simple words for a student.")
180
+ if ai_response:
181
+ st.markdown(f"<div class='concept-box'>{ai_response}</div>", unsafe_allow_html=True)
182
 
183
  # --- Experiment Assistant Section ---
184
  if app_mode == "πŸ§ͺ Experiment Assistant":
 
228
  5. How to interpret the results
229
  """
230
 
231
+ explanation = query_ai(prompt)
232
 
233
  if explanation:
234
  st.success("βœ… Experiment Guide Generated!")
 
361
  lab_text = st.text_area("", lab_text, height=300, label_visibility="collapsed")
362
 
363
  # --- AI Evaluation ---
364
+ if lab_text.strip():
365
  # -- AI Evaluation Prompt --
366
  full_prompt = f"""You are a science teacher evaluating a student's lab report. Please provide a comprehensive analysis:
367
 
 
400
 
401
  if st.button("πŸ§ͺ Analyze Report", use_container_width=True):
402
  with st.spinner("πŸ” Analyzing report with AI. This may take 20-30 seconds..."):
403
+ result = query_ai(full_prompt)
404
 
405
  if result:
406
  st.success("βœ… Analysis Complete!")
 
512
  Answer the question based on the lab report. If the question can't be answered from the report,
513
  suggest what information the student should add to answer it.
514
  """
515
+ followup_response = query_ai(followup_prompt)
516
 
517
  if followup_response:
518
  st.markdown("### πŸ’¬ AI Response")
519
  st.markdown(f'<div class="tip-box">{followup_response}</div>', unsafe_allow_html=True)
 
 
520
  else:
521
  # Show sample report if no file uploaded
522
  st.markdown("---")
 
536
 
537
  st.info("πŸ‘† Upload your own lab report to get a personalized analysis!")
538
 
539
+ # Footer
540
  st.markdown("---")
541
+ st.markdown('<div class="footer">πŸ”¬ Science Lab Assistant | Made for Students & Educators</div>', unsafe_allow_html=True)