Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,6 +2,7 @@ import os
|
|
| 2 |
import streamlit as st
|
| 3 |
import PyPDF2
|
| 4 |
import requests
|
|
|
|
| 5 |
import faiss
|
| 6 |
from groq import Groq
|
| 7 |
|
|
@@ -35,20 +36,20 @@ def chunk_text(text, max_length=500):
|
|
| 35 |
chunks.append(chunk.strip())
|
| 36 |
return chunks
|
| 37 |
|
| 38 |
-
# Function to compute
|
| 39 |
def compute_embeddings(chunks):
|
| 40 |
embeddings = []
|
| 41 |
for chunk in chunks:
|
| 42 |
-
vector = [ord(char) for char in chunk[:300]] # Truncate to 300 characters
|
| 43 |
-
padded_vector = vector
|
| 44 |
embeddings.append(padded_vector)
|
| 45 |
-
return embeddings
|
| 46 |
|
| 47 |
# Function to create FAISS index
|
| 48 |
def create_faiss_index(embeddings):
|
| 49 |
-
dimension =
|
| 50 |
index = faiss.IndexFlatL2(dimension)
|
| 51 |
-
index.add(
|
| 52 |
return index
|
| 53 |
|
| 54 |
# Function to query Groq API
|
|
@@ -78,8 +79,8 @@ def main():
|
|
| 78 |
question = st.text_input("Ask a question based on the document:")
|
| 79 |
if question:
|
| 80 |
st.write("Searching for relevant chunks...")
|
| 81 |
-
question_embedding = compute_embeddings([question])[0]
|
| 82 |
-
|
| 83 |
relevant_chunk = chunks[indices[0][0]]
|
| 84 |
|
| 85 |
st.write("Generating answer using Groq API...")
|
|
@@ -90,3 +91,4 @@ def main():
|
|
| 90 |
if __name__ == "__main__":
|
| 91 |
main()
|
| 92 |
|
|
|
|
|
|
| 2 |
import streamlit as st
|
| 3 |
import PyPDF2
|
| 4 |
import requests
|
| 5 |
+
import numpy as np
|
| 6 |
import faiss
|
| 7 |
from groq import Groq
|
| 8 |
|
|
|
|
| 36 |
chunks.append(chunk.strip())
|
| 37 |
return chunks
|
| 38 |
|
| 39 |
+
# Function to compute embeddings using NumPy
|
| 40 |
def compute_embeddings(chunks):
|
| 41 |
embeddings = []
|
| 42 |
for chunk in chunks:
|
| 43 |
+
vector = np.array([ord(char) for char in chunk[:300]], dtype=np.float32) # Truncate to 300 characters
|
| 44 |
+
padded_vector = np.pad(vector, (0, 300 - len(vector)), mode="constant")
|
| 45 |
embeddings.append(padded_vector)
|
| 46 |
+
return np.vstack(embeddings)
|
| 47 |
|
| 48 |
# Function to create FAISS index
|
| 49 |
def create_faiss_index(embeddings):
|
| 50 |
+
dimension = embeddings.shape[1]
|
| 51 |
index = faiss.IndexFlatL2(dimension)
|
| 52 |
+
index.add(embeddings)
|
| 53 |
return index
|
| 54 |
|
| 55 |
# Function to query Groq API
|
|
|
|
| 79 |
question = st.text_input("Ask a question based on the document:")
|
| 80 |
if question:
|
| 81 |
st.write("Searching for relevant chunks...")
|
| 82 |
+
question_embedding = compute_embeddings([question])[0].reshape(1, -1)
|
| 83 |
+
distances, indices = index.search(question_embedding, k=1)
|
| 84 |
relevant_chunk = chunks[indices[0][0]]
|
| 85 |
|
| 86 |
st.write("Generating answer using Groq API...")
|
|
|
|
| 91 |
if __name__ == "__main__":
|
| 92 |
main()
|
| 93 |
|
| 94 |
+
|