Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -18,6 +18,7 @@ import gradio as gr
|
|
18 |
import spaces
|
19 |
|
20 |
from pathlib import Path
|
|
|
21 |
|
22 |
# 1. System prompt
|
23 |
SYSTEM_PROMPT = """
|
@@ -44,22 +45,40 @@ model = AutoModelForCausalLM.from_pretrained(
|
|
44 |
|
45 |
print(f"Model loaded on device: {model.device}")
|
46 |
|
47 |
-
# 3. Load PDF files
|
48 |
-
from PyPDF2 import PdfReader
|
49 |
-
|
50 |
-
# Read all PDFs into a list of small chunks
|
51 |
def load_pdfs(folder_path="."):
|
52 |
docs = []
|
|
|
53 |
for pdf_file in Path(folder_path).glob("*.pdf"):
|
54 |
reader = PdfReader(str(pdf_file))
|
55 |
for page in reader.pages:
|
56 |
text = page.extract_text()
|
57 |
if text:
|
58 |
-
|
59 |
-
|
60 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
61 |
return docs
|
62 |
|
|
|
63 |
document_chunks = load_pdfs(".")
|
64 |
print(f"Loaded {len(document_chunks)} text chunks from PDFs.")
|
65 |
|
@@ -67,10 +86,12 @@ print(f"Loaded {len(document_chunks)} text chunks from PDFs.")
|
|
67 |
embedder = SentenceTransformer("all-MiniLM-L6-v2") # Fast small model
|
68 |
doc_embeddings = embedder.encode(document_chunks, normalize_embeddings=True)
|
69 |
|
70 |
-
# 5. Retrieval function
|
71 |
def retrieve_context(question, top_k=3):
|
72 |
question_embedding = embedder.encode(question, normalize_embeddings=True)
|
73 |
-
|
|
|
|
|
74 |
top_indices = torch.topk(scores, k=min(top_k, len(scores))).indices.tolist()
|
75 |
return "\n\n".join([document_chunks[idx] for idx in top_indices])
|
76 |
|
@@ -118,7 +139,7 @@ def respond(
|
|
118 |
response += new_text
|
119 |
yield response
|
120 |
|
121 |
-
# 7. Gradio
|
122 |
demo = gr.ChatInterface(
|
123 |
fn=respond,
|
124 |
title="Café Eleven Assistant",
|
@@ -170,4 +191,4 @@ demo = gr.ChatInterface(
|
|
170 |
|
171 |
# 8. Launch
|
172 |
if __name__ == "__main__":
|
173 |
-
demo.launch()
|
|
|
18 |
import spaces
|
19 |
|
20 |
from pathlib import Path
|
21 |
+
from PyPDF2 import PdfReader
|
22 |
|
23 |
# 1. System prompt
|
24 |
SYSTEM_PROMPT = """
|
|
|
45 |
|
46 |
print(f"Model loaded on device: {model.device}")
|
47 |
|
48 |
+
# 3. Load PDF files
|
|
|
|
|
|
|
49 |
def load_pdfs(folder_path="."):
|
50 |
docs = []
|
51 |
+
current_section = None
|
52 |
for pdf_file in Path(folder_path).glob("*.pdf"):
|
53 |
reader = PdfReader(str(pdf_file))
|
54 |
for page in reader.pages:
|
55 |
text = page.extract_text()
|
56 |
if text:
|
57 |
+
lines = text.split("\n")
|
58 |
+
for line in lines:
|
59 |
+
line = line.strip()
|
60 |
+
if not line:
|
61 |
+
continue
|
62 |
+
|
63 |
+
# New smarter heading detection:
|
64 |
+
# If the line is mostly UPPERCASE and not too long
|
65 |
+
if line.isupper() and len(line.split()) <= 6:
|
66 |
+
if current_section:
|
67 |
+
docs.append(current_section)
|
68 |
+
current_section = line
|
69 |
+
else:
|
70 |
+
if current_section:
|
71 |
+
current_section += f" | {line}"
|
72 |
+
else:
|
73 |
+
current_section = line
|
74 |
+
|
75 |
+
if current_section:
|
76 |
+
docs.append(current_section)
|
77 |
+
current_section = None
|
78 |
+
|
79 |
return docs
|
80 |
|
81 |
+
|
82 |
document_chunks = load_pdfs(".")
|
83 |
print(f"Loaded {len(document_chunks)} text chunks from PDFs.")
|
84 |
|
|
|
86 |
embedder = SentenceTransformer("all-MiniLM-L6-v2") # Fast small model
|
87 |
doc_embeddings = embedder.encode(document_chunks, normalize_embeddings=True)
|
88 |
|
89 |
+
# 5. Retrieval function with float32 fix
|
90 |
def retrieve_context(question, top_k=3):
|
91 |
question_embedding = embedder.encode(question, normalize_embeddings=True)
|
92 |
+
question_embedding = torch.tensor(question_embedding, dtype=torch.float32)
|
93 |
+
doc_embeds = torch.tensor(doc_embeddings, dtype=torch.float32)
|
94 |
+
scores = doc_embeds @ question_embedding
|
95 |
top_indices = torch.topk(scores, k=min(top_k, len(scores))).indices.tolist()
|
96 |
return "\n\n".join([document_chunks[idx] for idx in top_indices])
|
97 |
|
|
|
139 |
response += new_text
|
140 |
yield response
|
141 |
|
142 |
+
# 7. Gradio ChatInterface
|
143 |
demo = gr.ChatInterface(
|
144 |
fn=respond,
|
145 |
title="Café Eleven Assistant",
|
|
|
191 |
|
192 |
# 8. Launch
|
193 |
if __name__ == "__main__":
|
194 |
+
demo.launch(share=True)
|