File size: 13,931 Bytes
fb79cf3
 
7dc7203
d7c5e54
 
 
fb79cf3
d7c5e54
0aba972
d7c5e54
582f303
d7c5e54
fb79cf3
 
 
 
 
ed36d82
 
d06aee8
 
 
 
 
 
e507847
fb79cf3
 
 
d7c5e54
 
 
 
 
fb79cf3
 
 
d7c5e54
 
 
 
 
 
 
 
 
 
 
 
 
fb79cf3
 
 
d7c5e54
fb79cf3
 
 
 
 
d7c5e54
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f7f7d8c
d7c5e54
f7f7d8c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d7c5e54
 
 
 
f7f7d8c
 
 
526fda1
f7f7d8c
526fda1
 
f7f7d8c
d7c5e54
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
46017ae
 
d7c5e54
46017ae
 
f7f7d8c
582f303
 
 
 
 
 
eb371b5
 
582f303
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ed36d82
582f303
ed36d82
582f303
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d7410d1
 
7ab03bd
582f303
7ab03bd
582f303
 
 
d7c5e54
582f303
 
 
 
 
 
 
 
 
46017ae
582f303
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ed36d82
582f303
 
 
7c18ca4
 
 
 
 
 
 
 
 
 
 
 
 
 
582f303
7c18ca4
 
 
582f303
ed36d82
7c18ca4
 
582f303
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7c18ca4
582f303
7c18ca4
582f303
7c18ca4
582f303
 
ed36d82
7c18ca4
 
 
 
 
 
 
 
 
 
582f303
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fb79cf3
 
b6072e3
 
d7c5e54
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
import gradio as gr
import spaces
from huggingface_hub import login
import accelerate
from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig, TextIteratorStreamer
import os
import torch
from typing import Optional, Iterator, Dict, Any, List
from threading import Thread
from types import NoneType
import traceback


# Initialize logging and device information
print(f"Is CUDA available: {torch.cuda.is_available()}")
print(f"CUDA device: {torch.cuda.get_device_name(torch.cuda.current_device())}")

MAX_NEW_TOKENS = 2**13
DEFAULT_MAX_NEW_TOKENS = 0.65*MAX_NEW_TOKENS
DEFAULT_SYSTEM_PROMPT = """
Tu es un expert en extraction de données dans des documents très longs et bruités. 
Tu comprends le sujet grâce à des liens sémantiques que tu peux extraire. 
Tu sers à créer des concepts hiérarchiques ainsi que des liens entre ceux-ci.
Réponds de manière claire et formelle et va droit au but dans ta tâche.
"""

class HuggingFaceLogin:
    """Handles authentication to the Hugging Face Hub using environment variables or explicit tokens."""
    def __init__(self, env_token_key: str = "HF_TOKEN"):
        """Initialize the login handler.

        Args:
            env_token_key (str): Environment variable key containing the token. Defaults to "HF_TOKEN".
        """
        self.token = os.getenv(env_token_key)
        
    def login(self, token: str = None) -> bool:
        """Authenticate with the Hugging Face Hub.

        Args:
            token (Optional[str]): Optional explicit token. If not provided, uses token from environment.

        Returns:
            bool: True if login successful, False otherwise.
        
        Raises:
            ValueError: If no token is available (neither in env nor passed explicitly).
        """

        if not self.token:
            raise ValueError("No authentication token provided. Set HF_TOKEN environment variable or pass token explicitly.")
        try:
            print("Logging in to the Hugging Face Hub...")
            login(token=self.token)
            return True
        except Exception as e:
            print(f"Login failed: {str(e)}")
            return False

model_config_4bit = BitsAndBytesConfig(
    load_in_4bit                = True,
    bnb_4bit_use_double_quant   = True,
    bnb_4bit_quant_type         = "nf4",
    bnb_4bit_compute_dtype=torch.float16
)

model_config_8bit = BitsAndBytesConfig(
    load_in_8bit=True,
    llm_int8_threshold=6.0,
    llm_int8_has_fp16_weight=False,
    bnb_8bit_compute_dtype=torch.float16
)

if torch.cuda.is_available():
    model_id = "meta-llama/Llama-3.1-8B-Instruct"
    model = AutoModelForCausalLM.from_pretrained(model_id,
                                                 quantization_config=model_config_8bit,
                                                 device_map="auto")
    tokenizer = AutoTokenizer.from_pretrained(model_id)

