Update app.py
Browse files
app.py
CHANGED
|
@@ -1,5 +1,50 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
|
| 5 |
def respond(
|
|
@@ -11,38 +56,19 @@ def respond(
|
|
| 11 |
top_p,
|
| 12 |
hf_token: gr.OAuthToken,
|
| 13 |
):
|
| 14 |
-
|
| 15 |
-
For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
|
| 16 |
-
"""
|
| 17 |
-
client = InferenceClient(token=hf_token.token, model="openai/gpt-oss-20b")
|
| 18 |
-
|
| 19 |
-
messages = [{"role": "system", "content": system_message}]
|
| 20 |
-
|
| 21 |
-
messages.extend(history)
|
| 22 |
|
| 23 |
-
|
|
|
|
| 24 |
|
| 25 |
-
|
|
|
|
| 26 |
|
| 27 |
-
|
| 28 |
-
messages,
|
| 29 |
-
max_tokens=max_tokens,
|
| 30 |
-
stream=True,
|
| 31 |
-
temperature=temperature,
|
| 32 |
-
top_p=top_p,
|
| 33 |
-
):
|
| 34 |
-
choices = message.choices
|
| 35 |
-
token = ""
|
| 36 |
-
if len(choices) and choices[0].delta.content:
|
| 37 |
-
token = choices[0].delta.content
|
| 38 |
|
| 39 |
-
|
| 40 |
-
yield response
|
| 41 |
|
| 42 |
|
| 43 |
-
"""
|
| 44 |
-
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
| 45 |
-
"""
|
| 46 |
chatbot = gr.ChatInterface(
|
| 47 |
respond,
|
| 48 |
type="messages",
|
|
@@ -63,6 +89,8 @@ chatbot = gr.ChatInterface(
|
|
| 63 |
with gr.Blocks() as demo:
|
| 64 |
with gr.Sidebar():
|
| 65 |
gr.LoginButton()
|
|
|
|
|
|
|
| 66 |
chatbot.render()
|
| 67 |
|
| 68 |
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
+
from langchain.chains import ConversationalRetrievalChain
|
| 4 |
+
from langchain.vectorstores import FAISS
|
| 5 |
+
from langchain.embeddings import HuggingFaceEmbeddings
|
| 6 |
+
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
| 7 |
+
from langchain.document_loaders import PyPDFLoader
|
| 8 |
+
import tempfile
|
| 9 |
+
|
| 10 |
+
# Initialize global variables
|
| 11 |
+
vectorstore = None
|
| 12 |
+
retrieval_chain = None
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
def process_pdf(file):
|
| 16 |
+
global vectorstore, retrieval_chain
|
| 17 |
+
|
| 18 |
+
# Save uploaded PDF temporarily
|
| 19 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as tmp:
|
| 20 |
+
tmp.write(file.read())
|
| 21 |
+
tmp_path = tmp.name
|
| 22 |
+
|
| 23 |
+
# Load PDF
|
| 24 |
+
loader = PyPDFLoader(tmp_path)
|
| 25 |
+
documents = loader.load()
|
| 26 |
+
|
| 27 |
+
# Split into chunks
|
| 28 |
+
text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
|
| 29 |
+
docs = text_splitter.split_documents(documents)
|
| 30 |
+
|
| 31 |
+
# Create embeddings and FAISS vectorstore
|
| 32 |
+
embeddings = HuggingFaceEmbeddings()
|
| 33 |
+
vectorstore = FAISS.from_documents(docs, embeddings)
|
| 34 |
+
|
| 35 |
+
# Setup retrieval chain with HuggingFace inference client
|
| 36 |
+
retriever = vectorstore.as_retriever()
|
| 37 |
+
retriever.search_kwargs["k"] = 4
|
| 38 |
+
|
| 39 |
+
client = InferenceClient(model="deepseek-ai/DeepSeek-R1-0528")
|
| 40 |
+
|
| 41 |
+
retrieval_chain = ConversationalRetrievalChain.from_llm(
|
| 42 |
+
llm=client,
|
| 43 |
+
retriever=retriever,
|
| 44 |
+
return_source_documents=True
|
| 45 |
+
)
|
| 46 |
+
|
| 47 |
+
return "PDF processed. You can now ask questions!"
|
| 48 |
|
| 49 |
|
| 50 |
def respond(
|
|
|
|
| 56 |
top_p,
|
| 57 |
hf_token: gr.OAuthToken,
|
| 58 |
):
|
| 59 |
+
global retrieval_chain
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
|
| 61 |
+
if retrieval_chain is None:
|
| 62 |
+
return "Please upload a PDF first."
|
| 63 |
|
| 64 |
+
# Reformat history for LangChain
|
| 65 |
+
chat_history = [(h["content"], h.get("response", "")) for h in history if h["role"] == "user"]
|
| 66 |
|
| 67 |
+
result = retrieval_chain({"question": message, "chat_history": chat_history})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
|
| 69 |
+
return result["answer"]
|
|
|
|
| 70 |
|
| 71 |
|
|
|
|
|
|
|
|
|
|
| 72 |
chatbot = gr.ChatInterface(
|
| 73 |
respond,
|
| 74 |
type="messages",
|
|
|
|
| 89 |
with gr.Blocks() as demo:
|
| 90 |
with gr.Sidebar():
|
| 91 |
gr.LoginButton()
|
| 92 |
+
pdf_upload = gr.File(label="Upload PDF", file_types=[".pdf"])
|
| 93 |
+
pdf_upload.upload(process_pdf, inputs=pdf_upload, outputs=[])
|
| 94 |
chatbot.render()
|
| 95 |
|
| 96 |
|