File size: 4,362 Bytes
35254ca
a1bd8b6
35254ca
 
927b5de
 
1874bf4
 
 
 
927b5de
1874bf4
 
1baccf7
 
 
 
 
927b5de
1aa5b50
927b5de
1aa5b50
 
 
67c4753
1aa5b50
 
f5d0b7e
 
927b5de
1874bf4
 
e1b8424
1874bf4
 
f5d0b7e
1874bf4
d0d8a82
1874bf4
5ff99f2
ea7c9d2
1874bf4
7c96374
 
 
 
 
 
 
 
 
 
 
 
f5d0b7e
8eb345d
5ff99f2
f5d0b7e
1874bf4
 
 
 
 
1aa5b50
 
 
 
3830fb5
 
d0d8a82
0b1197e
9db20d7
 
 
1aa5b50
88d1760
927b5de
3830fb5
927b5de
 
3792262
 
 
943fb12
927b5de
 
 
7504c85
 
 
 
 
 
 
 
 
927b5de
 
 
 
 
 
 
 
 
 
 
3792262
927b5de
 
 
 
 
 
 
 
1874bf4
 
 
 
968fe0b
1874bf4
d0d8a82
1874bf4
 
edc6972
 
927b5de
1874bf4
 
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
import optimum
import transformers
from transformers import AutoConfig, AutoTokenizer, AutoModel, AutoModelForCausalLM
from optimum.bettertransformer import BetterTransformer
import torch
import gradio as gr
import json
import os
import shutil
import requests

# Define the device
device = "cuda" if torch.cuda.is_available() else "cpu"
#Define variables 
temperature=0.4
max_new_tokens=240
top_p=0.92
repetition_penalty=1.7

model_name = "OpenLLM-France/Claire-7B-0.1"

tokenizer = transformers.AutoTokenizer.from_pretrained(model_name)
model = transformers.AutoModelForCausalLM.from_pretrained(model_name,
    device_map="auto",
    torch_dtype=torch.bfloat16,
    load_in_4bit=True                          # For efficient inference, if supported by the GPU card
)
model = BetterTransformer.transform(model)


# Class to encapsulate the Falcon chatbot
class FalconChatBot:
    def __init__(self, system_prompt="Le dialogue suivant est une conversation"):
        self.system_prompt = system_prompt

    def predict(self, user_message, assistant_message, temperature=0.4, max_new_tokens=700, top_p=0.99, repetition_penalty=1.9):
        # Combine the user and assistant messages into a conversation
        conversation = f"{self.system_prompt} {assistant_message if assistant_message else ''} {user_message} "
        # Encode the conversation using the tokenizer
        input_ids = tokenizer.encode(conversation, return_tensors="pt", add_special_tokens=False)
        input_ids = input_ids.to(device)
        # Generate a response using the Falcon model
        response = model.generate(
            input_ids=input_ids, 
            use_cache=False, 
            early_stopping=False, 
            bos_token_id=model.config.bos_token_id, 
            eos_token_id=model.config.eos_token_id, 
            pad_token_id=model.config.eos_token_id, 
            temperature=temperature, 
            do_sample=True, 
            max_new_tokens=max_new_tokens, 
            top_p=top_p, 
            repetition_penalty=repetition_penalty
        )
        # Decode the generated response to text
        response_text = tokenizer.decode(response[0], skip_special_tokens=True)
        return response_text

# Create the Falcon chatbot instance
falcon_bot = FalconChatBot()

# Define the Gradio interface
title = "👋🏻Bienvenue à Tonic's 🌜🌚Claire Chat !"
description = "Vous pouvez utiliser [🌜🌚ClaireGPT](https://huggingface.co/OpenLLM-France/Claire-7B-0.1) Ou dupliquer pour l'uiliser localement ou sur huggingface! [Join me on Discord to build together](https://discord.gg/VqTxc76K3u)."
examples = [
    [
        "Le dialogue suivant est une conversation entre Emmanuel Macron et Elon Musk:",  # user_message
        "[Emmanuel Macron]: Bonjour Monsieur Musk. Je vous remercie de me recevoir aujourd'hui.",  # assistant_message
        0.9,  # temperature
        150,  # max_new_tokens
        0.90,  # top_p
        1.9,  # repetition_penalty
    ]
]

additional_inputs=[
    gr.Textbox("", label="Introduisez Un Personnage Ici ou Mettez En Scene"),
    gr.Slider(
        label="Max new tokens",
        value=100,  # Default value
        minimum=25,
        maximum=256,
        step=1,
        interactive=True,
        info="The maximum numbers of new tokens",
    ),
    gr.Slider(
        label="Temperature",
        value=0.7,  # Default value
        minimum=0.05,
        maximum=1.0,
        step=0.05,
        interactive=True,
        info="Higher values produce more diverse outputs",
    ),
    gr.Slider(
        label="Top-p (nucleus sampling)",
        value=0.90,
        minimum=0.01,
        maximum=0.99,
        step=0.05,
        interactive=True,
        info="Higher values sample more low-probability tokens",
    ),
    gr.Slider(
        label="Repetition penalty",
        value=1.9,
        minimum=1.0,
        maximum=2.0,
        step=0.05,
        interactive=True,
        info="Penalize repeated tokens",
    )
]

iface = gr.Interface(
    fn=falcon_bot.predict,
    title=title,
    description=description,
    examples=examples,
    inputs=[
        gr.Textbox(label="Utilisez se format pour initier une conversation [Personage:]", type="text", lines=5),
    ] + additional_inputs,
    outputs="text",
    theme="ParityError/Anime"
)

# Launch the Gradio interface for the Falcon model
iface.launch()