Update app.py
Browse files
app.py
CHANGED
@@ -1,73 +1,26 @@
|
|
1 |
-
|
2 |
-
from
|
3 |
-
|
4 |
-
from langchain.llms import HuggingFaceHub
|
5 |
-
from langchain.embeddings import HuggingFaceHubEmbeddings
|
6 |
-
from langchain.vectorstores import Chroma
|
7 |
-
from langchain.chains import RetrievalQA
|
8 |
|
9 |
-
|
10 |
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
text_splitter = CharacterTextSplitter(chunk_size=1024, chunk_overlap=0)
|
15 |
-
texts = text_splitter.split_documents(documents)
|
16 |
-
embeddings = HuggingFaceHubEmbeddings()
|
17 |
-
db = Chroma.from_documents(texts, embeddings)
|
18 |
-
retriever = db.as_retriever()
|
19 |
-
llm = HuggingFaceHub(repo_id=repo_id, model_kwargs={'temperature': 0.5, 'max_new_tokens': 2096})
|
20 |
-
global qa
|
21 |
-
qa = RetrievalQA.from_chain_type(llm=llm, chain_type='stuff', retriever=retriever, return_source_documents=True)
|
22 |
-
return "Ready"
|
23 |
|
24 |
-
def add_text(history, text):
|
25 |
-
history = history + [(text, None)]
|
26 |
-
return history, ''
|
27 |
|
28 |
-
def bot(history):
|
29 |
-
response = infer(history[-1][0])
|
30 |
-
history[-1][1] = response['result']
|
31 |
-
return history
|
32 |
|
33 |
-
|
34 |
-
query = question
|
35 |
-
result = qa({'query': query})
|
36 |
-
return result
|
37 |
|
38 |
-
|
39 |
-
|
40 |
-
""
|
|
|
|
|
41 |
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
gr.HTML(title)
|
49 |
-
|
50 |
-
with gr.Column():
|
51 |
-
pdf_doc = gr.File(label='Upload a PDF', file_types=['.pdf'])
|
52 |
-
repo_id = gr.Dropdown(label='LLM',
|
53 |
-
choices=[
|
54 |
-
'mistralai/Mistral-7B-Instruct-v0.1',
|
55 |
-
'HuggingFaceH4/zephyr-7b-beta',
|
56 |
-
'meta-llama/Llama-2-7b-chat-hf',
|
57 |
-
'01-ai/Yi-6B-200K'
|
58 |
-
],
|
59 |
-
value='mistralai/Mistral-7B-Instruct-v0.1')
|
60 |
-
with gr.Row():
|
61 |
-
langchain_status = gr.Textbox(label='Status', placeholder='', interactive=False)
|
62 |
-
load_pdf = gr.Button('Load PDF to LangChain')
|
63 |
-
|
64 |
-
chatbot = gr.Chatbot([], elem_id='chatbot')#.style(height=350)
|
65 |
-
question = gr.Textbox(label='Question', placeholder='Type your query')
|
66 |
-
submit_btn = gr.Button('Send')
|
67 |
-
|
68 |
-
repo_id.change(pdf_changes, inputs=[pdf_doc, repo_id], outputs=[langchain_status], queue=False)
|
69 |
-
load_pdf.click(pdf_changes, inputs=[pdf_doc, repo_id], outputs=[langchain_status], queue=False)
|
70 |
-
question.submit(add_text, [chatbot, question], [chatbot, question]).then(bot, chatbot, chatbot)
|
71 |
-
submit_btn.click(add_text, [chatbot, question], [chatbot, question]).then(bot, chatbot, chatbot)
|
72 |
-
|
73 |
-
demo.launch()
|
|
|
1 |
+
from datasets import load_dataset
|
2 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig, AutoTokenizer
|
3 |
+
import torch
|
|
|
|
|
|
|
|
|
4 |
|
5 |
+
#dataset_name = "timdettmers/openassistant-guanaco" ###Human ,.,,,,,, ###Assistant
|
6 |
|
7 |
+
dataset_name = 'AlexanderDoria/novel17_test' #french novels
|
8 |
+
dataset = load_dataset(dataset_name, split="train")
|
9 |
+
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
|
|
|
|
|
|
|
11 |
|
|
|
|
|
|
|
|
|
12 |
|
13 |
+
model_name = "TinyPixel/Llama-2-7B-bf16-sharded"
|
|
|
|
|
|
|
14 |
|
15 |
+
bnb_config = BitsAndBytesConfig(
|
16 |
+
load_in_4bit=True,
|
17 |
+
bnb_4bit_quant_type="nf4",
|
18 |
+
bnb_4bit_compute_dtype=torch.float16,
|
19 |
+
)
|
20 |
|
21 |
+
model = AutoModelForCausalLM.from_pretrained(
|
22 |
+
model_name,
|
23 |
+
quantization_config=bnb_config,
|
24 |
+
trust_remote_code=True
|
25 |
+
)
|
26 |
+
model.config.use_cache = False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|