Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import gradio as gr
|
3 |
+
from langchain.vectorstores import FAISS
|
4 |
+
from langchain.embeddings import HuggingFaceEmbeddings
|
5 |
+
from langchain.document_loaders import PyPDFLoader
|
6 |
+
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
7 |
+
from langchain.chains import RetrievalQA
|
8 |
+
from langchain.chat_models import ChatGroq
|
9 |
+
from tempfile import NamedTemporaryFile
|
10 |
+
|
11 |
+
# Load Groq API Key securely (for Hugging Face secrets)
|
12 |
+
os.environ["GROQ_API_KEY"] = os.getenv("GROQ_API_KEY")
|
13 |
+
|
14 |
+
# Helper to process uploaded PDFs and build vectorstore
|
15 |
+
def process_pdfs(files):
|
16 |
+
all_docs = []
|
17 |
+
for file in files:
|
18 |
+
with NamedTemporaryFile(delete=False, suffix=".pdf") as temp_file:
|
19 |
+
temp_file.write(file.read())
|
20 |
+
loader = PyPDFLoader(temp_file.name)
|
21 |
+
all_docs.extend(loader.load())
|
22 |
+
|
23 |
+
splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=50)
|
24 |
+
chunks = splitter.split_documents(all_docs)
|
25 |
+
|
26 |
+
embeddings = HuggingFaceEmbeddings(model_name="all-MiniLM-L6-v2")
|
27 |
+
vectorstore = FAISS.from_documents(chunks, embeddings)
|
28 |
+
retriever = vectorstore.as_retriever()
|
29 |
+
|
30 |
+
llm = ChatGroq(model_name="mixtral-8x7b-32768", temperature=0)
|
31 |
+
qa_chain = RetrievalQA.from_chain_type(llm=llm, retriever=retriever)
|
32 |
+
return qa_chain
|
33 |
+
|
34 |
+
# Global chain (reset with new uploads)
|
35 |
+
qa_chain = None
|
36 |
+
|
37 |
+
# Upload + Process PDFs
|
38 |
+
def upload_pdfs(files):
|
39 |
+
global qa_chain
|
40 |
+
qa_chain = process_pdfs(files)
|
41 |
+
return "✅ PDFs uploaded and processed. Now ask your questions."
|
42 |
+
|
43 |
+
# Ask a question
|
44 |
+
def ask_question(query):
|
45 |
+
if qa_chain is None:
|
46 |
+
return "❌ Please upload Kaggle notebooks/competition PDFs first."
|
47 |
+
result = qa_chain.run(query)
|
48 |
+
return result
|
49 |
+
|
50 |
+
# Gradio UI
|
51 |
+
upload = gr.File(file_types=[".pdf"], file_count="multiple", label="Upload Kaggle PDFs")
|
52 |
+
btn_upload = gr.Button("Process PDFs")
|
53 |
+
question = gr.Textbox(label="Ask a question about uploaded notebooks")
|
54 |
+
answer = gr.Textbox(label="Assistant Answer")
|
55 |
+
|
56 |
+
with gr.Blocks() as app:
|
57 |
+
gr.Markdown("## 🤖 Kaggle Study Assistant\nUpload PDFs from Kaggle and ask intelligent questions.")
|
58 |
+
upload_output = gr.Textbox(visible=True)
|
59 |
+
btn_upload.click(fn=upload_pdfs, inputs=upload, outputs=upload_output)
|
60 |
+
question.submit(fn=ask_question, inputs=question, outputs=answer)
|
61 |
+
|
62 |
+
app.launch()
|