gaur3009 commited on
Commit
323149c
Β·
verified Β·
1 Parent(s): 1faf467

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +123 -38
src/streamlit_app.py CHANGED
@@ -1,40 +1,125 @@
1
- import altair as alt
2
- import numpy as np
3
- import pandas as pd
4
  import streamlit as st
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
 
6
- """
7
- # Welcome to Streamlit!
8
-
9
- Edit `/streamlit_app.py` to customize this app to your heart's desire :heart:.
10
- If you have any questions, checkout our [documentation](https://docs.streamlit.io) and [community
11
- forums](https://discuss.streamlit.io).
12
-
13
- In the meantime, below is an example of what you can do with just a few lines of code:
14
- """
15
-
16
- num_points = st.slider("Number of points in spiral", 1, 10000, 1100)
17
- num_turns = st.slider("Number of turns in spiral", 1, 300, 31)
18
-
19
- indices = np.linspace(0, 1, num_points)
20
- theta = 2 * np.pi * num_turns * indices
21
- radius = indices
22
-
23
- x = radius * np.cos(theta)
24
- y = radius * np.sin(theta)
25
-
26
- df = pd.DataFrame({
27
- "x": x,
28
- "y": y,
29
- "idx": indices,
30
- "rand": np.random.randn(num_points),
31
- })
32
-
33
- st.altair_chart(alt.Chart(df, height=700, width=700)
34
- .mark_point(filled=True)
35
- .encode(
36
- x=alt.X("x", axis=None),
37
- y=alt.Y("y", axis=None),
38
- color=alt.Color("idx", legend=None, scale=alt.Scale()),
39
- size=alt.Size("rand", legend=None, scale=alt.Scale(range=[1, 150])),
40
- ))
 
 
 
 
1
  import streamlit as st
2
+ import re
3
+ import random
4
+ import PyPDF2
5
+ import numpy as np
6
+ from collections import defaultdict, deque
7
+ from sentence_transformers import SentenceTransformer
8
+ from sklearn.metrics.pairwise import cosine_similarity
9
+
10
+ # ---------------------
11
+ # Tokenization
12
+ # ---------------------
13
+ def tokenize(text):
14
+ return re.findall(r"\w+", text.lower())
15
+
16
+ # ---------------------
17
+ # PDF QA System
18
+ # ---------------------
19
+ class PDFQASystem:
20
+ def __init__(self):
21
+ self.text_chunks = []
22
+ self.embeddings = None
23
+ self.model = SentenceTransformer('all-MiniLM-L6-v2')
24
+ self.active_document = None
25
+
26
+ def process_pdf_stream(self, uploaded_file):
27
+ text = self._extract_pdf_text(uploaded_file)
28
+ self.text_chunks = self._chunk_text(text)
29
+ self.embeddings = self.model.encode(self.text_chunks)
30
+ self.active_document = uploaded_file.name
31
+
32
+ def _extract_pdf_text(self, uploaded_file):
33
+ text = ""
34
+ reader = PyPDF2.PdfReader(uploaded_file)
35
+ for page in reader.pages:
36
+ page_text = page.extract_text()
37
+ if page_text:
38
+ text += page_text
39
+ return text
40
+
41
+ def _chunk_text(self, text, chunk_size=500):
42
+ words = text.split()
43
+ return [' '.join(words[i:i+chunk_size]) for i in range(0, len(words), chunk_size)]
44
+
45
+ def answer_question(self, question):
46
+ if not self.active_document:
47
+ return "No document loaded. Please upload a PDF first."
48
+
49
+ question_embedding = self.model.encode(question)
50
+ similarities = cosine_similarity([question_embedding], self.embeddings)[0]
51
+ best_match_idx = np.argmax(similarities)
52
+ return self.text_chunks[best_match_idx]
53
+
54
+ # ---------------------
55
+ # Intent Classifier
56
+ # ---------------------
57
+ class IntentClassifier:
58
+ def __init__(self):
59
+ self.intents = {
60
+ "greet": ["hello", "hi", "hey"],
61
+ "bye": ["bye", "goodbye", "exit"],
62
+ "qa": ["what", "when", "how", "explain", "tell", "who", "why"],
63
+ "help": ["help", "support", "assist"]
64
+ }
65
+
66
+ def predict(self, tokens):
67
+ scores = defaultdict(int)
68
+ for token in tokens:
69
+ for intent, keywords in self.intents.items():
70
+ if token in keywords:
71
+ scores[intent] += 1
72
+ return max(scores, key=scores.get) if scores else "qa"
73
+
74
+ # ---------------------
75
+ # AI Agent Core
76
+ # ---------------------
77
+ class DocumentAI:
78
+ def __init__(self):
79
+ self.intent_recognizer = IntentClassifier()
80
+ self.qa_system = PDFQASystem()
81
+ self.responses = {
82
+ "greet": ["πŸ‘‹ Hello! I'm your document assistant.", "Hi there! Ready to answer your document questions."],
83
+ "bye": ["Goodbye!", "See you later!", "Thanks for using the assistant!"],
84
+ "help": "Upload a PDF and ask questions. I’ll answer from its content!",
85
+ "no_doc": "Please upload a PDF document first."
86
+ }
87
+
88
+ def handle_query(self, text):
89
+ tokens = tokenize(text)
90
+ intent = self.intent_recognizer.predict(tokens)
91
+
92
+ if intent == "greet":
93
+ return random.choice(self.responses["greet"])
94
+ elif intent == "bye":
95
+ return random.choice(self.responses["bye"])
96
+ elif intent == "help":
97
+ return self.responses["help"]
98
+ elif intent == "qa":
99
+ if self.qa_system.active_document:
100
+ return self.qa_system.answer_question(text)
101
+ else:
102
+ return self.responses["no_doc"]
103
+ else:
104
+ return "πŸ€– I’m not sure how to respond. Try saying 'help'."
105
+
106
+ # ---------------------
107
+ # Streamlit UI
108
+ # ---------------------
109
+ st.set_page_config(page_title="Document AI Assistant", page_icon="πŸ“„")
110
+ st.title("πŸ“„ AI PDF Assistant")
111
+ st.markdown("Ask questions from uploaded PDF files!")
112
+
113
+ ai = DocumentAI()
114
+
115
+ uploaded_file = st.file_uploader("Upload a PDF", type="pdf")
116
+
117
+ if uploaded_file:
118
+ ai.qa_system.process_pdf_stream(uploaded_file)
119
+ st.success(f"βœ… PDF '{uploaded_file.name}' processed successfully!")
120
+
121
+ query = st.text_input("Ask a question from the document:")
122
 
123
+ if query:
124
+ answer = ai.handle_query(query)
125
+ st.markdown(f"**🧠 Answer:** {answer}")