Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,17 +1,11 @@
|
|
1 |
import gradio as gr
|
2 |
-
from huggingface_hub import InferenceClient
|
3 |
from pathlib import Path
|
4 |
-
from
|
5 |
from pdfplumber import open as open_pdf
|
6 |
-
from
|
7 |
-
|
8 |
-
"""
|
9 |
-
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
|
10 |
-
"""
|
11 |
-
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
12 |
|
13 |
# Load the PDF file
|
14 |
-
pdf_path = Path("
|
15 |
with open_pdf(pdf_path) as pdf:
|
16 |
text = "\n".join(page.extract_text() for page in pdf.pages)
|
17 |
|
@@ -19,9 +13,13 @@ with open_pdf(pdf_path) as pdf:
|
|
19 |
chunk_size = 1000 # Adjust this value based on your needs
|
20 |
text_chunks: List[str] = [text[i:i+chunk_size] for i in range(0, len(text), chunk_size)]
|
21 |
|
22 |
-
# Load the
|
23 |
-
|
24 |
-
|
|
|
|
|
|
|
|
|
25 |
|
26 |
def respond(
|
27 |
message,
|
@@ -43,15 +41,16 @@ def respond(
|
|
43 |
|
44 |
response = ""
|
45 |
|
46 |
-
#
|
47 |
-
|
48 |
-
|
|
|
49 |
|
50 |
-
# Encode the context and user's message
|
51 |
-
input_ids =
|
52 |
|
53 |
-
# Generate the response using the
|
54 |
-
output =
|
55 |
input_ids,
|
56 |
max_length=max_tokens,
|
57 |
num_beams=num_beams,
|
@@ -59,7 +58,7 @@ def respond(
|
|
59 |
early_stopping=True
|
60 |
)
|
61 |
|
62 |
-
response =
|
63 |
|
64 |
yield response
|
65 |
|
|
|
1 |
import gradio as gr
|
|
|
2 |
from pathlib import Path
|
3 |
+
from transformers import RAGTokenForingModel, AutoTokenizer, AutoModelForCausalLM
|
4 |
from pdfplumber import open as open_pdf
|
5 |
+
from typing import List
|
|
|
|
|
|
|
|
|
|
|
6 |
|
7 |
# Load the PDF file
|
8 |
+
pdf_path = Path("path/to/your/pdf/file.pdf")
|
9 |
with open_pdf(pdf_path) as pdf:
|
10 |
text = "\n".join(page.extract_text() for page in pdf.pages)
|
11 |
|
|
|
13 |
chunk_size = 1000 # Adjust this value based on your needs
|
14 |
text_chunks: List[str] = [text[i:i+chunk_size] for i in range(0, len(text), chunk_size)]
|
15 |
|
16 |
+
# Load the RAG model and tokenizer for retrieval
|
17 |
+
rag_tokenizer = AutoTokenizer.from_pretrained("facebook/rag-token-nq")
|
18 |
+
rag_model = RAGTokenForingModel.from_pretrained("facebook/rag-token-nq")
|
19 |
+
|
20 |
+
# Load the DialoGPT model and tokenizer for generation
|
21 |
+
dialogpt_tokenizer = AutoTokenizer.from_pretrained("microsoft/DialoGPT-medium")
|
22 |
+
dialogpt_model = AutoModelForCausalLM.from_pretrained("microsoft/DialoGPT-medium")
|
23 |
|
24 |
def respond(
|
25 |
message,
|
|
|
41 |
|
42 |
response = ""
|
43 |
|
44 |
+
# Retrieve relevant chunks using the RAG model
|
45 |
+
rag_input_ids = rag_tokenizer(message, return_tensors="pt").input_ids
|
46 |
+
rag_output = rag_model(rag_input_ids, text_chunks, return_retrieved_inputs=True)
|
47 |
+
retrieved_text = rag_output.retrieved_inputs
|
48 |
|
49 |
+
# Encode the context and user's message for DialoGPT
|
50 |
+
input_ids = dialogpt_tokenizer.encode(retrieved_text + "\n\n" + message, return_tensors="pt")
|
51 |
|
52 |
+
# Generate the response using the DialoGPT model
|
53 |
+
output = dialogpt_model.generate(
|
54 |
input_ids,
|
55 |
max_length=max_tokens,
|
56 |
num_beams=num_beams,
|
|
|
58 |
early_stopping=True
|
59 |
)
|
60 |
|
61 |
+
response = dialogpt_tokenizer.decode(output[0], skip_special_tokens=True)
|
62 |
|
63 |
yield response
|
64 |
|