Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -2,84 +2,67 @@ import gradio as gr
|
|
2 |
from langchain_community.document_loaders import PyPDFLoader
|
3 |
from langchain_community.embeddings import HuggingFaceEmbeddings
|
4 |
from langchain_community.vectorstores import Chroma
|
5 |
-
from langchain_community.
|
6 |
-
from langchain.chains import
|
|
|
7 |
import os
|
8 |
import shutil
|
9 |
|
10 |
-
#
|
11 |
-
|
|
|
|
|
12 |
|
13 |
-
#
|
14 |
-
llm =
|
15 |
-
repo_id="mistralai/Mistral-7B-Instruct-v0.2",
|
16 |
-
temperature=0.2,
|
17 |
-
huggingfacehub_api_token=HUGGINGFACEHUB_API_TOKEN
|
18 |
-
)
|
19 |
|
20 |
-
# Embeddings (Hugging Face miniLM for fast processing)
|
21 |
embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
|
|
|
22 |
|
23 |
-
|
24 |
-
UPLOAD_DIR = "pdf_uploads"
|
25 |
-
if not os.path.exists(UPLOAD_DIR):
|
26 |
-
os.makedirs(UPLOAD_DIR)
|
27 |
|
28 |
-
def process_pdf(
|
29 |
-
# Save PDF file
|
30 |
-
file_path = os.path.join(UPLOAD_DIR, file.name)
|
31 |
-
with open(file_path, "wb") as f:
|
32 |
-
f.write(file.read())
|
33 |
-
|
34 |
-
# Load PDF text using langchain
|
35 |
loader = PyPDFLoader(file_path)
|
36 |
pages = loader.load_and_split()
|
37 |
-
|
38 |
-
# Create Chroma vector store (in-memory)
|
39 |
vectordb = Chroma.from_documents(pages, embedding=embeddings)
|
40 |
-
|
41 |
-
|
42 |
-
# Create RetrievalQA chain
|
43 |
-
qa_chain = RetrievalQA.from_chain_type(
|
44 |
llm=llm,
|
45 |
-
|
46 |
-
|
47 |
-
return_source_documents=True
|
48 |
)
|
49 |
-
|
50 |
-
# Return the QA chain to use in the chat
|
51 |
return qa_chain
|
52 |
|
53 |
-
# Global variable to hold QA chain for the session
|
54 |
-
qa_chain = None
|
55 |
-
|
56 |
def upload_pdf(file):
|
57 |
global qa_chain
|
58 |
-
|
|
|
|
|
59 |
return "β
PDF uploaded and processed! Ask me anything about it."
|
60 |
|
61 |
def chatbot(user_message, history):
|
62 |
if qa_chain is None:
|
63 |
return "β Please upload a PDF first.", history
|
64 |
-
|
65 |
-
response = qa_chain
|
66 |
-
|
|
|
67 |
return "", history
|
68 |
|
69 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
70 |
-
gr.Markdown("<h1 style='text-align:center;'
|
71 |
-
|
72 |
with gr.Row():
|
73 |
-
pdf_upload = gr.File(label="Upload
|
74 |
upload_btn = gr.Button("Process PDF")
|
75 |
-
|
76 |
chatbot_ui = gr.Chatbot(height=400)
|
77 |
user_input = gr.Textbox(label="Ask something about the PDF...", placeholder="Type your question here and hit Enter")
|
78 |
|
79 |
upload_btn.click(upload_pdf, inputs=pdf_upload, outputs=chatbot_ui)
|
80 |
user_input.submit(chatbot, [user_input, chatbot_ui], [user_input, chatbot_ui])
|
81 |
-
|
82 |
-
gr.Markdown("<footer style='text-align:center; font-size:0.85rem; color:#64748b;'>
|
83 |
|
84 |
if __name__ == "__main__":
|
85 |
demo.launch()
|
|
|
2 |
from langchain_community.document_loaders import PyPDFLoader
|
3 |
from langchain_community.embeddings import HuggingFaceEmbeddings
|
4 |
from langchain_community.vectorstores import Chroma
|
5 |
+
from langchain_community.chat_models import ChatGroq
|
6 |
+
from langchain.chains import ConversationalRetrievalChain
|
7 |
+
from langchain.memory import ConversationBufferMemory
|
8 |
import os
|
9 |
import shutil
|
10 |
|
11 |
+
# PDF upload folder
|
12 |
+
UPLOAD_DIR = "pdf_uploads"
|
13 |
+
if not os.path.exists(UPLOAD_DIR):
|
14 |
+
os.makedirs(UPLOAD_DIR)
|
15 |
|
16 |
+
# Initialize LLaMA 3 (no API key directly in code)
|
17 |
+
llm = ChatGroq(model_name="llama3-8b-8192")
|
|
|
|
|
|
|
|
|
18 |
|
|
|
19 |
embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
|
20 |
+
memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True)
|
21 |
|
22 |
+
qa_chain = None
|
|
|
|
|
|
|
23 |
|
24 |
+
def process_pdf(file_path):
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
loader = PyPDFLoader(file_path)
|
26 |
pages = loader.load_and_split()
|
27 |
+
|
|
|
28 |
vectordb = Chroma.from_documents(pages, embedding=embeddings)
|
29 |
+
qa_chain = ConversationalRetrievalChain.from_llm(
|
|
|
|
|
|
|
30 |
llm=llm,
|
31 |
+
retriever=vectordb.as_retriever(),
|
32 |
+
memory=memory
|
|
|
33 |
)
|
|
|
|
|
34 |
return qa_chain
|
35 |
|
|
|
|
|
|
|
36 |
def upload_pdf(file):
|
37 |
global qa_chain
|
38 |
+
file_path = os.path.join(UPLOAD_DIR, os.path.basename(file))
|
39 |
+
shutil.copy(file, file_path)
|
40 |
+
qa_chain = process_pdf(file_path)
|
41 |
return "β
PDF uploaded and processed! Ask me anything about it."
|
42 |
|
43 |
def chatbot(user_message, history):
|
44 |
if qa_chain is None:
|
45 |
return "β Please upload a PDF first.", history
|
46 |
+
|
47 |
+
response = qa_chain({"question": user_message, "chat_history": history})
|
48 |
+
answer = response["answer"]
|
49 |
+
history.append((user_message, answer))
|
50 |
return "", history
|
51 |
|
52 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
53 |
+
gr.Markdown("<h1 style='text-align:center;'>π LLaMA 3 PDF Chatbot</h1>")
|
54 |
+
|
55 |
with gr.Row():
|
56 |
+
pdf_upload = gr.File(label="Upload PDF", file_types=[".pdf"])
|
57 |
upload_btn = gr.Button("Process PDF")
|
58 |
+
|
59 |
chatbot_ui = gr.Chatbot(height=400)
|
60 |
user_input = gr.Textbox(label="Ask something about the PDF...", placeholder="Type your question here and hit Enter")
|
61 |
|
62 |
upload_btn.click(upload_pdf, inputs=pdf_upload, outputs=chatbot_ui)
|
63 |
user_input.submit(chatbot, [user_input, chatbot_ui], [user_input, chatbot_ui])
|
64 |
+
|
65 |
+
gr.Markdown("<footer style='text-align:center; font-size:0.85rem; color:#64748b;'>Built with LLaMA 3 + LangChain on Hugging Face Spaces π</footer>")
|
66 |
|
67 |
if __name__ == "__main__":
|
68 |
demo.launch()
|