Spaces:
Sleeping
Sleeping
File size: 12,839 Bytes
0c6a021 5912f7b 0c6a021 5912f7b 0c6a021 5912f7b 0c6a021 ca4336e 0c6a021 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 |
# -*- coding: utf-8 -*-
# Copyright (c) Louis Brulé Naudet. All Rights Reserved.
# This software may be used and distributed according to the terms of the License Agreement.
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import os
import logging
from threading import Thread
from typing import Iterator
from typing import (
Dict,
List,
)
import chromadb
import gradio as gr
import polars as pl
import spaces
import torch
from chromadb.config import Settings
from chromadb.utils import embedding_functions
from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
def setup_logger(name="my_logger", log_file=None, level=logging.INFO):
"""
Set up and return a logger with optional console and file logging.
This function configures a logger that outputs messages to the console and
optionally writes them to a specified log file. The logger supports different severity levels (e.g., DEBUG, INFO, WARNING, ERROR, CRITICAL) and can be customized with a unique name.
Parameters
----------
name : str, optional
The name of the logger. This allows you to identify different loggers
if multiple instances are used. Default is "my_logger".
log_file : str, optional
The path to a file where logs will be saved. If None, logs will only
be displayed in the console. Default is None.
level : int, optional
The logging level that controls the minimum severity of messages to log. Typical values are logging.DEBUG, logging.INFO, logging.WARNING,
logging.ERROR, and logging.CRITICAL. Default is logging.INFO.
Returns
-------
logging.Logger
A configured logger instance with the specified settings.
Examples
--------
>>> logger = setup_logger("example_logger", log_file="example.log", level=logging.DEBUG)
>>> logger.info("This is an info message.")
>>> logger.debug("This is a debug message.")
>>> logger.warning("This is a warning message.")
>>> logger.error("This is an error message.")
>>> logger.critical("This is a critical message.")
Notes
-----
The function adds a `StreamHandler` to output logs to the console and a
`FileHandler` if a log file is specified. Each handler uses the same logging format which includes the timestamp, logger name, severity level, and message.
The function creates a new logger with the specified name only if one with
that name does not already exist. Repeated calls with the same `name` will
retrieve the existing logger, which may lead to duplicate handlers if not
handled carefully.
See Also
--------
logging.Logger : Python's built-in logger class, providing full details on
all supported logging operations.
"""
# Create a custom logger
logger = logging.getLogger(name)
logger.setLevel(level)
# Create console handler and set level
console_handler = logging.StreamHandler()
console_handler.setLevel(level)
# Create formatter and add it to handlers
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
console_handler.setFormatter(formatter)
# Add handlers to the logger
logger.addHandler(console_handler)
# Optionally, add a file handler
if log_file:
file_handler = logging.FileHandler(log_file)
file_handler.setLevel(level)
file_handler.setFormatter(formatter)
logger.addHandler(file_handler)
return logger
logger = setup_logger("application", level=logging.DEBUG)
MAX_NEW_TOKENS: int = 2048
DESCRIPTION: str = """[<img src="https://huggingface.co/louisbrulenaudet/lemone-embed-pro/resolve/main/assets/thumbnail.webp" alt="Built with Lemone-embed Pro" width="600"/>](https://huggingface.co/louisbrulenaudet/lemone-embed-pro)
This space showcases the [Lemone-embed-pro](https://huggingface.co/louisbrulenaudet/Lemone-embed-pro)
model by [Louis Brulé Naudet](https://huggingface.co/louisbrulenaudet).
The model is tailored to meet the specific demands of information retrieval across large-scale tax-related corpora, supporting the implementation of production-ready Retrieval-Augmented Generation (RAG) applications. Its primary purpose is to enhance the efficiency and accuracy of legal processes in the taxation domain, with an emphasis on delivering consistent performance in real-world settings, while also contributing to advancements in legal natural language processing research.
"""
SYSTEME_PROMPT: str = """Vous êtes un assistant juridique spécialisé en fiscalité, conçu pour fournir des réponses précises et contextualisées en vous appuyant sur des documents de référence considérés comme d'autorité. Votre rôle consiste à extraire et formuler des réponses détaillées et nuancées, qui s'appuient sur le contenu pertinent des documents fournis, en adoptant un discours académique et professionnel.
Objectifs principaux :
1. **Analyse sémantique rigoureuse** : Évaluez le contenu sémantique des documents afin d'assurer une compréhension exhaustive de chaque passage pertinent en lien avec la question posée.
2. **Élaboration de phrases complexes** : Formulez des réponses en utilisant des structures de phrases élaborées et variées qui permettent d'étendre et d'enrichir l'expression des idées sans jamais simplifier excessivement les thématiques abordées.
3. **Qualité linguistique** : Chaque réponse doit être rédigée dans un français exempt de fautes d'orthographe, de grammaire, de syntaxe, et de ponctuation. Utilisez un style d'écriture qui témoigne de rigueur professionnelle.
4. **Neutralité et objectivité** : Maintenez une approche neutre ou nuancée dans les réponses, en mettant en avant une analyse impartiale de chaque thématique juridique ou fiscale abordée.
5. **Contextualisation juridique** : Assurez-vous que chaque réponse contextualise explicitement le sujet juridique ou fiscal en question afin de garantir une compréhension autonome de la problématique posée.
6. **Respect du style littéraire** : Utilisez un style littéraire et professionnel dans chaque réponse, et intégrez des exemples pertinents lorsque ceux-ci renforcent la clarté de l'analyse. Évitez tout usage de formulations vagues ou d'interprétations subjectives.
7. **Directivité et impersonnalité** : Formulez les réponses de manière directe, en évitant l’utilisation de pronoms personnels ou d'expressions référentielles implicites qui pourraient diminuer la clarté ou l’autorité du discours.
8. **Usage exhaustif des sources** : Exploitez intégralement le contenu des documents fournis, de sorte qu'aucun détail pertinent n'est négligé et que chaque réponse conserve un caractère hautement spécialisé. Lorsque des flux ou des exemples numériques apparaissent, intégrez-les sans altérer le contexte.
9. **Absence de citation implicite** : Ne faites jamais référence aux documents comme des "sources" ou "textes sources". Intégrez directement les informations en les reformulant dans un discours naturel et autonome, en respectant la logique de la thématique abordée."""
if not torch.cuda.is_available():
DESCRIPTION += "\n<p>Running on CPU 🥶 This demo does not work on CPU.</p>"
else:
logger.info("Loading model from Hugging Face Hub...")
model = AutoModelForCausalLM.from_pretrained(
"CohereForAI/c4ai-command-r-v01-4bit",
torch_dtype=torch.float16,
device_map="auto",
token=os.getenv("HF_TOKEN")
)
device = model.device
tokenizer = AutoTokenizer.from_pretrained(
"CohereForAI/c4ai-command-r-v01-4bit",
token=os.getenv("HF_TOKEN")
)
tokenizer.use_default_system_prompt = False
client = chromadb.PersistentClient(
path="./chroma.db",
settings=Settings(anonymized_telemetry=False)
)
sentence_transformer_ef = embedding_functions.SentenceTransformerEmbeddingFunction(
model_name="louisbrulenaudet/lemone-embed-pro",
device="cuda" if torch.cuda.is_available() else "cpu",
trust_remote_code=True
)
logger.info("Creating collection using the embeddings dataset from Hugging Face Hub...")
collection = client.get_or_create_collection(
name="tax",
embedding_function=sentence_transformer_ef
)
def trim_input_ids(
input_ids,
max_length
):
"""
Trim the input token IDs if they exceed the maximum length.
Parameters
----------
input_ids : torch.Tensor
The input token IDs.
max_length : int
The maximum length allowed.
Returns
-------
torch.Tensor
The trimmed input token IDs.
"""
if input_ids.shape[1] > max_length:
input_ids = input_ids[:, -max_length:]
print(f"Trimmed input from conversation as it was longer than {max_length} tokens.")
return input_ids
@spaces.GPU
def inference(
message: str,
chat_history: list,
) -> Iterator[str]:
"""
Generate a response to a given message within a conversation context.
This function utilizes a pre-trained language model to generate a response to a given message, considering the conversation context provided in the chat history.
Parameters
----------
message : str
The user's message for which a response is generated.
chat_history : list
A list containing tuples representing the conversation history. Each tuple should consist of two elements: the user's message and the assistant's response.
Yields
------
str
A generated response to the given message.
Notes
-----
- This function requires a GPU for efficient processing and may not work properly on CPU.
- The conversation history should be provided in the form of a list of tuples, where each tuple represents a user message followed by the assistant's response.
"""
global collection
global device
global tokenizer
global model
global logger
global MAX_NEW_TOKENS
global SYSTEME_PROMPT
conversation: List[Dict[str, str]] = []
if SYSTEME_PROMPT:
conversation.append(
{
"role": "system", "content": SYSTEME_PROMPT
}
)
for user, assistant in chat_history:
conversation.extend(
[
{
"role": "user",
"content": user
},
{
"role": "assistant",
"content": assistant
}
]
)
conversation.append(
{
"role": "user",
"content": message
}
)
documents_with_metadata: Dict[str, List[str]] = collection.query(
query_texts=[message],
n_results=5,
)
documents: List[Dict[str, str]] = []
for meta, document in zip(documents_with_metadata["metadatas"][0], documents_with_metadata["documents"][0]):
documents.append({
"title": f"""{meta["title_main"]}, {meta["id_sub"]}""",
"text": f"""Lien : {meta["url_sourcepage"]}\n\n{document}"""
})
input_ids = tokenizer.apply_chat_template(
conversation=conversation,
documents=documents,
chat_template="rag",
tokenize=True,
add_generation_prompt=True,
return_tensors="pt"
).to(device)
# input_ids = trim_input_ids(
# input_ids=input_ids,
# max_length=128000
# )
streamer = TextIteratorStreamer(
tokenizer,
timeout=10.0,
skip_prompt=True,
skip_special_tokens=True
)
generate_kwargs = dict(
{"input_ids": input_ids},
streamer=streamer,
max_new_tokens=MAX_NEW_TOKENS,
do_sample=True,
num_beams=1,
temperature=0.5,
eos_token_id=tokenizer.eos_token_id
)
t = Thread(
target=model.generate,
kwargs=generate_kwargs
)
t.start()
outputs: str = []
for text in streamer:
outputs.append(text)
yield "".join(outputs).replace("<|EOT|>","")
chatbot = gr.Chatbot(
type="messages",
show_copy_button=True
)
with gr.Blocks(theme=gr.themes.Origin()) as demo:
gr.Markdown(
value=DESCRIPTION
)
gr.DuplicateButton()
chat_interface = gr.ChatInterface(
fn=inference,
type="messages",
chatbot=chatbot,
)
if __name__ == "__main__":
demo.queue().launch() |