Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import fitz
|
| 3 |
import numpy as np
|
| 4 |
import requests
|
| 5 |
import faiss
|
|
@@ -12,16 +14,18 @@ from sentence_transformers import SentenceTransformer
|
|
| 12 |
from concurrent.futures import ThreadPoolExecutor
|
| 13 |
|
| 14 |
# Configuration
|
| 15 |
-
GROQ_API_KEY = "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"
|
| 16 |
-
|
| 17 |
CHUNK_SIZE = 512
|
| 18 |
MAX_TOKENS = 4096
|
| 19 |
-
MODEL = SentenceTransformer(MODEL_NAME)
|
| 20 |
WORKERS = 8
|
| 21 |
|
|
|
|
|
|
|
|
|
|
| 22 |
class DocumentProcessor:
|
| 23 |
def __init__(self):
|
| 24 |
-
self.index = faiss.IndexFlatIP(
|
| 25 |
self.chunks = []
|
| 26 |
self.processor_pool = ThreadPoolExecutor(max_workers=WORKERS)
|
| 27 |
|
|
@@ -79,9 +83,22 @@ class DocumentProcessor:
|
|
| 79 |
return ""
|
| 80 |
|
| 81 |
def semantic_chunking(self, text):
|
| 82 |
-
|
| 83 |
-
chunks = [
|
| 84 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
|
| 86 |
def process_documents(self, files):
|
| 87 |
self.chunks = []
|
|
@@ -101,9 +118,9 @@ class DocumentProcessor:
|
|
| 101 |
return "Error: No chunks generated from documents"
|
| 102 |
|
| 103 |
try:
|
| 104 |
-
embeddings =
|
| 105 |
all_chunks,
|
| 106 |
-
batch_size=
|
| 107 |
convert_to_tensor=True,
|
| 108 |
show_progress_bar=False
|
| 109 |
).cpu().numpy().astype('float32')
|
|
@@ -124,7 +141,7 @@ class DocumentProcessor:
|
|
| 124 |
print("\n" + "="*40 + " QUERY PROCESSING " + "="*40)
|
| 125 |
print(f"Question: {question}")
|
| 126 |
|
| 127 |
-
question_embedding =
|
| 128 |
_, indices = self.index.search(question_embedding, 3)
|
| 129 |
print(f"Top indices: {indices}")
|
| 130 |
|
|
@@ -139,12 +156,12 @@ class DocumentProcessor:
|
|
| 139 |
payload = {
|
| 140 |
"messages": [{
|
| 141 |
"role": "user",
|
| 142 |
-
"content": f"Answer concisely: {question}\nContext: {context}"
|
| 143 |
}],
|
| 144 |
"model": "mixtral-8x7b-32768",
|
| 145 |
"temperature": 0.3,
|
| 146 |
"max_tokens": MAX_TOKENS,
|
| 147 |
-
"stream":
|
| 148 |
}
|
| 149 |
|
| 150 |
response = requests.post(
|
|
@@ -159,20 +176,8 @@ class DocumentProcessor:
|
|
| 159 |
if response.status_code != 200:
|
| 160 |
return f"API Error: {response.text}", False
|
| 161 |
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
if chunk:
|
| 165 |
-
try:
|
| 166 |
-
decoded = chunk.decode('utf-8').strip()
|
| 167 |
-
if decoded.startswith('data:'):
|
| 168 |
-
data = json.loads(decoded[5:])
|
| 169 |
-
if content := data.get('choices', [{}])[0].get('delta', {}).get('content', ''):
|
| 170 |
-
full_answer.append(content)
|
| 171 |
-
except Exception as e:
|
| 172 |
-
print(f"Chunk Error: {str(e)}")
|
| 173 |
-
continue
|
| 174 |
-
|
| 175 |
-
final_answer = ''.join(full_answer)
|
| 176 |
print(f"Final Answer: {final_answer}")
|
| 177 |
return final_answer, True
|
| 178 |
|
|
@@ -189,19 +194,23 @@ def ask_question(question, chat_history):
|
|
| 189 |
answer, success = processor.query(question)
|
| 190 |
return chat_history + [(question, answer)]
|
| 191 |
|
| 192 |
-
with gr.Blocks(title="
|
| 193 |
-
gr.Markdown("## ๐ Multi-Format
|
| 194 |
with gr.Row():
|
| 195 |
-
files = gr.File(
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
|
|
|
|
|
|
| 199 |
status = gr.Textbox(label="Processing Status", interactive=False)
|
| 200 |
chatbot = gr.Chatbot(height=500, label="Chat History")
|
| 201 |
with gr.Row():
|
| 202 |
-
question = gr.Textbox(
|
| 203 |
-
|
| 204 |
-
|
|
|
|
|
|
|
| 205 |
ask_btn = gr.Button("Ask", variant="primary")
|
| 206 |
clear_btn = gr.Button("Clear Chat")
|
| 207 |
|
|
|
|
| 1 |
+
!pip install langdetect faiss-cpu transformers gradio groq sentence-transformers pypdf2 python-pptx pandas docx2txt
|
| 2 |
+
|
| 3 |
import gradio as gr
|
| 4 |
+
import fitz # PyMuPDF
|
| 5 |
import numpy as np
|
| 6 |
import requests
|
| 7 |
import faiss
|
|
|
|
| 14 |
from concurrent.futures import ThreadPoolExecutor
|
| 15 |
|
| 16 |
# Configuration
|
| 17 |
+
GROQ_API_KEY = "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" # Replace with your actual key
|
| 18 |
+
EMBEDDING_MODEL = "sentence-transformers/all-MiniLM-L6-v2" # Proper embedding model
|
| 19 |
CHUNK_SIZE = 512
|
| 20 |
MAX_TOKENS = 4096
|
|
|
|
| 21 |
WORKERS = 8
|
| 22 |
|
| 23 |
+
# Initialize the embedding model
|
| 24 |
+
embedding_model = SentenceTransformer(EMBEDDING_MODEL)
|
| 25 |
+
|
| 26 |
class DocumentProcessor:
|
| 27 |
def __init__(self):
|
| 28 |
+
self.index = faiss.IndexFlatIP(embedding_model.get_sentence_embedding_dimension())
|
| 29 |
self.chunks = []
|
| 30 |
self.processor_pool = ThreadPoolExecutor(max_workers=WORKERS)
|
| 31 |
|
|
|
|
| 83 |
return ""
|
| 84 |
|
| 85 |
def semantic_chunking(self, text):
|
| 86 |
+
sentences = re.split(r'(?<!\w\.\w.)(?<![A-Z][a-z]\.)(?<=\.|\?)\s', text)
|
| 87 |
+
chunks = []
|
| 88 |
+
current_chunk = ""
|
| 89 |
+
|
| 90 |
+
for sentence in sentences:
|
| 91 |
+
if len(current_chunk) + len(sentence) < CHUNK_SIZE:
|
| 92 |
+
current_chunk += " " + sentence
|
| 93 |
+
else:
|
| 94 |
+
if current_chunk:
|
| 95 |
+
chunks.append(current_chunk.strip())
|
| 96 |
+
current_chunk = sentence
|
| 97 |
+
|
| 98 |
+
if current_chunk:
|
| 99 |
+
chunks.append(current_chunk.strip())
|
| 100 |
+
|
| 101 |
+
return chunks[:1000] # Limit to 1000 chunks per document
|
| 102 |
|
| 103 |
def process_documents(self, files):
|
| 104 |
self.chunks = []
|
|
|
|
| 118 |
return "Error: No chunks generated from documents"
|
| 119 |
|
| 120 |
try:
|
| 121 |
+
embeddings = embedding_model.encode(
|
| 122 |
all_chunks,
|
| 123 |
+
batch_size=32,
|
| 124 |
convert_to_tensor=True,
|
| 125 |
show_progress_bar=False
|
| 126 |
).cpu().numpy().astype('float32')
|
|
|
|
| 141 |
print("\n" + "="*40 + " QUERY PROCESSING " + "="*40)
|
| 142 |
print(f"Question: {question}")
|
| 143 |
|
| 144 |
+
question_embedding = embedding_model.encode([question], convert_to_tensor=True).cpu().numpy().astype('float32')
|
| 145 |
_, indices = self.index.search(question_embedding, 3)
|
| 146 |
print(f"Top indices: {indices}")
|
| 147 |
|
|
|
|
| 156 |
payload = {
|
| 157 |
"messages": [{
|
| 158 |
"role": "user",
|
| 159 |
+
"content": f"Answer concisely based on the context: {question}\nContext: {context}"
|
| 160 |
}],
|
| 161 |
"model": "mixtral-8x7b-32768",
|
| 162 |
"temperature": 0.3,
|
| 163 |
"max_tokens": MAX_TOKENS,
|
| 164 |
+
"stream": False # Changed to False for simpler handling
|
| 165 |
}
|
| 166 |
|
| 167 |
response = requests.post(
|
|
|
|
| 176 |
if response.status_code != 200:
|
| 177 |
return f"API Error: {response.text}", False
|
| 178 |
|
| 179 |
+
data = response.json()
|
| 180 |
+
final_answer = data.get("choices", [{}])[0].get("message", {}).get("content", "")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 181 |
print(f"Final Answer: {final_answer}")
|
| 182 |
return final_answer, True
|
| 183 |
|
|
|
|
| 194 |
answer, success = processor.query(question)
|
| 195 |
return chat_history + [(question, answer)]
|
| 196 |
|
| 197 |
+
with gr.Blocks(title="Document ChatBot") as app:
|
| 198 |
+
gr.Markdown("## ๐ Multi-Format Document ChatBot")
|
| 199 |
with gr.Row():
|
| 200 |
+
files = gr.File(
|
| 201 |
+
file_count="multiple",
|
| 202 |
+
file_types=[".pdf", ".docx", ".txt", ".pptx", ".xls", ".xlsx", ".csv"],
|
| 203 |
+
label="Upload Documents"
|
| 204 |
+
)
|
| 205 |
+
process_btn = gr.Button("Process Documents", variant="primary")
|
| 206 |
status = gr.Textbox(label="Processing Status", interactive=False)
|
| 207 |
chatbot = gr.Chatbot(height=500, label="Chat History")
|
| 208 |
with gr.Row():
|
| 209 |
+
question = gr.Textbox(
|
| 210 |
+
label="Your Query",
|
| 211 |
+
placeholder="Enter your question about the documents...",
|
| 212 |
+
max_lines=3
|
| 213 |
+
)
|
| 214 |
ask_btn = gr.Button("Ask", variant="primary")
|
| 215 |
clear_btn = gr.Button("Clear Chat")
|
| 216 |
|