# Helper function to generate responses from the LLM
def generate_llm_response(
    conversation: List[Dict[str, str]],
    max_new_tokens: int,
    temperature: float,
    top_p: float,
    top_k: int,
    repetition_penalty: float
) -> str:
    """Generate a response from the LLM based on the conversation."""
    input_ids = tokenizer.apply_chat_template(
        conversation, 
        return_tensors="pt",
        add_generation_prompt=True
    )
    
    input_ids = input_ids.to(model.device)
    
    streamer = TextIteratorStreamer(
        tokenizer,
        timeout=2*60.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,
        top_p=top_p,
        top_k=top_k,
        temperature=temperature,
        num_beams=1,
        repetition_penalty=repetition_penalty,
        pad_token_id=tokenizer.eos_token_id,
    )
    
    t = Thread(
        target=model.generate,
        kwargs=generate_kwargs
    )
    t.start()
    
    # Collect the output
    accumulated_response = ""
    for text in streamer:
        accumulated_response += text
        yield accumulated_response

def append_text_knowledge(file_path: str) -> str:
    """
    Reads content from a selected file and returns it as a string.
    
    Args:
        file_path (str): Path to the selected file
        
    Returns:
        str: Content of the file or empty string if no file selected
    """
    if file_path:
        try:
            with open(file_path, "r", encoding="utf-8") as f:
                return f.read()
        except Exception as e:
            print("Error reading file: {e}")
            return ""
    return ""

knowledge_textbox = gr.Textbox(
    label="Knowledge Text",
    lines= 20,
    visible=False
)

