File size: 2,989 Bytes
e1b6759 4a69ed6 e481fe4 4a69ed6 3bd21b0 4a69ed6 c3b340d 4a69ed6 e481fe4 4a69ed6 c3b340d 4a69ed6 7f8ff79 4a69ed6 c3b340d 4a69ed6 e481fe4 4a69ed6 adc1e0f 3bd21b0 c3b340d adc1e0f 3bd21b0 4a69ed6 3bd21b0 4a69ed6 e1b6759 4a69ed6 e1b6759 4a69ed6 e1b6759 e481fe4 4a69ed6 a0ee27c e1b6759 4a69ed6 e481fe4 e1b6759 4a69ed6 e1b6759 4a69ed6 e1b6759 4a69ed6 e1b6759 e481fe4 e1b6759 e481fe4 |
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 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
import gradio as gr
from huggingface_hub import InferenceClient
from langchain.chains import ConversationalRetrievalChain
from langchain.vectorstores import FAISS
from langchain.embeddings import HuggingFaceEmbeddings
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain.document_loaders import PyPDFLoader
from langchain_huggingface import HuggingFaceEndpoint, ChatHuggingFace
import tempfile
# Initialize global variables
vectorstore = None
retrieval_chain = None
def process_pdf(file):
global vectorstore, retrieval_chain
tmp_path = file.name
loader = PyPDFLoader(tmp_path)
documents = loader.load()
text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
docs = text_splitter.split_documents(documents)
embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
vectorstore = FAISS.from_documents(docs, embeddings)
retriever = vectorstore.as_retriever(search_kwargs={"k": 4})
# ✅ Wrap DeepSeek model properly
llm = HuggingFaceEndpoint(
repo_id="deepseek-ai/DeepSeek-R1-0528",
task="text-generation",
max_new_tokens=512,
do_sample=False,
repetition_penalty=1.03,
provider="auto", # let Hugging Face choose the best provider for you
)
chat_model = ChatHuggingFace(llm=llm)
retrieval_chain = ConversationalRetrievalChain.from_llm(
llm=chat_model,
retriever=retriever,
return_source_documents=True
)
return "PDF processed. You can now ask questions!"
def respond(
message,
history: list[dict[str, str]],
system_message,
max_tokens,
temperature,
top_p,
hf_token: gr.OAuthToken,
):
global retrieval_chain
if retrieval_chain is None:
return "Please upload a PDF first."
# Reformat history for LangChain
chat_history = [(h["content"], h.get("response", "")) for h in history if h["role"] == "user"]
result = retrieval_chain.invoke({"question": message, "chat_history": chat_history})
return result["answer"]
chatbot = gr.ChatInterface(
respond,
type="messages",
additional_inputs=[
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
gr.Slider(
minimum=0.1,
maximum=1.0,
value=0.95,
step=0.05,
label="Top-p (nucleus sampling)",
),
],
)
with gr.Blocks() as demo:
with gr.Sidebar():
gr.LoginButton()
pdf_upload = gr.File(label="Upload PDF", file_types=[".pdf"])
status = gr.Textbox(label="Status", interactive=False)
pdf_upload.upload(process_pdf, inputs=pdf_upload, outputs=status)
chatbot.render()
if __name__ == "__main__":
demo.launch()
|