Spaces:
Sleeping
Sleeping
Create app.py
Browse files
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)
|