Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,7 +2,6 @@ import os
|
|
| 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 |
|
|
@@ -40,16 +39,16 @@ def chunk_text(text, max_length=500):
|
|
| 40 |
def compute_embeddings(chunks):
|
| 41 |
embeddings = []
|
| 42 |
for chunk in chunks:
|
| 43 |
-
vector =
|
| 44 |
-
padded_vector =
|
| 45 |
-
embeddings.append(padded_vector
|
| 46 |
-
return
|
| 47 |
|
| 48 |
# Function to create FAISS index
|
| 49 |
def create_faiss_index(embeddings):
|
| 50 |
-
dimension = embeddings
|
| 51 |
index = faiss.IndexFlatL2(dimension)
|
| 52 |
-
index.add(embeddings)
|
| 53 |
return index
|
| 54 |
|
| 55 |
# Function to query Groq API
|
|
@@ -80,7 +79,7 @@ def main():
|
|
| 80 |
if question:
|
| 81 |
st.write("Searching for relevant chunks...")
|
| 82 |
question_embedding = compute_embeddings([question])[0]
|
| 83 |
-
_, indices = index.search(
|
| 84 |
relevant_chunk = chunks[indices[0][0]]
|
| 85 |
|
| 86 |
st.write("Generating answer using Groq API...")
|
|
@@ -91,4 +90,3 @@ def main():
|
|
| 91 |
if __name__ == "__main__":
|
| 92 |
main()
|
| 93 |
|
| 94 |
-
|
|
|
|
| 2 |
import streamlit as st
|
| 3 |
import PyPDF2
|
| 4 |
import requests
|
|
|
|
| 5 |
import faiss
|
| 6 |
from groq import Groq
|
| 7 |
|
|
|
|
| 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 + [0] * (300 - len(vector)) # Zero-pad to fixed size
|
| 44 |
+
embeddings.append(padded_vector)
|
| 45 |
+
return embeddings
|
| 46 |
|
| 47 |
# Function to create FAISS index
|
| 48 |
def create_faiss_index(embeddings):
|
| 49 |
+
dimension = len(embeddings[0])
|
| 50 |
index = faiss.IndexFlatL2(dimension)
|
| 51 |
+
index.add(faiss.FloatVectorArray(embeddings))
|
| 52 |
return index
|
| 53 |
|
| 54 |
# Function to query Groq API
|
|
|
|
| 79 |
if question:
|
| 80 |
st.write("Searching for relevant chunks...")
|
| 81 |
question_embedding = compute_embeddings([question])[0]
|
| 82 |
+
_, indices = index.search(faiss.FloatVectorArray([question_embedding]), k=1)
|
| 83 |
relevant_chunk = chunks[indices[0][0]]
|
| 84 |
|
| 85 |
st.write("Generating answer using Groq API...")
|
|
|
|
| 90 |
if __name__ == "__main__":
|
| 91 |
main()
|
| 92 |
|
|
|