Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,116 +1,90 @@
|
|
1 |
-
# app.py
|
2 |
-
|
3 |
import streamlit as st
|
4 |
-
import requests
|
5 |
-
from fpdf import FPDF
|
6 |
import pytesseract
|
7 |
from PIL import Image
|
8 |
import fitz # PyMuPDF
|
9 |
import io
|
10 |
-
import
|
11 |
|
12 |
-
#
|
13 |
-
API_KEY = "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"
|
|
|
14 |
MODEL = "mistralai/mistral-7b-instruct"
|
15 |
|
16 |
-
# ====== FUNCTIONS ======
|
17 |
-
def query_llm(prompt):
|
18 |
-
url = "https://openrouter.ai/api/v1/chat/completions"
|
19 |
-
headers = {
|
20 |
-
"Authorization": f"Bearer {API_KEY}",
|
21 |
-
"Content-Type": "application/json"
|
22 |
-
}
|
23 |
-
data = {
|
24 |
-
"model": MODEL,
|
25 |
-
"messages": [
|
26 |
-
{"role": "user", "content": prompt}
|
27 |
-
]
|
28 |
-
}
|
29 |
-
response = requests.post(url, headers=headers, json=data)
|
30 |
-
if response.status_code == 200:
|
31 |
-
return response.json()['choices'][0]['message']['content']
|
32 |
-
else:
|
33 |
-
return f"β API Error: {response.text}"
|
34 |
-
|
35 |
-
def extract_text_from_image(image):
|
36 |
-
return pytesseract.image_to_string(Image.open(image))
|
37 |
-
|
38 |
-
def extract_text_from_pdf(pdf_file):
|
39 |
-
text = ""
|
40 |
-
doc = fitz.open(stream=pdf_file.read(), filetype="pdf")
|
41 |
-
for page in doc:
|
42 |
-
text += page.get_text()
|
43 |
-
return text
|
44 |
-
|
45 |
-
def evaluate_lab_report(text):
|
46 |
-
missing_sections = []
|
47 |
-
suggestions = []
|
48 |
-
score = 0
|
49 |
-
total = 5
|
50 |
-
|
51 |
-
if re.search(r"objective", text, re.I): score += 1
|
52 |
-
else:
|
53 |
-
missing_sections.append("Objective")
|
54 |
-
suggestions.append("Add a clear objective stating what the experiment is trying to discover.")
|
55 |
-
|
56 |
-
if re.search(r"hypothesis", text, re.I): score += 1
|
57 |
-
else:
|
58 |
-
missing_sections.append("Hypothesis")
|
59 |
-
suggestions.append("Include a hypothesis that predicts the experiment outcome.")
|
60 |
-
|
61 |
-
if re.search(r"procedure|method", text, re.I): score += 1
|
62 |
-
else:
|
63 |
-
missing_sections.append("Procedure")
|
64 |
-
suggestions.append("Describe the steps followed during the experiment.")
|
65 |
-
|
66 |
-
if re.search(r"observation", text, re.I): score += 1
|
67 |
-
else:
|
68 |
-
missing_sections.append("Observation")
|
69 |
-
suggestions.append("Mention what was observed during the experiment.")
|
70 |
-
|
71 |
-
if re.search(r"conclusion", text, re.I): score += 1
|
72 |
-
else:
|
73 |
-
missing_sections.append("Conclusion")
|
74 |
-
suggestions.append("Summarize the findings in a conclusion.")
|
75 |
-
|
76 |
-
return score, total, missing_sections, suggestions
|
77 |
-
|
78 |
-
# ====== STREAMLIT UI ======
|
79 |
st.set_page_config(page_title="π§ͺ AI Science Lab Assistant", layout="centered")
|
80 |
st.title("π§ͺ AI Science Lab Assistant")
|
81 |
-
st.markdown("Upload your **lab report** (image or PDF), and the AI will analyze, evaluate, and answer questions about it!")
|
82 |
-
|
83 |
-
uploaded_file = st.file_uploader("π€ Upload Lab Report (Image or PDF)", type=["png", "jpg", "jpeg", "pdf"])
|
84 |
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
|
|
91 |
|
92 |
-
|
93 |
-
|
94 |
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
st.markdown("β **Missing Sections**:")
|
100 |
-
for m in missing:
|
101 |
-
st.write(f"- {m}")
|
102 |
-
if tips:
|
103 |
-
st.markdown("π‘ **Improvement Tips**:")
|
104 |
-
for t in tips:
|
105 |
-
st.write(f"- {t}")
|
106 |
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
114 |
|
115 |
-
|
116 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import streamlit as st
|
|
|
|
|
2 |
import pytesseract
|
3 |
from PIL import Image
|
4 |
import fitz # PyMuPDF
|
5 |
import io
|
6 |
+
import requests
|
7 |
|
8 |
+
# --- Config ---
|
9 |
+
API_KEY = "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"
|
10 |
+
API_URL = "https://openrouter.ai/api/v1/chat/completions"
|
11 |
MODEL = "mistralai/mistral-7b-instruct"
|
12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
st.set_page_config(page_title="π§ͺ AI Science Lab Assistant", layout="centered")
|
14 |
st.title("π§ͺ AI Science Lab Assistant")
|
|
|
|
|
|
|
15 |
|
16 |
+
st.markdown("""
|
17 |
+
This tool helps students evaluate their lab reports by:
|
18 |
+
- Extracting text from uploaded **PDFs** or **images**
|
19 |
+
- Automatically **checking for completeness** (Objective, Hypothesis, etc.)
|
20 |
+
- Giving **improvement tips** π‘
|
21 |
+
- Letting you **ask questions** about your report π
|
22 |
+
""")
|
23 |
|
24 |
+
# --- Upload ---
|
25 |
+
uploaded_file = st.file_uploader("πΈ Upload Your Lab Report Image or PDF", type=["jpg", "jpeg", "png", "pdf"])
|
26 |
|
27 |
+
lab_text = ""
|
28 |
+
if uploaded_file:
|
29 |
+
file_bytes = uploaded_file.read()
|
30 |
+
file_ext = uploaded_file.name.split(".")[-1].lower()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
31 |
|
32 |
+
if file_ext == "pdf":
|
33 |
+
doc = fitz.open(stream=file_bytes, filetype="pdf")
|
34 |
+
for page in doc:
|
35 |
+
lab_text += page.get_text()
|
36 |
+
else:
|
37 |
+
image = Image.open(io.BytesIO(file_bytes))
|
38 |
+
lab_text = pytesseract.image_to_string(image)
|
39 |
+
|
40 |
+
st.subheader("π Extracted Lab Report Text:")
|
41 |
+
st.text_area("", lab_text, height=300)
|
42 |
+
|
43 |
+
# -- AI Evaluation Prompt --
|
44 |
+
full_prompt = f"""Here is a science lab report:
|
45 |
+
{lab_text}
|
46 |
+
|
47 |
+
Please evaluate the report based on the following:
|
48 |
+
- Does it include sections like Objective, Hypothesis, Procedure, Observation, and Conclusion?
|
49 |
+
- Point out any missing or incomplete sections.
|
50 |
+
- Give feedback or improvement suggestions like: "Try writing a more detailed observation."
|
51 |
+
- Grade it roughly on a scale from 1 to 10 for completeness.
|
52 |
+
|
53 |
+
Respond clearly:
|
54 |
+
"""
|
55 |
+
|
56 |
+
def query_ai(prompt):
|
57 |
+
headers = {
|
58 |
+
"Authorization": f"Bearer {API_KEY}",
|
59 |
+
"Content-Type": "application/json"
|
60 |
+
}
|
61 |
+
payload = {
|
62 |
+
"model": MODEL,
|
63 |
+
"messages": [
|
64 |
+
{"role": "system", "content": "You are a helpful science teacher."},
|
65 |
+
{"role": "user", "content": prompt}
|
66 |
+
]
|
67 |
+
}
|
68 |
+
response = requests.post(API_URL, headers=headers, json=payload)
|
69 |
+
return response.json()['choices'][0]['message']['content']
|
70 |
|
71 |
+
if st.button("π§ Evaluate My Lab Report"):
|
72 |
+
with st.spinner("Analyzing report with AI..."):
|
73 |
+
result = query_ai(full_prompt)
|
74 |
+
st.success("β
Evaluation Complete")
|
75 |
+
st.markdown("### π AI Feedback:")
|
76 |
+
st.markdown(result)
|
77 |
+
|
78 |
+
# Ask follow-up questions
|
79 |
+
st.subheader("π€ Ask AI About Your Report")
|
80 |
+
user_question = st.text_input("Type your question")
|
81 |
+
if st.button("π Ask") and user_question:
|
82 |
+
with st.spinner("Thinking..."):
|
83 |
+
followup_prompt = f"Here is the lab report:
|
84 |
+
{lab_text}
|
85 |
+
|
86 |
+
Now answer this question about it:
|
87 |
+
{user_question}"
|
88 |
+
followup_response = query_ai(followup_prompt)
|
89 |
+
st.markdown("### π¬ AI Answer:")
|
90 |
+
st.markdown(followup_response)
|