File size: 2,197 Bytes
736448d
1a7b2d4
 
736448d
09aa142
736448d
09aa142
 
 
 
1a7b2d4
09aa142
1a7b2d4
 
09aa142
1a7b2d4
 
09aa142
 
 
 
736448d
09aa142
 
 
 
 
 
736448d
09aa142
 
 
736448d
09aa142
 
 
736448d
09aa142
736448d
09aa142
 
 
 
736448d
09aa142
 
 
 
 
736448d
09aa142
736448d
09aa142
 
 
736448d
09aa142
 
 
736448d
09aa142
 
 
 
 
 
736448d
09aa142
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import os
import streamlit as st
from groq import Groq
from langchain.embeddings import HuggingFaceEmbeddings
from langchain.vectorstores import FAISS
from langchain.text_splitter import CharacterTextSplitter
from langchain.document_loaders import TextLoader, PyPDFLoader
from langchain.chains import RetrievalQA
from langchain.llms.base import LLM
from tempfile import NamedTemporaryFile

# Load Groq API key from environment variable
GROQ_API_KEY = os.getenv("GROQ_API_KEY")

# Initialize Groq client
groq_client = Groq(api_key=GROQ_API_KEY)

# Define a basic LLM wrapper for Groq
class GroqLLM(LLM):
    def __init__(self, model_name="llama3-8b-8192"):
        self.model_name = model_name

    def _call(self, prompt, stop=None):
        response = groq_client.chat.completions.create(
            model=self.model_name,
            messages=[{"role": "user", "content": prompt}],
        )
        return response.choices[0].message.content.strip()

    @property
    def _llm_type(self):
        return "groq_llm"

# Streamlit UI
st.title("📚 RAG App with Groq + HuggingFace + Streamlit")
st.write("Upload a PDF or TXT file, ask a question, and get answers powered by RAG.")

uploaded_file = st.file_uploader("Upload your document", type=["pdf", "txt"])

if uploaded_file:
    with NamedTemporaryFile(delete=False) as tmp_file:
        tmp_file.write(uploaded_file.read())
        tmp_path = tmp_file.name

    # Load document
    if uploaded_file.type == "application/pdf":
        loader = PyPDFLoader(tmp_path)
    else:
        loader = TextLoader(tmp_path)

    docs = loader.load()

    # Split into chunks
    splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
    texts = splitter.split_documents(docs)

    # Create embeddings and FAISS index
    embeddings = HuggingFaceEmbeddings()
    db = FAISS.from_documents(texts, embeddings)

    # RAG chain
    retriever = db.as_retriever()
    qa_chain = RetrievalQA.from_chain_type(llm=GroqLLM(), retriever=retriever)

    # Input box
    query = st.text_input("Ask something about the document:")

    if query:
        result = qa_chain.run(query)
        st.markdown("### 🧠 Answer:")
        st.success(result)