Naz786 commited on
Commit
f7d91c8
Β·
verified Β·
1 Parent(s): 818139f

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +105 -0
app.py ADDED
@@ -0,0 +1,105 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # ===============================
2
+ # πŸ“¦ Install dependencies (Only for Colab)
3
+ # ===============================
4
+ # !pip install streamlit faiss-cpu PyPDF2 sentence-transformers
5
+
6
+ # ===============================
7
+ # πŸ“„ Imports
8
+ # ===============================
9
+ import os
10
+ import numpy as np
11
+ import PyPDF2
12
+ import streamlit as st
13
+ from sentence_transformers import SentenceTransformer
14
+ import faiss
15
+
16
+ # βœ… Set Streamlit page configuration at the top
17
+ st.set_page_config(
18
+ page_title="πŸ“… Exam Schedule Chatbot",
19
+ page_icon="πŸ€–",
20
+ layout="wide"
21
+ )
22
+
23
+ # ===============================
24
+ # 🎨 Styling
25
+ # ===============================
26
+ st.markdown("""
27
+ <style>
28
+ .main-title {
29
+ font-size: 40px;
30
+ font-weight: 800;
31
+ color: #1f77b4;
32
+ text-align: center;
33
+ }
34
+ .sub-title {
35
+ font-size: 20px;
36
+ color: #555;
37
+ text-align: center;
38
+ }
39
+ .stTextInput > div > input {
40
+ font-size: 16px;
41
+ height: 3em;
42
+ }
43
+ .stFileUploader {
44
+ margin-bottom: 20px;
45
+ }
46
+ </style>
47
+ """, unsafe_allow_html=True)
48
+
49
+ # ===============================
50
+ # 🧠 Load PDF & Extract Text
51
+ # ===============================
52
+ def extract_text_from_pdf(pdf_file):
53
+ reader = PyPDF2.PdfReader(pdf_file)
54
+ text = ''
55
+ for page in reader.pages:
56
+ content = page.extract_text()
57
+ if content:
58
+ text += content
59
+ return text
60
+
61
+ # ===============================
62
+ # 🧩 Embed Text and Create FAISS Index
63
+ # ===============================
64
+ def embed_and_index(text, model):
65
+ chunks = text.split("\n")
66
+ docs = [chunk.strip() for chunk in chunks if chunk.strip()]
67
+ vectors = model.encode(docs, convert_to_tensor=False)
68
+ index = faiss.IndexFlatL2(len(vectors[0]))
69
+ index.add(np.array(vectors))
70
+ return docs, index
71
+
72
+ # ===============================
73
+ # πŸ€– Query with Context
74
+ # ===============================
75
+ def query_with_context(question, docs, index, model):
76
+ question_vec = model.encode([question], convert_to_tensor=False)
77
+ D, I = index.search(np.array(question_vec), k=3)
78
+ context = "\n".join([docs[i] for i in I[0]])
79
+ return f"πŸ“Œ **Relevant Information:**\n\n{context}"
80
+
81
+ # ===============================
82
+ # πŸ’¬ UI
83
+ # ===============================
84
+ st.markdown('<div class="main-title">πŸ“… University Exam Schedule Chatbot</div>', unsafe_allow_html=True)
85
+ st.markdown('<div class="sub-title">Ask questions like "When is the AI exam?" or "Date of Software Engineering paper?"</div>', unsafe_allow_html=True)
86
+
87
+ uploaded_file = st.file_uploader("πŸ“€ Upload your Date Sheet PDF", type="pdf")
88
+ query = st.text_input("❓ Ask something about your exam schedule:")
89
+
90
+ if uploaded_file and query:
91
+ if 'docs' not in st.session_state:
92
+ text = extract_text_from_pdf(uploaded_file)
93
+ model = SentenceTransformer("all-MiniLM-L6-v2")
94
+ st.session_state.docs, st.session_state.index = embed_and_index(text, model)
95
+ st.session_state.embedding_model = model
96
+
97
+ answer = query_with_context(
98
+ query,
99
+ st.session_state.docs,
100
+ st.session_state.index,
101
+ st.session_state.embedding_model
102
+ )
103
+
104
+ st.markdown("### πŸ’¬ Answer:")
105
+ st.success(answer)