with gr.Blocks() as demo:
    gr.Markdown("# Ontology Generation with Chain-of-Thought")
    chatbot = gr.Chatbot(type="messages")
    message_input  = gr.Textbox(
        label="message",
        placeholder="Ask about the elicitation text...",
        lines=2,
        submit_btn=True
    )
    
    with gr.Row():
        file_explorer = gr.FileExplorer(
            glob="**/*.txt",
            file_count="single",
            label="Upload file",
            show_label=True
        )
        knowledge_input = gr.Textbox(
            label="Knowledge text",
            lines=6,
            visible=True
        )
    with gr.Accordion("Advanced Settings", open=False):
        system_prompt_input = gr.Textbox(
            label="System Prompt",
            lines=4,
            value=DEFAULT_SYSTEM_PROMPT
        )
        with gr.Row():
            with gr.Column():
                max_tokens_slider = gr.Slider(
                    label="Max new tokens",
                    minimum=1,
                    maximum=MAX_NEW_TOKENS,
                    step=1,
                    value=DEFAULT_MAX_NEW_TOKENS
                )
                temperature_slider = gr.Slider(
                    label="Temperature",
                    minimum=0.1,
                    maximum=4.0,
                    step=0.1,
                    value=0.2
                )
            
            with gr.Column():
                top_p_slider = gr.Slider(
                    label="Top-p (nucleus sampling)",
                    minimum=0.05,
                    maximum=1.0,
                    step=0.05,
                    value=0.8
                )
                top_k_slider = gr.Slider(
                    label="Top-k",
                    minimum=1,
                    maximum=1000,
                    step=1,
                    value=50
                )
                repetition_penalty_slider = gr.Slider(
                    label="Repetition penalty",
                    minimum=1.0,
                    maximum=2.0,
                    step=0.05,
                    value=1.0
                )

    # Example prompts
    examples = gr.Examples(
        examples=[
            ["Extract meaningful entities in your knowledge document in order to create a Turtle-formatted output where you create classes and sub-classes and object properties automatically."],
            ["Make a simple list of the classes, sub-classes and object properties that can be extracted from the knowledge document."]
        ],
        inputs=message_input
    )
    
    def user_message(message:str, history:List[Dict[str, str]]):
        """Add user message to chat history.

        Args:
            message (str): The User Message to send
            history (List[Dict[str,str]]): The previous chat conversation history.
        """
        if message.strip() == "":
            return history, message
    
        history = history + [{"role":"user", "content": message}]
        return history, ""

    def bot_response(history, knowledge, system_prompt, max_tokens, temp, top_p, top_k, rep_penalty):
        """Generate assistant response with visible thinking.

        Args:
            history (List[Dict[str, str]]): The previous chat conversation history
            knowledge (Any): Documents to pass as knowledge to the multimodal model
            system_prompt (str): System prompt that the model follows
            max_tokens (int): Max number of allowed output tokens
            temp (float): Model's Temperature
            top_p (int): Model's Top p value
            top_k (int): Model's Top k value
            rep_penalty (float): Model's repetition penalty
            
        Returns:
            history (List[Dict[str, str]]): The history of the conversation updated
        """
        try:
            if not history or history[-1]["role"] != "user":
                return history
            
            user_message = history[-1]["content"]
            # thinking message with pending status
            history.append({
                "role": "assistant",
                "content": "Je réfléchis étape par étape...",
                "metadata": {
                    "title": "Réflexion",
                    "status": "pending"
                }
            })
            yield history
            
            thinking_conversation = []
            if system_prompt:
                thinking_conversation.append({"role": "system", "content": system_prompt})
            if knowledge:
                thinking_conversation.append({
                    "role": "assistant",
                    "content": f"Voici le document que je dois comprendre: {knowledge}\n\nJe vais l'analyser étape par étape."
                })
                
            for msg in history[:-2]: # All msg except user message and thinking part
                thinking_conversation.append(msg)
            
            thinking_prompt = user_message + "\n\nRéfléchis étape par étape. D'abord identifie l'intention de l'utilisateur. Quand tu as compris ce qui t'est demandé, commence à établir un plan clair et précis que tu peux suivre. Utilise l'italic et le gras en Markdown pour séquencer et prioriser tes actions."
            thinking_conversation.append({"role": "user", "content": thinking_prompt})
            
            # GENERATE THINKING
            for thinking_partial in generate_llm_response(thinking_conversation,
                                                          max_new_tokens=max_tokens * 2,
                                                          temperature=temp,
                                                          top_p=top_p,
                                                          top_k=top_k,
                                                          repetition_penalty=rep_penalty):
                # update the thinking message
                history[-1] = {
                    "role": "assistant",
                    "content": thinking_partial,
                    "metadata": {
                        "title": "Réflexion",
                        "status": "done"
                    }
                }
                yield history
            
            history[-1]["metadata"]["status"] = "done"
            yield history
            
            print("DEBUG:\t\tYielded history of ```thinking_result```")

            final_conversation = []
            if system_prompt:
                    final_conversation.append({"role": "system", "content": system_prompt})
            if knowledge:
                final_conversation.append({
                    "role": "assistant",
                    "content": f"J'ai analysé ce document: {knowledge}"
                })
            
            for msg in history[:-1]: # exclude thinking
                if "metadata" not in msg or "title" not in msg.get("metadata", {}):
                    final_conversation.append(msg)

            final_conversation.append({
                "role": "assistant",
                "content": f"Voici mon analyse étape par étape:\n{history[-1]['content']}\n\nMaintenant je vais formaliser le résultat final."
            })
            final_conversation.append({
                "role": "assistant",
                "content": "Je formule ma réponse finale..."
            })
            yield history
            
            for final_partial in generate_llm_response(final_conversation,
                                                       max_new_tokens=max_tokens,
                                                       temperature=temp * 0.8,  # Lower temperature for final answer
                                                       top_p=top_p,
                                                       top_k=top_k,
                                                       repetition_penalty=rep_penalty):
                history[-1]["content"] = final_partial
                yield history
            print("DEBUG:\t\tYielded history of ```final_answer```")

        except Exception as e:
            error_traceback = traceback.format_exc()
            print(f"Error traceback:\n{error_traceback}")
            
            history.append({
                "role": "assistant",
                "content": f"An error occurred: {str(e)}\n\nTraceback details:\n{error_traceback}"
            })
            yield history
    
    file_explorer.change(
        append_text_knowledge,
        file_explorer,
        knowledge_input
    )
    
    message_input.submit(
        user_message,
        inputs=[message_input, chatbot],
        outputs=[chatbot, message_input]
    ).then(
        bot_response,
        inputs=[
            chatbot,
            knowledge_input,
            system_prompt_input,
            max_tokens_slider,
            temperature_slider,
            top_p_slider,
            top_k_slider,
            repetition_penalty_slider
        ],
        outputs=chatbot
    )
if __name__ == "__main__":
    auth = HuggingFaceLogin()
    if auth.login():
        print("Login successful!")
    demo.queue().launch()