Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,68 +1,71 @@
|
|
| 1 |
import os
|
| 2 |
import streamlit as st
|
| 3 |
from groq import Groq
|
| 4 |
-
from langchain.vectorstores import FAISS
|
| 5 |
from langchain.embeddings import HuggingFaceEmbeddings
|
| 6 |
-
from langchain.
|
| 7 |
-
from langchain.prompts import PromptTemplate
|
| 8 |
-
from langchain.document_loaders import TextLoader
|
| 9 |
from langchain.text_splitter import CharacterTextSplitter
|
| 10 |
-
from
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
-
# API key from
|
| 13 |
GROQ_API_KEY = os.getenv("GROQ_API_KEY")
|
| 14 |
|
| 15 |
-
#
|
| 16 |
groq_client = Groq(api_key=GROQ_API_KEY)
|
| 17 |
|
| 18 |
-
#
|
| 19 |
-
|
| 20 |
-
|
|
|
|
| 21 |
|
| 22 |
-
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
f.write(uploaded_file.read())
|
| 29 |
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
splitter = CharacterTextSplitter(chunk_size=500, chunk_overlap=50)
|
| 34 |
-
docs = splitter.split_documents(documents)
|
| 35 |
|
| 36 |
-
|
| 37 |
-
st.info("π Generating embeddings...")
|
| 38 |
-
embeddings = HuggingFaceEmbeddings()
|
| 39 |
-
vectorstore = FAISS.from_documents(docs, embeddings)
|
| 40 |
|
| 41 |
-
|
| 42 |
-
|
|
|
|
|
|
|
| 43 |
|
| 44 |
-
#
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
|
|
|
| 48 |
|
| 49 |
-
|
| 50 |
|
| 51 |
-
#
|
| 52 |
-
|
|
|
|
| 53 |
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
context = "\n\n".join([doc.page_content for doc in results[:3]])
|
| 58 |
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
st.
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
import streamlit as st
|
| 3 |
from groq import Groq
|
|
|
|
| 4 |
from langchain.embeddings import HuggingFaceEmbeddings
|
| 5 |
+
from langchain.vectorstores import FAISS
|
|
|
|
|
|
|
| 6 |
from langchain.text_splitter import CharacterTextSplitter
|
| 7 |
+
from langchain.document_loaders import TextLoader, PyPDFLoader
|
| 8 |
+
from langchain.chains import RetrievalQA
|
| 9 |
+
from langchain.llms.base import LLM
|
| 10 |
+
from tempfile import NamedTemporaryFile
|
| 11 |
|
| 12 |
+
# Load Groq API key from environment variable
|
| 13 |
GROQ_API_KEY = os.getenv("GROQ_API_KEY")
|
| 14 |
|
| 15 |
+
# Initialize Groq client
|
| 16 |
groq_client = Groq(api_key=GROQ_API_KEY)
|
| 17 |
|
| 18 |
+
# Define a basic LLM wrapper for Groq
|
| 19 |
+
class GroqLLM(LLM):
|
| 20 |
+
def __init__(self, model_name="llama3-8b-8192"):
|
| 21 |
+
self.model_name = model_name
|
| 22 |
|
| 23 |
+
def _call(self, prompt, stop=None):
|
| 24 |
+
response = groq_client.chat.completions.create(
|
| 25 |
+
model=self.model_name,
|
| 26 |
+
messages=[{"role": "user", "content": prompt}],
|
| 27 |
+
)
|
| 28 |
+
return response.choices[0].message.content.strip()
|
| 29 |
|
| 30 |
+
@property
|
| 31 |
+
def _llm_type(self):
|
| 32 |
+
return "groq_llm"
|
|
|
|
| 33 |
|
| 34 |
+
# Streamlit UI
|
| 35 |
+
st.title("π RAG App with Groq + HuggingFace + Streamlit")
|
| 36 |
+
st.write("Upload a PDF or TXT file, ask a question, and get answers powered by RAG.")
|
|
|
|
|
|
|
| 37 |
|
| 38 |
+
uploaded_file = st.file_uploader("Upload your document", type=["pdf", "txt"])
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
+
if uploaded_file:
|
| 41 |
+
with NamedTemporaryFile(delete=False) as tmp_file:
|
| 42 |
+
tmp_file.write(uploaded_file.read())
|
| 43 |
+
tmp_path = tmp_file.name
|
| 44 |
|
| 45 |
+
# Load document
|
| 46 |
+
if uploaded_file.type == "application/pdf":
|
| 47 |
+
loader = PyPDFLoader(tmp_path)
|
| 48 |
+
else:
|
| 49 |
+
loader = TextLoader(tmp_path)
|
| 50 |
|
| 51 |
+
docs = loader.load()
|
| 52 |
|
| 53 |
+
# Split into chunks
|
| 54 |
+
splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
|
| 55 |
+
texts = splitter.split_documents(docs)
|
| 56 |
|
| 57 |
+
# Create embeddings and FAISS index
|
| 58 |
+
embeddings = HuggingFaceEmbeddings()
|
| 59 |
+
db = FAISS.from_documents(texts, embeddings)
|
|
|
|
| 60 |
|
| 61 |
+
# RAG chain
|
| 62 |
+
retriever = db.as_retriever()
|
| 63 |
+
qa_chain = RetrievalQA.from_chain_type(llm=GroqLLM(), retriever=retriever)
|
| 64 |
+
|
| 65 |
+
# Input box
|
| 66 |
+
query = st.text_input("Ask something about the document:")
|
| 67 |
|
| 68 |
+
if query:
|
| 69 |
+
result = qa_chain.run(query)
|
| 70 |
+
st.markdown("### π§ Answer:")
|
| 71 |
+
st.success(result)
|