Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -2,19 +2,25 @@
|
|
2 |
import streamlit as st
|
3 |
import pytesseract
|
4 |
from PIL import Image
|
5 |
-
import fitz # PyMuPDF
|
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 |
-
#
|
15 |
-
|
16 |
-
|
|
|
17 |
API_KEY = os.getenv("OPENROUTER_API_KEY")
|
|
|
|
|
|
|
|
|
|
|
18 |
MODEL = "llama3-70b-8192" # Or "mixtral-8x7b-32768" or "gemma-7b-it"
|
19 |
|
20 |
# Set Tesseract path for different environments
|
@@ -22,7 +28,7 @@ try:
|
|
22 |
# For Windows
|
23 |
if os.name == 'nt':
|
24 |
pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract.exe'
|
25 |
-
# For Linux
|
26 |
elif 'tesseract' not in os.environ.get('PATH', ''):
|
27 |
pytesseract.pytesseract.tesseract_cmd = '/usr/bin/tesseract'
|
28 |
except Exception as e:
|
@@ -156,6 +162,7 @@ experiments = {
|
|
156 |
}
|
157 |
|
158 |
# AI Query Function
|
|
|
159 |
def query_ai(prompt):
|
160 |
headers = {
|
161 |
"Authorization": f"Bearer {API_KEY}",
|
@@ -171,6 +178,21 @@ def query_ai(prompt):
|
|
171 |
}
|
172 |
try:
|
173 |
response = requests.post(API_URL, headers=headers, json=payload, timeout=120)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
174 |
response.raise_for_status()
|
175 |
return response.json()['choices'][0]['message']['content']
|
176 |
except requests.exceptions.HTTPError as err:
|
@@ -179,7 +201,6 @@ def query_ai(prompt):
|
|
179 |
except Exception as e:
|
180 |
st.error(f"Error connecting to AI service: {str(e)}")
|
181 |
return None
|
182 |
-
|
183 |
# Navigation
|
184 |
app_mode = st.radio("Choose Mode:", ["π§ͺ Experiment Assistant", "π Lab Report Analyzer"],
|
185 |
horizontal=True, label_visibility="collapsed")
|
@@ -372,9 +393,9 @@ else:
|
|
372 |
|
373 |
if file_ext == "pdf":
|
374 |
try:
|
375 |
-
|
376 |
-
|
377 |
-
|
378 |
st.success("β
PDF text extracted successfully!")
|
379 |
except Exception as e:
|
380 |
st.error(f"Error reading PDF: {str(e)}")
|
|
|
2 |
import streamlit as st
|
3 |
import pytesseract
|
4 |
from PIL import Image
|
|
|
5 |
import io
|
6 |
import requests
|
7 |
import re
|
|
|
8 |
from fpdf import FPDF
|
9 |
from datetime import datetime
|
10 |
import os
|
11 |
+
from dotenv import load_dotenv
|
12 |
+
import pdfplumber # Alternative PDF library
|
13 |
|
14 |
+
# Load environment variables
|
15 |
+
load_dotenv()
|
16 |
+
|
17 |
+
# --- Retrieve API Key ---
|
18 |
API_KEY = os.getenv("OPENROUTER_API_KEY")
|
19 |
+
if not API_KEY:
|
20 |
+
st.error("OPENROUTER_API_KEY not found in environment variables! Please add it to your .env file.")
|
21 |
+
st.stop()
|
22 |
+
|
23 |
+
API_URL = "https://api.groq.com/openai/v1/chat/completions"
|
24 |
MODEL = "llama3-70b-8192" # Or "mixtral-8x7b-32768" or "gemma-7b-it"
|
25 |
|
26 |
# Set Tesseract path for different environments
|
|
|
28 |
# For Windows
|
29 |
if os.name == 'nt':
|
30 |
pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract.exe'
|
31 |
+
# For Linux
|
32 |
elif 'tesseract' not in os.environ.get('PATH', ''):
|
33 |
pytesseract.pytesseract.tesseract_cmd = '/usr/bin/tesseract'
|
34 |
except Exception as e:
|
|
|
162 |
}
|
163 |
|
164 |
# AI Query Function
|
165 |
+
# Replace the query_ai function with this version
|
166 |
def query_ai(prompt):
|
167 |
headers = {
|
168 |
"Authorization": f"Bearer {API_KEY}",
|
|
|
178 |
}
|
179 |
try:
|
180 |
response = requests.post(API_URL, headers=headers, json=payload, timeout=120)
|
181 |
+
|
182 |
+
# Check for 401 Unauthorized specifically
|
183 |
+
if response.status_code == 401:
|
184 |
+
st.error("""
|
185 |
+
**Invalid API Key!**
|
186 |
+
|
187 |
+
Your Groq API key is either:
|
188 |
+
- Missing
|
189 |
+
- Incorrect
|
190 |
+
- Expired
|
191 |
+
|
192 |
+
Please check your .env file and ensure you have a valid key.
|
193 |
+
""")
|
194 |
+
return None
|
195 |
+
|
196 |
response.raise_for_status()
|
197 |
return response.json()['choices'][0]['message']['content']
|
198 |
except requests.exceptions.HTTPError as err:
|
|
|
201 |
except Exception as e:
|
202 |
st.error(f"Error connecting to AI service: {str(e)}")
|
203 |
return None
|
|
|
204 |
# Navigation
|
205 |
app_mode = st.radio("Choose Mode:", ["π§ͺ Experiment Assistant", "π Lab Report Analyzer"],
|
206 |
horizontal=True, label_visibility="collapsed")
|
|
|
393 |
|
394 |
if file_ext == "pdf":
|
395 |
try:
|
396 |
+
with pdfplumber.open(io.BytesIO(file_bytes)) as pdf:
|
397 |
+
for page in pdf.pages:
|
398 |
+
lab_text += page.extract_text() + "\n"
|
399 |
st.success("β
PDF text extracted successfully!")
|
400 |
except Exception as e:
|
401 |
st.error(f"Error reading PDF: {str(e)}")
|