Spaces:
Runtime error
Runtime error
Delete app.py
Browse files
app.py
DELETED
@@ -1,109 +0,0 @@
|
|
1 |
-
import gradio as gr
|
2 |
-
from gradio_pdf import PDF
|
3 |
-
from qdrant_client import models, QdrantClient
|
4 |
-
from sentence_transformers import SentenceTransformer
|
5 |
-
from PyPDF2 import PdfReader
|
6 |
-
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
7 |
-
from langchain.callbacks.manager import CallbackManager
|
8 |
-
from langchain.llms import LlamaCpp
|
9 |
-
from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler
|
10 |
-
from langchain.vectorstores import Qdrant
|
11 |
-
from transformers import AutoModelForCausalLM
|
12 |
-
|
13 |
-
# Load the embedding model
|
14 |
-
encoder = SentenceTransformer('jinaai/jina-embedding-b-en-v1')
|
15 |
-
print("Embedding model loaded...")
|
16 |
-
|
17 |
-
# Load the LLM
|
18 |
-
callback_manager = CallbackManager([StreamingStdOutCallbackHandler()])
|
19 |
-
'''
|
20 |
-
llm = AutoModelForCausalLM.from_pretrained(
|
21 |
-
"TheBloke/Llama-2-7B-Chat-GGUF",
|
22 |
-
model_file="llama-2-7b-chat.Q3_K_S.gguf",
|
23 |
-
model_type="llama",
|
24 |
-
temperature=0.2,
|
25 |
-
repetition_penalty=1.5,
|
26 |
-
max_new_tokens=300,
|
27 |
-
)
|
28 |
-
'''
|
29 |
-
llm = LlamaCpp(
|
30 |
-
model_path="./llama-2-7b-chat.Q3_K_S.gguf",
|
31 |
-
temperature = 0.2,
|
32 |
-
n_ctx=2048,
|
33 |
-
f16_kv=True, # MUST set to True, otherwise you will run into problem after a couple of calls
|
34 |
-
max_tokens = 500,
|
35 |
-
callback_manager=callback_manager,
|
36 |
-
verbose=True,
|
37 |
-
)
|
38 |
-
|
39 |
-
print("LLM loaded...")
|
40 |
-
|
41 |
-
client = QdrantClient(path="./db")
|
42 |
-
|
43 |
-
def setup_database(files):
|
44 |
-
all_chunks = []
|
45 |
-
for file in files:
|
46 |
-
pdf_path = file
|
47 |
-
reader = PdfReader(pdf_path)
|
48 |
-
text = "".join(page.extract_text() for page in reader.pages if page.extract_text())
|
49 |
-
text_splitter = RecursiveCharacterTextSplitter(chunk_size=250, chunk_overlap=50, length_function=len)
|
50 |
-
chunks = text_splitter.split_text(text)
|
51 |
-
all_chunks.extend(chunks)
|
52 |
-
|
53 |
-
print(f"Total chunks: {len(all_chunks)}")
|
54 |
-
|
55 |
-
client.recreate_collection(
|
56 |
-
collection_name="my_facts",
|
57 |
-
vectors_config=models.VectorParams(
|
58 |
-
size=encoder.get_sentence_embedding_dimension(),
|
59 |
-
distance=models.Distance.COSINE,
|
60 |
-
),
|
61 |
-
)
|
62 |
-
|
63 |
-
print("Collection created...")
|
64 |
-
|
65 |
-
for idx, chunk in enumerate(all_chunks):
|
66 |
-
client.upload_record(
|
67 |
-
collection_name="my_facts",
|
68 |
-
record=models.Record(
|
69 |
-
id=idx,
|
70 |
-
vector=encoder.encode(chunk).tolist(),
|
71 |
-
payload={"text": chunk}
|
72 |
-
)
|
73 |
-
)
|
74 |
-
|
75 |
-
print("Records uploaded...")
|
76 |
-
|
77 |
-
def answer(question):
|
78 |
-
hits = client.search(
|
79 |
-
collection_name="my_facts",
|
80 |
-
query_vector=encoder.encode(question).tolist(),
|
81 |
-
limit=3
|
82 |
-
)
|
83 |
-
|
84 |
-
context = " ".join(hit.payload["text"] for hit in hits)
|
85 |
-
system_prompt = "You are a helpful co-worker. Use the provided context to answer user questions. Do not use any other information."
|
86 |
-
prompt = f"Context: {context}\nUser: {question}\n{system_prompt}"
|
87 |
-
response = llm(prompt)
|
88 |
-
return response
|
89 |
-
|
90 |
-
def chat(messages):
|
91 |
-
if not messages:
|
92 |
-
return "Please upload PDF documents to initialize the database."
|
93 |
-
last_message = messages[-1]
|
94 |
-
return answer(last_message["message"])
|
95 |
-
|
96 |
-
screen = gr.Interface(
|
97 |
-
fn=chat,
|
98 |
-
inputs=gr.Textbox(placeholder="Type your question here..."),
|
99 |
-
outputs="chatbot",
|
100 |
-
title="Q&A with PDFs 👩🏻💻📓✍🏻💡",
|
101 |
-
description="This app facilitates a conversation with PDFs uploaded💡",
|
102 |
-
theme="soft",
|
103 |
-
live=True,
|
104 |
-
allow_screenshot=False,
|
105 |
-
allow_flagging=False,
|
106 |
-
)
|
107 |
-
|
108 |
-
# Add a way to upload and setup the database before starting the chat
|
109 |
-
screen.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|