Spaces:
Sleeping
Sleeping
Elouarn
commited on
Commit
·
77610e7
1
Parent(s):
fd3c245
request test
Browse files
app.py
CHANGED
@@ -1,195 +1,208 @@
|
|
1 |
-
from huggingface_hub import InferenceClient
|
2 |
-
import gradio as gr
|
3 |
-
import re
|
4 |
|
5 |
|
6 |
-
client = InferenceClient("mistralai/Mixtral-8x7B-Instruct-v0.1")
|
7 |
|
8 |
-
# Name of the chat
|
9 |
-
chat_name = "Tavernier 🍺"
|
10 |
|
11 |
|
12 |
-
# Variable globale pour compter les itérations
|
13 |
-
global_iteration = 0
|
14 |
|
15 |
-
# Variable globale pour stocker le contexte
|
16 |
-
global_context = {
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
}
|
24 |
|
25 |
|
26 |
-
# Créer une interface avec un champ de texte pour le contexte caché
|
27 |
-
context_input = gr.Textbox(
|
28 |
-
|
29 |
-
)
|
30 |
|
31 |
|
32 |
-
def update_dynamic_context(user_input):
|
33 |
-
|
34 |
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
|
44 |
|
45 |
-
def filter_sensitive_info(text):
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
|
51 |
|
52 |
-
def remove_length_info(response_text):
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
def format_prompt(message, history, user_id=""):
|
58 |
-
max_history_length = 10 # Limiter la longueur de l'historique
|
59 |
-
history = history[-max_history_length:]
|
60 |
-
|
61 |
-
prompt = "<s>"
|
62 |
-
|
63 |
-
for user_prompt, bot_response in history:
|
64 |
-
prompt += f"[USER] {user_prompt} [/USER]"
|
65 |
-
if (
|
66 |
-
user_prompt == history[-1][0]
|
67 |
-
): # N'ajouter les balises [BOT] qu'autour de la dernière réponse du bot
|
68 |
-
prompt += f" [BOT]{filter_sensitive_info(bot_response)}[/BOT] "
|
69 |
-
else:
|
70 |
-
prompt += f" {filter_sensitive_info(bot_response)} "
|
71 |
-
prompt += f"</s>[INST][USER] {message} [/USER][/INST]"
|
72 |
-
|
73 |
-
# Ajouter le contexte statique et dynamique à chaque fois que l'utilisateur pose une question
|
74 |
-
prompt += f"[INST] {global_context['static_context']} {global_context['dynamic_context']} [/INST]"
|
75 |
-
|
76 |
-
return prompt
|
77 |
-
|
78 |
-
|
79 |
-
def generate(
|
80 |
-
prompt,
|
81 |
-
history,
|
82 |
-
user_id,
|
83 |
-
temperature=0.2,
|
84 |
-
max_new_tokens=256,
|
85 |
-
top_p=0.95,
|
86 |
-
repetition_penalty=1.0,
|
87 |
-
):
|
88 |
-
temperature = float(temperature)
|
89 |
-
if temperature < 1e-2:
|
90 |
-
temperature = 1e-2
|
91 |
-
top_p = float(top_p)
|
92 |
-
|
93 |
-
generate_kwargs = dict(
|
94 |
-
temperature=temperature,
|
95 |
-
max_new_tokens=max_new_tokens,
|
96 |
-
top_p=top_p,
|
97 |
-
repetition_penalty=repetition_penalty,
|
98 |
-
do_sample=True,
|
99 |
-
seed=42,
|
100 |
-
)
|
101 |
-
|
102 |
-
update_dynamic_context(prompt)
|
103 |
-
|
104 |
-
formatted_prompt = format_prompt(prompt, history, user_id)
|
105 |
-
global global_iteration
|
106 |
-
global_iteration += 1
|
107 |
-
print(f"\n\nIteration {global_iteration}/{user_id}: {formatted_prompt}")
|
108 |
-
|
109 |
-
stream = client.text_generation(
|
110 |
-
formatted_prompt,
|
111 |
-
**generate_kwargs,
|
112 |
-
stream=True,
|
113 |
-
details=True,
|
114 |
-
return_full_text=False,
|
115 |
-
)
|
116 |
-
output = ""
|
117 |
-
|
118 |
-
for response in stream:
|
119 |
-
response_text = filter_sensitive_info(response.token.text)
|
120 |
-
response_text = remove_length_info(response_text)
|
121 |
-
output += response_text
|
122 |
-
yield output
|
123 |
-
return output
|
124 |
-
|
125 |
-
|
126 |
-
mychatbot = gr.Chatbot(
|
127 |
-
avatar_images=["./berger.jpg", "./tavernier.jpg"],
|
128 |
-
bubble_full_width=False,
|
129 |
-
show_label=False,
|
130 |
-
)
|
131 |
-
|
132 |
-
|
133 |
-
def chatbot_interface(request):
|
134 |
-
# Récupérer l'identifiant unique de l'utilisateur à partir de la requête HTML
|
135 |
-
user_id = request.query_params.get("user_id", None)
|
136 |
-
|
137 |
-
# Créer l'interface du chatbot avec l'identifiant unique de l'utilisateur en tant qu'entrée
|
138 |
-
chatbot = gr.Chatbot(
|
139 |
-
avatar_images=["./berger.jpg", "./tavernier.jpg"],
|
140 |
-
bubble_full_width=False,
|
141 |
-
show_label=False,
|
142 |
-
)
|
143 |
-
chatbot_interface = gr.Interface(
|
144 |
-
fn=generate,
|
145 |
-
inputs=[user_id],
|
146 |
-
chatbot=chatbot,
|
147 |
-
title="Mon chatbot",
|
148 |
-
retry_btn=None,
|
149 |
-
undo_btn=None,
|
150 |
-
)
|
151 |
-
return chatbot_interface
|
152 |
-
|
153 |
-
|
154 |
-
# Créer une interface pour le chatbot avec un champ de texte pour le contexte caché
|
155 |
-
|
156 |
-
|
157 |
-
# def reset_history(chatbot):
|
158 |
-
# chatbot.history = []
|
159 |
-
# return []
|
160 |
-
|
161 |
-
|
162 |
-
# def update_context(context):
|
163 |
-
# global global_context
|
164 |
-
# global_context["static_context"] = context
|
165 |
-
# global mychatbot
|
166 |
-
# mychatbot.history = reset_history(mychatbot)
|
167 |
-
# global global_iteration
|
168 |
-
# global_iteration = 0
|
169 |
-
# return None
|
170 |
|
171 |
|
172 |
-
#
|
173 |
-
#
|
174 |
-
#
|
175 |
-
|
176 |
-
#
|
177 |
-
|
178 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
179 |
|
|
|
|
|
180 |
|
181 |
-
#
|
182 |
-
|
183 |
-
|
184 |
-
#
|
185 |
-
#
|
186 |
-
#
|
187 |
-
#
|
188 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
189 |
# )
|
190 |
|
191 |
-
# demo = gr.TabbedInterface([chatbot_interface, context_interface], title="Tavernier 🍺")
|
192 |
|
193 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
194 |
|
195 |
-
|
|
|
1 |
+
# from huggingface_hub import InferenceClient
|
2 |
+
# import gradio as gr
|
3 |
+
# import re
|
4 |
|
5 |
|
6 |
+
# client = InferenceClient("mistralai/Mixtral-8x7B-Instruct-v0.1")
|
7 |
|
8 |
+
# # Name of the chat
|
9 |
+
# chat_name = "Tavernier 🍺"
|
10 |
|
11 |
|
12 |
+
# # Variable globale pour compter les itérations
|
13 |
+
# global_iteration = 0
|
14 |
|
15 |
+
# # Variable globale pour stocker le contexte
|
16 |
+
# global_context = {
|
17 |
+
# "static_context": "Tu es le gérant d'une taverne dans le village de Montaillou au 14ème siècle dans l'univers des cathares dans les Pyrénées. Tu ne parles qu'en français. Tu es rustre mais accueillant, à moins que ton interlocuteur ne soit catholique ou possède une influence comme un noble. Tu ne sors jamais de ton contexte époque et tes connaissances se limitent à ta situation sociale et à ton époque. Tu dois être immersif et cohérent pour la personne qui va te parler. Prends toujours en compte l'historique de la conversation. Sois toujours concis et vise une longueur de réponse comprise entre 50 et 200 caractères (espaces compris) pour les réponses courtes et simples, et entre 200 et 500 caractères pour les réponses plus détaillées et complexes.",
|
18 |
+
# "dynamic_context": "",
|
19 |
+
# "sensitive_info_filter": [
|
20 |
+
# "contexte caché",
|
21 |
+
# "hidden_context",
|
22 |
+
# ],
|
23 |
+
# }
|
24 |
|
25 |
|
26 |
+
# # Créer une interface avec un champ de texte pour le contexte caché
|
27 |
+
# context_input = gr.Textbox(
|
28 |
+
# label="Contexte caché", value=global_context["static_context"]
|
29 |
+
# )
|
30 |
|
31 |
|
32 |
+
# def update_dynamic_context(user_input):
|
33 |
+
# global global_context
|
34 |
|
35 |
+
# keywords = {
|
36 |
+
# "meutre": "Un meutre a été mentionné. Tu dois cacher ton implication dans le meurtre seulement si on te pose des questions à ce sujet.",
|
37 |
+
# }
|
38 |
|
39 |
+
# for keyword in keywords:
|
40 |
+
# if keyword in user_input.lower():
|
41 |
+
# global_context["dynamic_context"] += f" {keywords[keyword]}"
|
42 |
+
# break
|
43 |
|
44 |
|
45 |
+
# def filter_sensitive_info(text):
|
46 |
+
# for word in global_context["sensitive_info_filter"]:
|
47 |
+
# if word in text.lower():
|
48 |
+
# return text.replace(word, "[REDACTED]")
|
49 |
+
# return text
|
50 |
|
51 |
|
52 |
+
# def remove_length_info(response_text):
|
53 |
+
# pattern = r"\(\d+ caractères\)"
|
54 |
+
# return re.sub(pattern, "", response_text)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
55 |
|
56 |
|
57 |
+
# def format_prompt(message, history, user_id=""):
|
58 |
+
# max_history_length = 10 # Limiter la longueur de l'historique
|
59 |
+
# history = history[-max_history_length:]
|
60 |
+
|
61 |
+
# prompt = "<s>"
|
62 |
+
|
63 |
+
# for user_prompt, bot_response in history:
|
64 |
+
# prompt += f"[USER] {user_prompt} [/USER]"
|
65 |
+
# if (
|
66 |
+
# user_prompt == history[-1][0]
|
67 |
+
# ): # N'ajouter les balises [BOT] qu'autour de la dernière réponse du bot
|
68 |
+
# prompt += f" [BOT]{filter_sensitive_info(bot_response)}[/BOT] "
|
69 |
+
# else:
|
70 |
+
# prompt += f" {filter_sensitive_info(bot_response)} "
|
71 |
+
# prompt += f"</s>[INST][USER] {message} [/USER][/INST]"
|
72 |
|
73 |
+
# # Ajouter le contexte statique et dynamique à chaque fois que l'utilisateur pose une question
|
74 |
+
# prompt += f"[INST] {global_context['static_context']} {global_context['dynamic_context']} [/INST]"
|
75 |
|
76 |
+
# return prompt
|
77 |
+
|
78 |
+
|
79 |
+
# def generate(
|
80 |
+
# prompt,
|
81 |
+
# history,
|
82 |
+
# user_id,
|
83 |
+
# temperature=0.2,
|
84 |
+
# max_new_tokens=256,
|
85 |
+
# top_p=0.95,
|
86 |
+
# repetition_penalty=1.0,
|
87 |
+
# ):
|
88 |
+
# temperature = float(temperature)
|
89 |
+
# if temperature < 1e-2:
|
90 |
+
# temperature = 1e-2
|
91 |
+
# top_p = float(top_p)
|
92 |
+
|
93 |
+
# generate_kwargs = dict(
|
94 |
+
# temperature=temperature,
|
95 |
+
# max_new_tokens=max_new_tokens,
|
96 |
+
# top_p=top_p,
|
97 |
+
# repetition_penalty=repetition_penalty,
|
98 |
+
# do_sample=True,
|
99 |
+
# seed=42,
|
100 |
+
# )
|
101 |
+
|
102 |
+
# update_dynamic_context(prompt)
|
103 |
+
|
104 |
+
# formatted_prompt = format_prompt(prompt, history, user_id)
|
105 |
+
# global global_iteration
|
106 |
+
# global_iteration += 1
|
107 |
+
# print(f"\n\nIteration {global_iteration}/{user_id}: {formatted_prompt}")
|
108 |
+
|
109 |
+
# stream = client.text_generation(
|
110 |
+
# formatted_prompt,
|
111 |
+
# **generate_kwargs,
|
112 |
+
# stream=True,
|
113 |
+
# details=True,
|
114 |
+
# return_full_text=False,
|
115 |
+
# )
|
116 |
+
# output = ""
|
117 |
+
|
118 |
+
# for response in stream:
|
119 |
+
# response_text = filter_sensitive_info(response.token.text)
|
120 |
+
# response_text = remove_length_info(response_text)
|
121 |
+
# output += response_text
|
122 |
+
# yield output
|
123 |
+
# return output
|
124 |
+
|
125 |
+
|
126 |
+
# mychatbot = gr.Chatbot(
|
127 |
+
# avatar_images=["./berger.jpg", "./tavernier.jpg"],
|
128 |
+
# bubble_full_width=False,
|
129 |
+
# show_label=False,
|
130 |
# )
|
131 |
|
|
|
132 |
|
133 |
+
# def chatbot_interface(request):
|
134 |
+
# # Récupérer l'identifiant unique de l'utilisateur à partir de la requête HTML
|
135 |
+
# user_id = request.query_params.get("user_id", None)
|
136 |
+
|
137 |
+
# # Créer l'interface du chatbot avec l'identifiant unique de l'utilisateur en tant qu'entrée
|
138 |
+
# chatbot = gr.Chatbot(
|
139 |
+
# avatar_images=["./berger.jpg", "./tavernier.jpg"],
|
140 |
+
# bubble_full_width=False,
|
141 |
+
# show_label=False,
|
142 |
+
# )
|
143 |
+
# chatbot_interface = gr.Interface(
|
144 |
+
# fn=generate,
|
145 |
+
# inputs=[user_id],
|
146 |
+
# chatbot=chatbot,
|
147 |
+
# title="Mon chatbot",
|
148 |
+
# retry_btn=None,
|
149 |
+
# undo_btn=None,
|
150 |
+
# )
|
151 |
+
# return chatbot_interface
|
152 |
+
|
153 |
+
|
154 |
+
# # Créer une interface pour le chatbot avec un champ de texte pour le contexte caché
|
155 |
+
|
156 |
+
|
157 |
+
# # def reset_history(chatbot):
|
158 |
+
# # chatbot.history = []
|
159 |
+
# # return []
|
160 |
+
|
161 |
+
|
162 |
+
# # def update_context(context):
|
163 |
+
# # global global_context
|
164 |
+
# # global_context["static_context"] = context
|
165 |
+
# # global mychatbot
|
166 |
+
# # mychatbot.history = reset_history(mychatbot)
|
167 |
+
# # global global_iteration
|
168 |
+
# # global_iteration = 0
|
169 |
+
# # return None
|
170 |
+
|
171 |
+
|
172 |
+
# # chatbot_interface = gr.ChatInterface(
|
173 |
+
# # fn=generate,
|
174 |
+
# # chatbot=mychatbot,
|
175 |
+
# # title=chat_name,
|
176 |
+
# # retry_btn=None,
|
177 |
+
# # undo_btn=None,
|
178 |
+
# # )
|
179 |
+
|
180 |
+
|
181 |
+
# # Ajouter le champ de texte pour le contexte caché à l'interface
|
182 |
+
# # context_interface = gr.Interface(
|
183 |
+
# # fn=update_context,
|
184 |
+
# # inputs=[context_input],
|
185 |
+
# # outputs=None,
|
186 |
+
# # title="Modifier le contexte caché",
|
187 |
+
# # description="Modifie le contexte caché du tavernier",
|
188 |
+
# # article=False,
|
189 |
+
# # )
|
190 |
+
|
191 |
+
# # demo = gr.TabbedInterface([chatbot_interface, context_interface], title="Tavernier 🍺")
|
192 |
+
|
193 |
+
# # Lancer et afficher l'interface
|
194 |
+
|
195 |
+
# chatbot_interface.queue().launch()
|
196 |
+
|
197 |
+
import gradio as gr
|
198 |
+
|
199 |
+
|
200 |
+
def echo(text, request: gr.Request):
|
201 |
+
if request:
|
202 |
+
print("Request headers dictionary:", request.headers)
|
203 |
+
print("IP address:", request.client.host)
|
204 |
+
print("Query parameters:", dict(request.query_params))
|
205 |
+
return text
|
206 |
+
|
207 |
|
208 |
+
io = gr.Interface(echo, "textbox", "textbox").launch()
|