Spaces:
Sleeping
Sleeping
Elouarn
commited on
Commit
·
e297a34
1
Parent(s):
a21a927
request html
Browse files
app.py
CHANGED
@@ -54,7 +54,7 @@ def remove_length_info(response_text):
|
|
54 |
return re.sub(pattern, "", response_text)
|
55 |
|
56 |
|
57 |
-
def format_prompt(message, history):
|
58 |
max_history_length = 10 # Limiter la longueur de l'historique
|
59 |
history = history[-max_history_length:]
|
60 |
|
@@ -79,6 +79,7 @@ def format_prompt(message, history):
|
|
79 |
def generate(
|
80 |
prompt,
|
81 |
history,
|
|
|
82 |
temperature=0.2,
|
83 |
max_new_tokens=256,
|
84 |
top_p=0.95,
|
@@ -100,10 +101,10 @@ def generate(
|
|
100 |
|
101 |
update_dynamic_context(prompt)
|
102 |
|
103 |
-
formatted_prompt = format_prompt(prompt, history)
|
104 |
global global_iteration
|
105 |
global_iteration += 1
|
106 |
-
print(f"\n\nIteration {global_iteration}: {formatted_prompt}")
|
107 |
|
108 |
stream = client.text_generation(
|
109 |
formatted_prompt,
|
@@ -129,44 +130,66 @@ mychatbot = gr.Chatbot(
|
|
129 |
)
|
130 |
|
131 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
132 |
# Créer une interface pour le chatbot avec un champ de texte pour le contexte caché
|
133 |
|
134 |
|
135 |
-
def reset_history(chatbot):
|
136 |
-
|
137 |
-
|
138 |
|
139 |
|
140 |
-
def update_context(context):
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
|
149 |
|
150 |
-
chatbot_interface = gr.ChatInterface(
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
)
|
157 |
|
158 |
|
159 |
# Ajouter le champ de texte pour le contexte caché à l'interface
|
160 |
-
context_interface = gr.Interface(
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
)
|
168 |
|
169 |
# demo = gr.TabbedInterface([chatbot_interface, context_interface], title="Tavernier 🍺")
|
170 |
|
171 |
-
#
|
172 |
-
|
|
|
|
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 |
|
|
|
79 |
def generate(
|
80 |
prompt,
|
81 |
history,
|
82 |
+
user_id,
|
83 |
temperature=0.2,
|
84 |
max_new_tokens=256,
|
85 |
top_p=0.95,
|
|
|
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,
|
|
|
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 |
+
if __name__ == "__main__":
|
195 |
+
chatbot_interface.queue().launch()
|