Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,127 +3,88 @@ from PyPDF2 import PdfReader
|
|
| 3 |
import requests
|
| 4 |
import re
|
| 5 |
|
| 6 |
-
#
|
| 7 |
st.set_page_config(
|
| 8 |
-
page_title="
|
| 9 |
page_icon="🎓",
|
| 10 |
layout="wide"
|
| 11 |
)
|
| 12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
def extract_text(pdf_file):
|
| 14 |
-
"""
|
| 15 |
-
|
|
|
|
|
|
|
| 16 |
try:
|
| 17 |
-
|
| 18 |
-
for page in reader.pages:
|
| 19 |
-
text += page.extract_text() + "\n\n"
|
| 20 |
-
return re.sub(r'\n{3,}', '\n\n', text)[:3000] # Remove excessive newlines
|
| 21 |
except Exception as e:
|
| 22 |
st.error(f"PDF Error: {str(e)}")
|
| 23 |
return None
|
| 24 |
|
| 25 |
def call_ai(prompt):
|
| 26 |
-
"""
|
| 27 |
try:
|
| 28 |
response = requests.post(
|
| 29 |
"https://api-inference.huggingface.co/models/mistralai/Mixtral-8x7B-Instruct-v0.1",
|
| 30 |
headers={"Authorization": f"Bearer {st.secrets.HF_TOKEN}"},
|
| 31 |
-
json={
|
| 32 |
-
|
| 33 |
-
"parameters": {
|
| 34 |
-
"temperature": 0.3, # Less creative but more focused
|
| 35 |
-
"max_new_tokens": 800,
|
| 36 |
-
"do_sample": False # More deterministic
|
| 37 |
-
}
|
| 38 |
-
},
|
| 39 |
-
timeout=45
|
| 40 |
)
|
| 41 |
-
if response.status_code == 200:
|
| 42 |
-
return clean_output(response.json()[0]['generated_text'])
|
| 43 |
-
return f"⚠️ Error: {response.text[:200]}"
|
| 44 |
except Exception as e:
|
| 45 |
-
return f"
|
| 46 |
-
|
| 47 |
-
def clean_output(text):
|
| 48 |
-
"""Remove all AI instructions and formatting artifacts"""
|
| 49 |
-
text = re.sub(r'(?:Create|Generate).*?:', '', text) # Remove prompt prefixes
|
| 50 |
-
text = re.sub(r'Page \d+.*?\n', '', text) # Remove page numbers
|
| 51 |
-
text = re.sub(r'©.*?\n', '', text) # Remove copyrights
|
| 52 |
-
text = re.sub(r'\n{3,}', '\n\n', text) # Normalize newlines
|
| 53 |
-
return text.strip()
|
| 54 |
|
| 55 |
def generate_content(text):
|
| 56 |
-
"""
|
| 57 |
return {
|
| 58 |
-
"summary": call_ai(f"
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
{text}"""),
|
| 64 |
-
|
| 65 |
-
"mindmap": call_ai(f"""Create Mermaid mindmap syntax (ONLY the code):
|
| 66 |
-
```mermaid
|
| 67 |
-
mindmap
|
| 68 |
-
root((Core Concept))
|
| 69 |
-
Main Idea 1
|
| 70 |
-
Detail 1
|
| 71 |
-
Main Idea 2
|
| 72 |
-
```
|
| 73 |
-
For:
|
| 74 |
-
{text}"""),
|
| 75 |
-
|
| 76 |
-
"flashcards": call_ai(f"""Generate EXACTLY 5 flashcards (ONLY Q/A pairs):
|
| 77 |
-
Q1: Clear question?
|
| 78 |
-
A1: Concise answer
|
| 79 |
-
---
|
| 80 |
-
Q2: Next question?
|
| 81 |
-
A2: Answer
|
| 82 |
-
From:
|
| 83 |
-
{text}"""),
|
| 84 |
-
|
| 85 |
-
"mcq": call_ai(f"""Create EXACTLY 5 MCQs (ONLY questions/answers):
|
| 86 |
-
1. Question?
|
| 87 |
-
a) Option 1
|
| 88 |
-
b) Option 2
|
| 89 |
-
c) Option 3
|
| 90 |
-
d) Option 4
|
| 91 |
-
Answer: a
|
| 92 |
-
From:
|
| 93 |
-
{text}""")
|
| 94 |
}
|
| 95 |
|
| 96 |
-
#
|
| 97 |
-
st.title("
|
| 98 |
-
uploaded_file = st.file_uploader("Upload PDF
|
| 99 |
|
| 100 |
-
if uploaded_file
|
| 101 |
-
|
|
|
|
| 102 |
text = extract_text(uploaded_file)
|
|
|
|
| 103 |
if text and len(text) > 100:
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 110 |
st.markdown(content["summary"])
|
| 111 |
|
| 112 |
-
with
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 113 |
st.markdown(content["flashcards"])
|
| 114 |
|
| 115 |
-
with
|
| 116 |
st.markdown(content["mcq"])
|
| 117 |
-
|
| 118 |
-
with col2:
|
| 119 |
-
st.subheader("🗺️ Interactive Mind Map")
|
| 120 |
-
st.code(content["mindmap"], language="mermaid")
|
| 121 |
-
st.components.v1.html(f"""
|
| 122 |
-
<div class="mermaid">
|
| 123 |
-
{content['mindmap'].replace('```mermaid', '').replace('```', '')}
|
| 124 |
-
</div>
|
| 125 |
-
<script src="https://cdn.jsdelivr.net/npm/mermaid/dist/mermaid.min.js"></script>
|
| 126 |
-
<script>mermaid.initialize({{startOnLoad:true}});</script>
|
| 127 |
-
""", height=500)
|
| 128 |
else:
|
| 129 |
-
st.warning("
|
|
|
|
| 3 |
import requests
|
| 4 |
import re
|
| 5 |
|
| 6 |
+
# Set page config first (only needed once)
|
| 7 |
st.set_page_config(
|
| 8 |
+
page_title="Smart Study Assistant",
|
| 9 |
page_icon="🎓",
|
| 10 |
layout="wide"
|
| 11 |
)
|
| 12 |
|
| 13 |
+
# Initialize session state (if needed)
|
| 14 |
+
if 'generated' not in st.session_state:
|
| 15 |
+
st.session_state.generated = False
|
| 16 |
+
|
| 17 |
def extract_text(pdf_file):
|
| 18 |
+
"""Simplified PDF text extraction"""
|
| 19 |
+
if not pdf_file:
|
| 20 |
+
st.warning("Please upload a PDF file")
|
| 21 |
+
return None
|
| 22 |
try:
|
| 23 |
+
return "\n\n".join([page.extract_text() for page in PdfReader(pdf_file).pages])[:3000]
|
|
|
|
|
|
|
|
|
|
| 24 |
except Exception as e:
|
| 25 |
st.error(f"PDF Error: {str(e)}")
|
| 26 |
return None
|
| 27 |
|
| 28 |
def call_ai(prompt):
|
| 29 |
+
"""Streamlined API call"""
|
| 30 |
try:
|
| 31 |
response = requests.post(
|
| 32 |
"https://api-inference.huggingface.co/models/mistralai/Mixtral-8x7B-Instruct-v0.1",
|
| 33 |
headers={"Authorization": f"Bearer {st.secrets.HF_TOKEN}"},
|
| 34 |
+
json={"inputs": prompt},
|
| 35 |
+
timeout=30
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
)
|
| 37 |
+
return response.json()[0]['generated_text'] if response.status_code == 200 else f"Error: {response.text}"
|
|
|
|
|
|
|
| 38 |
except Exception as e:
|
| 39 |
+
return f"Connection Error: {str(e)}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
def generate_content(text):
|
| 42 |
+
"""Direct content generation without separate materials dict"""
|
| 43 |
return {
|
| 44 |
+
"summary": call_ai(f"Create structured markdown summary:\n{text}"),
|
| 45 |
+
"bullets": call_ai(f"Convert to hierarchical bullets:\n{text}"),
|
| 46 |
+
"mindmap": call_ai(f"Generate Mermaid mindmap syntax:\n{text}"),
|
| 47 |
+
"flashcards": call_ai(f"Create 5 flashcards:\n{text}"),
|
| 48 |
+
"mcq": call_ai(f"Generate 5 MCQs:\n{text}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
}
|
| 50 |
|
| 51 |
+
# UI Components
|
| 52 |
+
st.title("🎓 Smart Study Assistant")
|
| 53 |
+
uploaded_file = st.file_uploader("Upload PDF Lecture Notes", type="pdf")
|
| 54 |
|
| 55 |
+
if uploaded_file:
|
| 56 |
+
if st.button("Generate Study Materials") or st.session_state.generated:
|
| 57 |
+
st.session_state.generated = True
|
| 58 |
text = extract_text(uploaded_file)
|
| 59 |
+
|
| 60 |
if text and len(text) > 100:
|
| 61 |
+
with st.spinner("Generating materials..."):
|
| 62 |
+
content = generate_content(text)
|
| 63 |
+
|
| 64 |
+
# Display results in tabs
|
| 65 |
+
tab1, tab2, tab3, tab4, tab5 = st.tabs([
|
| 66 |
+
"Summary", "Bullet Points", "Mind Map",
|
| 67 |
+
"Flashcards", "MCQ Quiz"
|
| 68 |
+
])
|
| 69 |
+
|
| 70 |
+
with tab1:
|
| 71 |
st.markdown(content["summary"])
|
| 72 |
|
| 73 |
+
with tab2:
|
| 74 |
+
st.markdown(content["bullets"])
|
| 75 |
+
|
| 76 |
+
with tab3:
|
| 77 |
+
st.code(content["mindmap"], language="mermaid")
|
| 78 |
+
st.components.v1.html(f"""
|
| 79 |
+
<div class="mermaid">{content['mindmap']}</div>
|
| 80 |
+
<script src="https://cdn.jsdelivr.net/npm/mermaid/dist/mermaid.min.js"></script>
|
| 81 |
+
<script>mermaid.initialize({{startOnLoad:true}});</script>
|
| 82 |
+
""", height=400)
|
| 83 |
+
|
| 84 |
+
with tab4:
|
| 85 |
st.markdown(content["flashcards"])
|
| 86 |
|
| 87 |
+
with tab5:
|
| 88 |
st.markdown(content["mcq"])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
else:
|
| 90 |
+
st.warning("PDF text too short - try another file")
|