Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -13,7 +13,7 @@ client = OpenAI(
|
|
| 13 |
base_url="https://api.together.xyz/v1",
|
| 14 |
)
|
| 15 |
|
| 16 |
-
|
| 17 |
current_conversation_id = None
|
| 18 |
|
| 19 |
|
|
@@ -25,7 +25,7 @@ def create_new_conversation():
|
|
| 25 |
global current_conversation_id
|
| 26 |
current_conversation_id = str(uuid.uuid4())
|
| 27 |
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M")
|
| 28 |
-
|
| 29 |
"title": "Nouvelle conversation",
|
| 30 |
"timestamp": timestamp,
|
| 31 |
"messages": [],
|
|
@@ -57,40 +57,37 @@ def chat(message, history, system_prompt):
|
|
| 57 |
yield f"❌ Erreur: {str(e)}"
|
| 58 |
|
| 59 |
|
| 60 |
-
def get_questions_list(history):
|
| 61 |
-
return [entry["content"] for entry in history if entry["role"] == "user"]
|
| 62 |
-
|
| 63 |
-
|
| 64 |
def respond(message, history, system_prompt):
|
| 65 |
global current_conversation_id
|
| 66 |
|
| 67 |
if not current_conversation_id:
|
| 68 |
current_conversation_id = create_new_conversation()
|
| 69 |
|
| 70 |
-
if not
|
| 71 |
-
|
| 72 |
|
| 73 |
history.append({"role": "user", "content": message})
|
| 74 |
history.append({"role": "assistant", "content": ""})
|
| 75 |
|
| 76 |
-
questions = get_questions_list(history)
|
| 77 |
-
|
| 78 |
for partial_response in chat(message, history[:-2], system_prompt):
|
| 79 |
history[-1]["content"] = partial_response
|
| 80 |
-
yield "", history
|
| 81 |
|
| 82 |
-
|
| 83 |
-
|
| 84 |
|
| 85 |
|
| 86 |
-
def
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
|
|
|
|
|
|
|
|
|
| 94 |
|
| 95 |
|
| 96 |
def new_conversation():
|
|
@@ -98,71 +95,32 @@ def new_conversation():
|
|
| 98 |
return (
|
| 99 |
[],
|
| 100 |
"Tu es un assistant IA utile, honnête et inoffensif. Réponds en français de manière claire et concise.",
|
| 101 |
-
|
| 102 |
)
|
| 103 |
|
| 104 |
|
| 105 |
def delete_conv():
|
| 106 |
global current_conversation_id
|
| 107 |
-
if current_conversation_id and current_conversation_id in
|
| 108 |
-
del
|
| 109 |
current_conversation_id = None
|
| 110 |
-
return [], "Tu es un assistant IA utile.",
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
SCROLL_JS = """
|
| 114 |
-
(idx) => {
|
| 115 |
-
setTimeout(() => {
|
| 116 |
-
const chatbot = document.querySelector('#main_chatbot');
|
| 117 |
-
if (!chatbot) return;
|
| 118 |
-
|
| 119 |
-
// Cherche tous les blocs de messages dans le chatbot
|
| 120 |
-
const allMessages = chatbot.querySelectorAll(
|
| 121 |
-
'.message.user, [data-testid="user"], .user.message-bubble-border, .bubble-wrap.user'
|
| 122 |
-
);
|
| 123 |
-
|
| 124 |
-
if (allMessages[idx]) {
|
| 125 |
-
allMessages[idx].scrollIntoView({ behavior: 'smooth', block: 'center' });
|
| 126 |
-
allMessages[idx].style.outline = '2px solid #4f46e5';
|
| 127 |
-
setTimeout(() => { allMessages[idx].style.outline = ''; }, 1500);
|
| 128 |
-
return;
|
| 129 |
-
}
|
| 130 |
-
|
| 131 |
-
// Fallback : tous les éléments de la liste de messages, alterner user/bot
|
| 132 |
-
const rows = chatbot.querySelectorAll('.message-wrap, .block');
|
| 133 |
-
let userCount = 0;
|
| 134 |
-
for (const row of rows) {
|
| 135 |
-
const isUser = row.classList.contains('user') ||
|
| 136 |
-
row.querySelector('.user') !== null;
|
| 137 |
-
if (isUser) {
|
| 138 |
-
if (userCount === idx) {
|
| 139 |
-
row.scrollIntoView({ behavior: 'smooth', block: 'center' });
|
| 140 |
-
row.style.outline = '2px solid #4f46e5';
|
| 141 |
-
setTimeout(() => { row.style.outline = ''; }, 1500);
|
| 142 |
-
return;
|
| 143 |
-
}
|
| 144 |
-
userCount++;
|
| 145 |
-
}
|
| 146 |
-
}
|
| 147 |
-
}, 150);
|
| 148 |
-
}
|
| 149 |
-
"""
|
| 150 |
|
| 151 |
with gr.Blocks(title="Chatbot Mentorat") as demo:
|
| 152 |
gr.Markdown("# 🤖 Chatbot Mentorat")
|
| 153 |
gr.Markdown("Votre assistant IA personnel avec historique et personnalisation")
|
| 154 |
|
| 155 |
-
scroll_state = gr.State(value=0)
|
| 156 |
-
|
| 157 |
with gr.Row():
|
| 158 |
with gr.Column(scale=1, min_width=280):
|
| 159 |
-
gr.Markdown("### 📚
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
|
|
|
| 166 |
)
|
| 167 |
|
| 168 |
with gr.Row():
|
|
@@ -191,7 +149,6 @@ with gr.Blocks(title="Chatbot Mentorat") as demo:
|
|
| 191 |
height=600,
|
| 192 |
show_label=False,
|
| 193 |
avatar_images=("👤", "🤖"),
|
| 194 |
-
elem_id="main_chatbot"
|
| 195 |
)
|
| 196 |
|
| 197 |
with gr.Row():
|
|
@@ -204,30 +161,20 @@ with gr.Blocks(title="Chatbot Mentorat") as demo:
|
|
| 204 |
|
| 205 |
gr.Markdown("*Streaming en temps réel activé - Réponses du modèle Llama 3.3 70B*")
|
| 206 |
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
inputs=[history_radio, chatbot],
|
| 210 |
-
outputs=[scroll_state]
|
| 211 |
-
).then(
|
| 212 |
-
fn=None,
|
| 213 |
-
inputs=[scroll_state],
|
| 214 |
-
js=SCROLL_JS
|
| 215 |
-
)
|
| 216 |
-
|
| 217 |
-
btn_new.click(new_conversation, outputs=[chatbot, system_prompt, history_radio])
|
| 218 |
-
btn_delete.click(delete_conv, outputs=[chatbot, system_prompt, history_radio])
|
| 219 |
|
| 220 |
msg.submit(
|
| 221 |
-
respond, [msg, chatbot, system_prompt], [msg, chatbot
|
| 222 |
-
)
|
| 223 |
|
| 224 |
btn_send.click(
|
| 225 |
-
respond, [msg, chatbot, system_prompt], [msg, chatbot
|
| 226 |
-
)
|
| 227 |
|
| 228 |
demo.load(
|
| 229 |
-
lambda:
|
| 230 |
-
outputs=[
|
| 231 |
)
|
| 232 |
|
| 233 |
|
|
|
|
| 13 |
base_url="https://api.together.xyz/v1",
|
| 14 |
)
|
| 15 |
|
| 16 |
+
conversation_history = {}
|
| 17 |
current_conversation_id = None
|
| 18 |
|
| 19 |
|
|
|
|
| 25 |
global current_conversation_id
|
| 26 |
current_conversation_id = str(uuid.uuid4())
|
| 27 |
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M")
|
| 28 |
+
conversation_history[current_conversation_id] = {
|
| 29 |
"title": "Nouvelle conversation",
|
| 30 |
"timestamp": timestamp,
|
| 31 |
"messages": [],
|
|
|
|
| 57 |
yield f"❌ Erreur: {str(e)}"
|
| 58 |
|
| 59 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
def respond(message, history, system_prompt):
|
| 61 |
global current_conversation_id
|
| 62 |
|
| 63 |
if not current_conversation_id:
|
| 64 |
current_conversation_id = create_new_conversation()
|
| 65 |
|
| 66 |
+
if not conversation_history[current_conversation_id]["messages"]:
|
| 67 |
+
conversation_history[current_conversation_id]["title"] = generate_conversation_title(message)
|
| 68 |
|
| 69 |
history.append({"role": "user", "content": message})
|
| 70 |
history.append({"role": "assistant", "content": ""})
|
| 71 |
|
|
|
|
|
|
|
| 72 |
for partial_response in chat(message, history[:-2], system_prompt):
|
| 73 |
history[-1]["content"] = partial_response
|
| 74 |
+
yield "", history
|
| 75 |
|
| 76 |
+
conversation_history[current_conversation_id]["messages"] = history
|
| 77 |
+
conversation_history[current_conversation_id]["system_prompt"] = system_prompt
|
| 78 |
|
| 79 |
|
| 80 |
+
def get_conversation_list():
|
| 81 |
+
if not conversation_history:
|
| 82 |
+
return "Aucune conversation"
|
| 83 |
+
items = []
|
| 84 |
+
for conv_id, conv_data in sorted(
|
| 85 |
+
conversation_history.items(),
|
| 86 |
+
key=lambda x: x[1]["timestamp"],
|
| 87 |
+
reverse=True
|
| 88 |
+
):
|
| 89 |
+
items.append(f"📌 {conv_data['title']}\n{conv_data['timestamp']}")
|
| 90 |
+
return "\n\n".join(items)
|
| 91 |
|
| 92 |
|
| 93 |
def new_conversation():
|
|
|
|
| 95 |
return (
|
| 96 |
[],
|
| 97 |
"Tu es un assistant IA utile, honnête et inoffensif. Réponds en français de manière claire et concise.",
|
| 98 |
+
get_conversation_list()
|
| 99 |
)
|
| 100 |
|
| 101 |
|
| 102 |
def delete_conv():
|
| 103 |
global current_conversation_id
|
| 104 |
+
if current_conversation_id and current_conversation_id in conversation_history:
|
| 105 |
+
del conversation_history[current_conversation_id]
|
| 106 |
current_conversation_id = None
|
| 107 |
+
return [], "Tu es un assistant IA utile.", get_conversation_list()
|
| 108 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
|
| 110 |
with gr.Blocks(title="Chatbot Mentorat") as demo:
|
| 111 |
gr.Markdown("# 🤖 Chatbot Mentorat")
|
| 112 |
gr.Markdown("Votre assistant IA personnel avec historique et personnalisation")
|
| 113 |
|
|
|
|
|
|
|
| 114 |
with gr.Row():
|
| 115 |
with gr.Column(scale=1, min_width=280):
|
| 116 |
+
gr.Markdown("### 📚 Historique")
|
| 117 |
+
|
| 118 |
+
history_display = gr.Textbox(
|
| 119 |
+
label="Conversations",
|
| 120 |
+
interactive=False,
|
| 121 |
+
lines=15,
|
| 122 |
+
max_lines=20,
|
| 123 |
+
placeholder="Aucune conversation"
|
| 124 |
)
|
| 125 |
|
| 126 |
with gr.Row():
|
|
|
|
| 149 |
height=600,
|
| 150 |
show_label=False,
|
| 151 |
avatar_images=("👤", "🤖"),
|
|
|
|
| 152 |
)
|
| 153 |
|
| 154 |
with gr.Row():
|
|
|
|
| 161 |
|
| 162 |
gr.Markdown("*Streaming en temps réel activé - Réponses du modèle Llama 3.3 70B*")
|
| 163 |
|
| 164 |
+
btn_new.click(new_conversation, outputs=[chatbot, system_prompt, history_display])
|
| 165 |
+
btn_delete.click(delete_conv, outputs=[chatbot, system_prompt, history_display])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 166 |
|
| 167 |
msg.submit(
|
| 168 |
+
respond, [msg, chatbot, system_prompt], [msg, chatbot]
|
| 169 |
+
).then(lambda: get_conversation_list(), outputs=[history_display])
|
| 170 |
|
| 171 |
btn_send.click(
|
| 172 |
+
respond, [msg, chatbot, system_prompt], [msg, chatbot]
|
| 173 |
+
).then(lambda: get_conversation_list(), outputs=[history_display])
|
| 174 |
|
| 175 |
demo.load(
|
| 176 |
+
lambda: get_conversation_list(),
|
| 177 |
+
outputs=[history_display]
|
| 178 |
)
|
| 179 |
|
| 180 |
|