-
-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
175 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,7 @@ | ||
VOLUME="/source/local-machine/dir:target/multi-container/app/dir" | ||
# VOLUME="c:/Users/User/:/User" e.g. | ||
# VOLUME="c:/Users/User/:/User" e.g. | ||
MODELS_PATH="/path/to/gguf/models" | ||
# MODELS_PATH="c:/Users/User/.cache/llama.cpp/" | ||
MODEL="your_favorite_model.gguf" | ||
# MODEL="stories260K.gguf" | ||
MAX_TOKENS="512" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
from utils import Translation, PDFdatabase, NeuralSearcher | ||
import gradio as gr | ||
from qdrant_client import QdrantClient | ||
from sentence_transformers import SentenceTransformer | ||
from argparse import ArgumentParser | ||
import os | ||
|
||
argparse = ArgumentParser() | ||
|
||
argparse.add_argument( | ||
"-pf", | ||
"--pdf_file", | ||
help="Single pdf file or N pdfs reported like this: /path/to/file1.pdf,/path/to/file2.pdf,...,/path/to/fileN.pdf (there is no strict naming, you just need to provide them comma-separated)", | ||
required=False, | ||
default="No file" | ||
) | ||
|
||
argparse.add_argument( | ||
"-d", | ||
"--directory", | ||
help="Directory where all your pdfs of interest are stored", | ||
required=False, | ||
default="No directory" | ||
) | ||
|
||
argparse.add_argument( | ||
"-l", | ||
"--language", | ||
help="Language of the written content contained in the pdfs", | ||
required=False, | ||
default="Same as query" | ||
) | ||
|
||
args = argparse.parse_args() | ||
|
||
|
||
pdff = args.pdf_file | ||
dirs = args.directory | ||
lan = args.language | ||
|
||
|
||
if pdff.replace("\\","").replace("'","") != "None" and dirs.replace("\\","").replace("'","") == "No directory": | ||
pdfs = pdff.replace("\\","/").replace("'","").split(",") | ||
else: | ||
pdfs = [os.path.join(dirs.replace("\\","/").replace("'",""), f) for f in os.listdir(dirs.replace("\\","/").replace("'","")) if f.endswith(".pdf")] | ||
|
||
client = QdrantClient(host="host.docker.internal", port="6333") | ||
encoder = SentenceTransformer("all-MiniLM-L6-v2") | ||
|
||
pdfdb = PDFdatabase(pdfs, encoder, client) | ||
pdfdb.preprocess() | ||
pdfdb.collect_data() | ||
pdfdb.qdrant_collection_and_upload() | ||
|
||
|
||
import requests | ||
|
||
def llama_cpp_respond(query, max_new_tokens): | ||
url = "http://host.docker.internal:8000/completion" | ||
headers = { | ||
"Content-Type": "application/json" | ||
} | ||
data = { | ||
"prompt": query, | ||
"n_predict": int(max_new_tokens) | ||
} | ||
|
||
response = requests.post(url, headers=headers, json=data) | ||
|
||
a = response.json() | ||
return a["content"] | ||
|
||
|
||
def reply(max_new_tokens, message): | ||
global pdfdb | ||
txt = Translation(message, "en") | ||
if txt.original == "en" and lan.replace("\\","").replace("'","") == "None": | ||
txt2txt = NeuralSearcher(pdfdb.collection_name, pdfdb.client, pdfdb.encoder) | ||
results = txt2txt.search(message) | ||
response = llama_cpp_respond(results[0]["text"], max_new_tokens) | ||
return response | ||
elif txt.original == "en" and lan.replace("\\","").replace("'","") != "None": | ||
txt2txt = NeuralSearcher(pdfdb.collection_name, pdfdb.client, pdfdb.encoder) | ||
transl = Translation(message, lan.replace("\\","").replace("'","")) | ||
message = transl.translatef() | ||
results = txt2txt.search(message) | ||
t = Translation(results[0]["text"], txt.original) | ||
res = t.translatef() | ||
response = llama_cpp_respond(res, max_new_tokens) | ||
return response | ||
elif txt.original != "en" and lan.replace("\\","").replace("'","") == "None": | ||
txt2txt = NeuralSearcher(pdfdb.collection_name, pdfdb.client, pdfdb.encoder) | ||
results = txt2txt.search(message) | ||
transl = Translation(results[0]["text"], "en") | ||
translation = transl.translatef() | ||
response = llama_cpp_respond(translation, max_new_tokens) | ||
t = Translation(response, txt.original) | ||
res = t.translatef() | ||
return res | ||
else: | ||
txt2txt = NeuralSearcher(pdfdb.collection_name, pdfdb.client, pdfdb.encoder) | ||
transl = Translation(message, lan.replace("\\","").replace("'","")) | ||
message = transl.translatef() | ||
results = txt2txt.search(message) | ||
t = Translation(results[0]["text"], txt.original) | ||
res = t.translatef() | ||
response = llama_cpp_respond(res, max_new_tokens) | ||
tr = Translation(response, txt.original) | ||
ress = tr.translatef() | ||
return ress | ||
|
||
demo = gr.Interface( | ||
reply, | ||
[ | ||
gr.Textbox( | ||
label="Max new tokens", | ||
info="The number reported should not be higher than the one specified within the .env file", | ||
lines=3, | ||
value=f"512", | ||
), | ||
gr.Textbox( | ||
label="Input query", | ||
info="Write your input query here", | ||
lines=3, | ||
value=f"What are penguins?", | ||
) | ||
], | ||
title="everything-ai-llamacpp", | ||
outputs="textbox" | ||
) | ||
demo.launch(server_name="0.0.0.0", share=False) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters