RICHERGIRL commited on
Commit
8940945
·
verified ·
1 Parent(s): 29a3f71

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +53 -92
app.py CHANGED
@@ -3,127 +3,88 @@ from PyPDF2 import PdfReader
3
  import requests
4
  import re
5
 
6
- # Configure page
7
  st.set_page_config(
8
- page_title="Perfect Study Assistant",
9
  page_icon="🎓",
10
  layout="wide"
11
  )
12
 
 
 
 
 
13
  def extract_text(pdf_file):
14
- """Clean PDF text extraction"""
15
- text = ""
 
 
16
  try:
17
- reader = PdfReader(pdf_file)
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
- """Optimized AI call with strict output control"""
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
- "inputs": prompt,
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"⚠️ Connection Error: {str(e)}"
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
- """Precise content generation with strict templates"""
57
  return {
58
- "summary": call_ai(f"""Extract ONLY key concepts as bullet points:
59
- - Concept 1
60
- - Concept 2
61
- - Sub-concept 2.1
62
- From this text (ONLY include the bullet points, no intro/outro):
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
- # Streamlit UI
97
- st.title("🎯 Perfect Study Assistant")
98
- uploaded_file = st.file_uploader("Upload PDF (2-3 pages work best)", type="pdf")
99
 
100
- if uploaded_file and st.button("Generate Precise Materials"):
101
- with st.spinner("Creating perfect study materials..."):
 
102
  text = extract_text(uploaded_file)
 
103
  if text and len(text) > 100:
104
- content = generate_content(text)
105
-
106
- col1, col2 = st.columns(2)
107
-
108
- with col1:
109
- with st.expander("📝 Clean Summary", expanded=True):
 
 
 
 
110
  st.markdown(content["summary"])
111
 
112
- with st.expander("📇 Flashcards"):
 
 
 
 
 
 
 
 
 
 
 
113
  st.markdown(content["flashcards"])
114
 
115
- with st.expander("❓ MCQ Quiz"):
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("Text too short - try a more substantial PDF")
 
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")