JoThanos
commited on
Commit
·
d6a952e
1
Parent(s):
6e4c902
add sentencepiece
Browse files- app.py +3 -122
- requirements.txt +2 -1
app.py
CHANGED
@@ -1,3 +1,4 @@
|
|
|
|
1 |
import torch
|
2 |
import gradio as gr
|
3 |
from textwrap import fill
|
@@ -18,11 +19,10 @@ from langchain.chains import LLMChain, SimpleSequentialChain, RetrievalQA, Conve
|
|
18 |
from transformers import BitsAndBytesConfig, AutoModelForCausalLM, AutoTokenizer, GenerationConfig, pipeline
|
19 |
import warnings
|
20 |
from huggingface_hub import login
|
21 |
-
import os
|
22 |
|
23 |
warnings.filterwarnings('ignore')
|
24 |
|
25 |
-
#
|
26 |
huggingface_token = os.getenv('huggingface_token')
|
27 |
login(huggingface_token)
|
28 |
|
@@ -118,123 +118,4 @@ iface = gr.ChatInterface(
|
|
118 |
submit_btn="Enviar"
|
119 |
)
|
120 |
|
121 |
-
iface.launch(share=True)
|
122 |
-
import torch
|
123 |
-
import gradio as gr
|
124 |
-
from textwrap import fill
|
125 |
-
from langchain.prompts.chat import (
|
126 |
-
ChatPromptTemplate,
|
127 |
-
HumanMessagePromptTemplate,
|
128 |
-
SystemMessagePromptTemplate,
|
129 |
-
)
|
130 |
-
from langchain import PromptTemplate
|
131 |
-
from langchain import HuggingFacePipeline
|
132 |
-
from langchain.vectorstores import Chroma
|
133 |
-
from langchain.schema import AIMessage, HumanMessage
|
134 |
-
from langchain.memory import ConversationBufferMemory
|
135 |
-
from langchain.embeddings import HuggingFaceEmbeddings
|
136 |
-
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
137 |
-
from langchain.document_loaders import UnstructuredMarkdownLoader, UnstructuredURLLoader
|
138 |
-
from langchain.chains import LLMChain, SimpleSequentialChain, RetrievalQA, ConversationalRetrievalChain
|
139 |
-
from transformers import BitsAndBytesConfig, AutoModelForCausalLM, AutoTokenizer, GenerationConfig, pipeline
|
140 |
-
import warnings
|
141 |
-
from huggingface_hub import login
|
142 |
-
|
143 |
-
warnings.filterwarnings('ignore')
|
144 |
-
|
145 |
-
# Login to Hugging Face
|
146 |
-
login("your_huggingface_token")
|
147 |
-
|
148 |
-
MODEL_NAME = "mistralai/Mistral-7B-Instruct-v0.3"
|
149 |
-
EMBEDDING_MODEL = 'sentence-transformers/paraphrase-multilingual-mpnet-base-v2'
|
150 |
-
|
151 |
-
quantization_config = BitsAndBytesConfig(
|
152 |
-
load_in_4bit=True,
|
153 |
-
bnb_4bit_compute_dtype=torch.float16,
|
154 |
-
bnb_4bit_quant_type="nf4",
|
155 |
-
bnb_4bit_use_double_quant=True,
|
156 |
-
)
|
157 |
-
|
158 |
-
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME, use_fast=True)
|
159 |
-
tokenizer.pad_token = tokenizer.eos_token
|
160 |
-
|
161 |
-
model = AutoModelForCausalLM.from_pretrained(
|
162 |
-
MODEL_NAME, torch_dtype=torch.float16,
|
163 |
-
trust_remote_code=True,
|
164 |
-
device_map="auto",
|
165 |
-
quantization_config=quantization_config
|
166 |
-
)
|
167 |
-
|
168 |
-
generation_config = GenerationConfig.from_pretrained(MODEL_NAME)
|
169 |
-
generation_config.max_new_tokens = 1024
|
170 |
-
generation_config.temperature = 0.0001
|
171 |
-
generation_config.top_p = 0.95
|
172 |
-
generation_config.do_sample = True
|
173 |
-
generation_config.repetition_penalty = 1.15
|
174 |
-
|
175 |
-
llm = HuggingFacePipeline(pipeline=pipeline)
|
176 |
-
embeddings = HuggingFaceEmbeddings(model_name = EMBEDDING_MODEL)
|
177 |
-
|
178 |
-
urls = [
|
179 |
-
"https://www.boe.es/diario_boe/txt.php?id=BOE-A-2024-9523"
|
180 |
-
]
|
181 |
-
|
182 |
-
loader = UnstructuredURLLoader(urls=urls)
|
183 |
-
documents = loader.load()
|
184 |
-
|
185 |
-
text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
|
186 |
-
texts_chunks = text_splitter.split_documents(documents)
|
187 |
-
|
188 |
-
db = Chroma.from_documents(texts_chunks, embeddings, persist_directory="db")
|
189 |
-
|
190 |
-
template = """Act as an lawyer assistant manager expert. Use the following information to answer the question at the end.
|
191 |
-
'You must always answer in Spanish' If you do not know the answer reply with 'I am sorry, I dont have enough information'.
|
192 |
-
Chat History
|
193 |
-
{chat_history}
|
194 |
-
Follow Up Input: {question}
|
195 |
-
Standalone question:
|
196 |
-
"""
|
197 |
-
|
198 |
-
CUSTOM_QUESTION_PROMPT = PromptTemplate.from_template(template)
|
199 |
-
|
200 |
-
memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True)
|
201 |
-
|
202 |
-
llm_chain = ConversationalRetrievalChain.from_llm(
|
203 |
-
llm=llm,
|
204 |
-
retriever=db.as_retriever(search_kwargs={"k": 2}),
|
205 |
-
memory=memory,
|
206 |
-
condense_question_prompt=CUSTOM_QUESTION_PROMPT,
|
207 |
-
)
|
208 |
-
|
209 |
-
def querying(query, history):
|
210 |
-
memory = ConversationBufferMemory(memory_key="chat_history", return_messages=False)
|
211 |
-
|
212 |
-
qa_chain = ConversationalRetrievalChain.from_llm(
|
213 |
-
llm=llm,
|
214 |
-
retriever=db.as_retriever(search_kwargs={"k": 2}),
|
215 |
-
memory=memory,
|
216 |
-
condense_question_prompt=CUSTOM_QUESTION_PROMPT,
|
217 |
-
)
|
218 |
-
|
219 |
-
result = qa_chain({"question": query})
|
220 |
-
return result["answer"].strip()
|
221 |
-
|
222 |
-
iface = gr.ChatInterface(
|
223 |
-
fn = querying,
|
224 |
-
chatbot=gr.Chatbot(height=600),
|
225 |
-
textbox=gr.Textbox(placeholder="Cuantos segmentos hay y en que consisten?", container=False, scale=7),
|
226 |
-
title="LawyerBot",
|
227 |
-
theme="soft",
|
228 |
-
examples=["¿Cuantos segmentos hay?",
|
229 |
-
"¿Que importe del bono digital corresponde a cada uno de los 5 segmentos?",
|
230 |
-
"¿Cuál es el importe de la ayuda para el segmento III en canto a dispositivo hardware?",
|
231 |
-
"Si tengo una microempresa de 2 empleado, ¿qué importe del bono digital me corresponde?",
|
232 |
-
"¿Qué nuevos segmentos de beneficiarios se han introducido?"],
|
233 |
-
cache_examples=True,
|
234 |
-
retry_btn="Repetir",
|
235 |
-
undo_btn="Deshacer",
|
236 |
-
clear_btn="Borrar",
|
237 |
-
submit_btn="Enviar"
|
238 |
-
)
|
239 |
-
|
240 |
-
iface.launch(share=True)
|
|
|
1 |
+
import os
|
2 |
import torch
|
3 |
import gradio as gr
|
4 |
from textwrap import fill
|
|
|
19 |
from transformers import BitsAndBytesConfig, AutoModelForCausalLM, AutoTokenizer, GenerationConfig, pipeline
|
20 |
import warnings
|
21 |
from huggingface_hub import login
|
|
|
22 |
|
23 |
warnings.filterwarnings('ignore')
|
24 |
|
25 |
+
# Ensure Hugging Face token is set in the environment variables
|
26 |
huggingface_token = os.getenv('huggingface_token')
|
27 |
login(huggingface_token)
|
28 |
|
|
|
118 |
submit_btn="Enviar"
|
119 |
)
|
120 |
|
121 |
+
iface.launch(share=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
requirements.txt
CHANGED
@@ -11,4 +11,5 @@ sentence-transformers
|
|
11 |
langchain-community
|
12 |
tiktoken
|
13 |
langchain_experimental
|
14 |
-
langchain_openai
|
|
|
|
11 |
langchain-community
|
12 |
tiktoken
|
13 |
langchain_experimental
|
14 |
+
langchain_openai
|
15 |
+
sentencepiece
|