sunbal7 commited on
Commit
b23e8d5
Β·
verified Β·
1 Parent(s): 904c443

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +61 -8
app.py CHANGED
@@ -6,14 +6,27 @@ import fitz # PyMuPDF
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
18
  st.set_page_config(
19
  page_title="πŸ”¬ Science Lab Assistant",
@@ -104,6 +117,13 @@ st.markdown("""
104
  color: #7f8c8d;
105
  font-size: 14px;
106
  }
 
 
 
 
 
 
 
107
  </style>
108
  """, unsafe_allow_html=True)
109
 
@@ -179,6 +199,8 @@ with st.sidebar:
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":
@@ -348,17 +370,48 @@ else:
348
  file_ext = uploaded_file.name.split(".")[-1].lower()
349
 
350
  if file_ext == "pdf":
351
- doc = fitz.open(stream=file_bytes, filetype="pdf")
352
- for page in doc:
353
- lab_text += page.get_text()
 
 
 
 
354
  else:
355
- image = Image.open(io.BytesIO(file_bytes))
356
- lab_text = pytesseract.image_to_string(image)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
357
 
358
  # Allow text editing
359
- st.markdown('<p class="subheader">✍️ Extracted Text</p>', unsafe_allow_html=True)
360
- st.caption("Review and edit the extracted text if needed before analysis")
361
- lab_text = st.text_area("", lab_text, height=300, label_visibility="collapsed")
 
362
 
363
  # --- AI Evaluation ---
364
  if lab_text.strip():
 
6
  import io
7
  import requests
8
  import re
9
+ import numpy as np
10
  from fpdf import FPDF
11
  from datetime import datetime
12
+ import os
13
 
14
  # --- Config ---
15
  API_URL = "https://openrouter.ai/api/v1/chat/completions"
16
  API_KEY = "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"
17
  MODEL = "mistralai/mistral-7b-instruct"
18
 
19
+ # Set Tesseract path for different environments
20
+ try:
21
+ # For Windows
22
+ if os.name == 'nt':
23
+ pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract.exe'
24
+ # For Linux (Streamlit Sharing)
25
+ elif 'tesseract' not in os.environ.get('PATH', ''):
26
+ pytesseract.pytesseract.tesseract_cmd = '/usr/bin/tesseract'
27
+ except Exception as e:
28
+ st.warning(f"Tesseract configuration issue: {str(e)}")
29
+
30
  # Set page config
31
  st.set_page_config(
32
  page_title="πŸ”¬ Science Lab Assistant",
 
117
  color: #7f8c8d;
118
  font-size: 14px;
119
  }
120
+ .ocr-warning {
121
+ background-color: #fef9e7;
122
+ border-left: 5px solid #f1c40f;
123
+ padding: 15px;
124
+ margin: 15px 0;
125
+ border-radius: 0 8px 8px 0;
126
+ }
127
  </style>
128
  """, unsafe_allow_html=True)
129
 
 
199
  ai_response = query_ai(f"Explain the term '{term}' in simple words for a student.")
200
  if ai_response:
201
  st.markdown(f"<div class='concept-box'>{ai_response}</div>", unsafe_allow_html=True)
202
+ else:
203
+ st.warning("Couldn't retrieve definition. Please try again.")
204
 
205
  # --- Experiment Assistant Section ---
206
  if app_mode == "πŸ§ͺ Experiment Assistant":
 
370
  file_ext = uploaded_file.name.split(".")[-1].lower()
371
 
372
  if file_ext == "pdf":
373
+ try:
374
+ doc = fitz.open(stream=file_bytes, filetype="pdf")
375
+ for page in doc:
376
+ lab_text += page.get_text()
377
+ st.success("βœ… PDF text extracted successfully!")
378
+ except Exception as e:
379
+ st.error(f"Error reading PDF: {str(e)}")
380
  else:
381
+ try:
382
+ image = Image.open(io.BytesIO(file_bytes))
383
+ st.image(image, caption="Uploaded Image", width=300)
384
+
385
+ # OCR processing
386
+ with st.spinner("Extracting text from image..."):
387
+ try:
388
+ lab_text = pytesseract.image_to_string(image)
389
+ st.success("βœ… Text extracted from image!")
390
+ except pytesseract.pytesseract.TesseractNotFoundError:
391
+ st.error("""
392
+ **Tesseract OCR not found!**
393
+
394
+ To enable image text extraction:
395
+ 1. Install Tesseract OCR on your system
396
+ 2. Add it to your system PATH
397
+ 3. Restart the application
398
+
399
+ For Windows: Download from [UB-Mannheim/tesseract](https://github.com/UB-Mannheim/tesseract/wiki)
400
+ For Linux: `sudo apt install tesseract-ocr`
401
+ For Mac: `brew install tesseract`
402
+ """)
403
+ st.stop()
404
+ except Exception as e:
405
+ st.error(f"OCR Error: {str(e)}")
406
+ st.stop()
407
+ except Exception as e:
408
+ st.error(f"Error processing image: {str(e)}")
409
 
410
  # Allow text editing
411
+ if lab_text:
412
+ st.markdown('<p class="subheader">✍️ Extracted Text</p>', unsafe_allow_html=True)
413
+ st.caption("Review and edit the extracted text if needed before analysis")
414
+ lab_text = st.text_area("", lab_text, height=300, label_visibility="collapsed")
415
 
416
  # --- AI Evaluation ---
417
  if lab_text.strip():