KHAOULA-KH commited on
Commit
342f878
·
verified ·
1 Parent(s): 5566a54

Upload Untitled3.ipynb

Browse files
Files changed (1) hide show
  1. Untitled3.ipynb +282 -0
Untitled3.ipynb ADDED
@@ -0,0 +1,282 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "nbformat": 4,
3
+ "nbformat_minor": 0,
4
+ "metadata": {
5
+ "colab": {
6
+ "provenance": []
7
+ },
8
+ "kernelspec": {
9
+ "name": "python3",
10
+ "display_name": "Python 3"
11
+ },
12
+ "language_info": {
13
+ "name": "python"
14
+ }
15
+ },
16
+ "cells": [
17
+ {
18
+ "cell_type": "code",
19
+ "source": [
20
+ "!pip install streamlit pyngrok\n"
21
+ ],
22
+ "metadata": {
23
+ "colab": {
24
+ "base_uri": "https://localhost:8080/"
25
+ },
26
+ "id": "ZHJdze1JNe-2",
27
+ "outputId": "587a9cb7-bd3f-4779-bafe-3742a0ac0e77"
28
+ },
29
+ "execution_count": 2,
30
+ "outputs": [
31
+ {
32
+ "output_type": "stream",
33
+ "name": "stdout",
34
+ "text": [
35
+ "Requirement already satisfied: streamlit in /usr/local/lib/python3.10/dist-packages (1.37.1)\n",
36
+ "Requirement already satisfied: pyngrok in /usr/local/lib/python3.10/dist-packages (7.2.0)\n",
37
+ "Requirement already satisfied: altair<6,>=4.0 in /usr/local/lib/python3.10/dist-packages (from streamlit) (4.2.2)\n",
38
+ "Requirement already satisfied: blinker<2,>=1.0.0 in /usr/lib/python3/dist-packages (from streamlit) (1.4)\n",
39
+ "Requirement already satisfied: cachetools<6,>=4.0 in /usr/local/lib/python3.10/dist-packages (from streamlit) (5.5.0)\n",
40
+ "Requirement already satisfied: click<9,>=7.0 in /usr/local/lib/python3.10/dist-packages (from streamlit) (8.1.7)\n",
41
+ "Requirement already satisfied: numpy<3,>=1.20 in /usr/local/lib/python3.10/dist-packages (from streamlit) (1.26.4)\n",
42
+ "Requirement already satisfied: packaging<25,>=20 in /usr/local/lib/python3.10/dist-packages (from streamlit) (24.1)\n",
43
+ "Requirement already satisfied: pandas<3,>=1.3.0 in /usr/local/lib/python3.10/dist-packages (from streamlit) (2.1.4)\n",
44
+ "Requirement already satisfied: pillow<11,>=7.1.0 in /usr/local/lib/python3.10/dist-packages (from streamlit) (9.4.0)\n",
45
+ "Requirement already satisfied: protobuf<6,>=3.20 in /usr/local/lib/python3.10/dist-packages (from streamlit) (3.20.3)\n",
46
+ "Requirement already satisfied: pyarrow>=7.0 in /usr/local/lib/python3.10/dist-packages (from streamlit) (14.0.2)\n",
47
+ "Requirement already satisfied: requests<3,>=2.27 in /usr/local/lib/python3.10/dist-packages (from streamlit) (2.32.3)\n",
48
+ "Requirement already satisfied: rich<14,>=10.14.0 in /usr/local/lib/python3.10/dist-packages (from streamlit) (13.7.1)\n",
49
+ "Requirement already satisfied: tenacity<9,>=8.1.0 in /usr/local/lib/python3.10/dist-packages (from streamlit) (8.5.0)\n",
50
+ "Requirement already satisfied: toml<2,>=0.10.1 in /usr/local/lib/python3.10/dist-packages (from streamlit) (0.10.2)\n",
51
+ "Requirement already satisfied: typing-extensions<5,>=4.3.0 in /usr/local/lib/python3.10/dist-packages (from streamlit) (4.12.2)\n",
52
+ "Requirement already satisfied: gitpython!=3.1.19,<4,>=3.0.7 in /usr/local/lib/python3.10/dist-packages (from streamlit) (3.1.43)\n",
53
+ "Requirement already satisfied: pydeck<1,>=0.8.0b4 in /usr/local/lib/python3.10/dist-packages (from streamlit) (0.9.1)\n",
54
+ "Requirement already satisfied: tornado<7,>=6.0.3 in /usr/local/lib/python3.10/dist-packages (from streamlit) (6.3.3)\n",
55
+ "Requirement already satisfied: watchdog<5,>=2.1.5 in /usr/local/lib/python3.10/dist-packages (from streamlit) (4.0.2)\n",
56
+ "Requirement already satisfied: PyYAML>=5.1 in /usr/local/lib/python3.10/dist-packages (from pyngrok) (6.0.2)\n",
57
+ "Requirement already satisfied: entrypoints in /usr/local/lib/python3.10/dist-packages (from altair<6,>=4.0->streamlit) (0.4)\n",
58
+ "Requirement already satisfied: jinja2 in /usr/local/lib/python3.10/dist-packages (from altair<6,>=4.0->streamlit) (3.1.4)\n",
59
+ "Requirement already satisfied: jsonschema>=3.0 in /usr/local/lib/python3.10/dist-packages (from altair<6,>=4.0->streamlit) (4.23.0)\n",
60
+ "Requirement already satisfied: toolz in /usr/local/lib/python3.10/dist-packages (from altair<6,>=4.0->streamlit) (0.12.1)\n",
61
+ "Requirement already satisfied: gitdb<5,>=4.0.1 in /usr/local/lib/python3.10/dist-packages (from gitpython!=3.1.19,<4,>=3.0.7->streamlit) (4.0.11)\n",
62
+ "Requirement already satisfied: python-dateutil>=2.8.2 in /usr/local/lib/python3.10/dist-packages (from pandas<3,>=1.3.0->streamlit) (2.8.2)\n",
63
+ "Requirement already satisfied: pytz>=2020.1 in /usr/local/lib/python3.10/dist-packages (from pandas<3,>=1.3.0->streamlit) (2024.1)\n",
64
+ "Requirement already satisfied: tzdata>=2022.1 in /usr/local/lib/python3.10/dist-packages (from pandas<3,>=1.3.0->streamlit) (2024.1)\n",
65
+ "Requirement already satisfied: charset-normalizer<4,>=2 in /usr/local/lib/python3.10/dist-packages (from requests<3,>=2.27->streamlit) (3.3.2)\n",
66
+ "Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.10/dist-packages (from requests<3,>=2.27->streamlit) (3.7)\n",
67
+ "Requirement already satisfied: urllib3<3,>=1.21.1 in /usr/local/lib/python3.10/dist-packages (from requests<3,>=2.27->streamlit) (2.0.7)\n",
68
+ "Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.10/dist-packages (from requests<3,>=2.27->streamlit) (2024.7.4)\n",
69
+ "Requirement already satisfied: markdown-it-py>=2.2.0 in /usr/local/lib/python3.10/dist-packages (from rich<14,>=10.14.0->streamlit) (3.0.0)\n",
70
+ "Requirement already satisfied: pygments<3.0.0,>=2.13.0 in /usr/local/lib/python3.10/dist-packages (from rich<14,>=10.14.0->streamlit) (2.16.1)\n",
71
+ "Requirement already satisfied: smmap<6,>=3.0.1 in /usr/local/lib/python3.10/dist-packages (from gitdb<5,>=4.0.1->gitpython!=3.1.19,<4,>=3.0.7->streamlit) (5.0.1)\n",
72
+ "Requirement already satisfied: MarkupSafe>=2.0 in /usr/local/lib/python3.10/dist-packages (from jinja2->altair<6,>=4.0->streamlit) (2.1.5)\n",
73
+ "Requirement already satisfied: attrs>=22.2.0 in /usr/local/lib/python3.10/dist-packages (from jsonschema>=3.0->altair<6,>=4.0->streamlit) (24.2.0)\n",
74
+ "Requirement already satisfied: jsonschema-specifications>=2023.03.6 in /usr/local/lib/python3.10/dist-packages (from jsonschema>=3.0->altair<6,>=4.0->streamlit) (2023.12.1)\n",
75
+ "Requirement already satisfied: referencing>=0.28.4 in /usr/local/lib/python3.10/dist-packages (from jsonschema>=3.0->altair<6,>=4.0->streamlit) (0.35.1)\n",
76
+ "Requirement already satisfied: rpds-py>=0.7.1 in /usr/local/lib/python3.10/dist-packages (from jsonschema>=3.0->altair<6,>=4.0->streamlit) (0.20.0)\n",
77
+ "Requirement already satisfied: mdurl~=0.1 in /usr/local/lib/python3.10/dist-packages (from markdown-it-py>=2.2.0->rich<14,>=10.14.0->streamlit) (0.1.2)\n",
78
+ "Requirement already satisfied: six>=1.5 in /usr/local/lib/python3.10/dist-packages (from python-dateutil>=2.8.2->pandas<3,>=1.3.0->streamlit) (1.16.0)\n"
79
+ ]
80
+ }
81
+ ]
82
+ },
83
+ {
84
+ "cell_type": "code",
85
+ "source": [
86
+ "%%writefile app.py\n",
87
+ "import streamlit as st\n",
88
+ "from gradio_client import Client\n",
89
+ "\n",
90
+ "# Configuration de la page\n",
91
+ "st.set_page_config(page_title=\"🎓🤖 Interface Chatbot - Activités Hassan II et ses chercheurs\", layout=\"centered\")\n",
92
+ "\n",
93
+ "# Custom CSS for styling\n",
94
+ "st.markdown(\"\"\"\n",
95
+ " <style>\n",
96
+ " body {\n",
97
+ " font-family: 'Arial', sans-serif;\n",
98
+ " }\n",
99
+ " .frame {\n",
100
+ " border: 2px solid #ccc;\n",
101
+ " border-radius: 12px;\n",
102
+ " padding: 20px;\n",
103
+ " margin: 15px 0;\n",
104
+ " background-color: #f1f5f9;\n",
105
+ " box-shadow: 0 4px 8px rgba(0,0,0,0.1);\n",
106
+ " }\n",
107
+ " .khaoula-label {\n",
108
+ " font-weight: bold;\n",
109
+ " color: #1E90FF;\n",
110
+ " font-size: 20px;\n",
111
+ " margin-bottom: 8px;\n",
112
+ " }\n",
113
+ " .chatbot-label {\n",
114
+ " font-weight: bold;\n",
115
+ " color: #FF4500;\n",
116
+ " font-size: 20px;\n",
117
+ " margin-bottom: 8px;\n",
118
+ " }\n",
119
+ " .message {\n",
120
+ " font-size: 16px;\n",
121
+ " line-height: 1.6;\n",
122
+ " }\n",
123
+ " .send-button {\n",
124
+ " background-color: #1E90FF;\n",
125
+ " color: white;\n",
126
+ " border-radius: 8px;\n",
127
+ " padding: 10px 20px;\n",
128
+ " border: none;\n",
129
+ " cursor: pointer;\n",
130
+ " font-size: 16px;\n",
131
+ " }\n",
132
+ " .send-button:hover {\n",
133
+ " background-color: #1C86EE;\n",
134
+ " }\n",
135
+ " </style>\n",
136
+ " \"\"\", unsafe_allow_html=True)\n",
137
+ "\n",
138
+ "# Titre et description\n",
139
+ "st.title(\"🎓🤖 Interface Chatbot - Activités Hassan II et ses chercheurs\")\n",
140
+ "st.write(\"💬 Entrez votre question ou message et obtenez une réponse 🧠\")\n",
141
+ "\n",
142
+ "# Initialize Gradio Client\n",
143
+ "client = Client(\"yousef4/mistral-7b-v0.3-chat\")\n",
144
+ "\n",
145
+ "# Session state to keep track of conversation\n",
146
+ "if 'conversation' not in st.session_state:\n",
147
+ " st.session_state.conversation = []\n",
148
+ "\n",
149
+ "# Display previous messages\n",
150
+ "for message in st.session_state.conversation:\n",
151
+ " st.markdown(message, unsafe_allow_html=True)\n",
152
+ "\n",
153
+ "# User input and button\n",
154
+ "user_input = st.text_input(\"🗣️ Khaoula :\")\n",
155
+ "\n",
156
+ "if st.button(\"Envoyer 🚀\", key=\"send_button\", help=\"Cliquez pour envoyer votre message\"):\n",
157
+ " if user_input.strip():\n",
158
+ " try:\n",
159
+ " response = client.predict(user_input)\n",
160
+ " user_message = f\"<div class='frame'><p class='khaoula-label'>🗣️ Khaoula :</p><p class='message'>{user_input}</p></div>\"\n",
161
+ " bot_message = f\"<div class='frame'><p class='chatbot-label'>🤖 Chatbot :</p><p class='message'>{response} 📚</p></div>\"\n",
162
+ "\n",
163
+ " # Update conversation\n",
164
+ " st.session_state.conversation.append(user_message)\n",
165
+ " st.session_state.conversation.append(bot_message)\n",
166
+ "\n",
167
+ " st.markdown(user_message, unsafe_allow_html=True)\n",
168
+ " st.markdown(bot_message, unsafe_allow_html=True)\n",
169
+ " except Exception as e:\n",
170
+ " st.error(f\"Une erreur s'est produite : {e}\")\n",
171
+ " else:\n",
172
+ " st.error(\"Veuillez entrer un message à envoyer.\")\n",
173
+ "\n",
174
+ "# Button to clear conversation\n",
175
+ "if st.button(\"Effacer la conversation\", key=\"clear_button\"):\n",
176
+ " st.session_state.conversation = []\n",
177
+ " st.experimental_rerun()\n"
178
+ ],
179
+ "metadata": {
180
+ "colab": {
181
+ "base_uri": "https://localhost:8080/"
182
+ },
183
+ "id": "MeP7GbHTNlaQ",
184
+ "outputId": "44340a68-6c67-4f3d-8f1e-3e8b65299800"
185
+ },
186
+ "execution_count": 9,
187
+ "outputs": [
188
+ {
189
+ "output_type": "stream",
190
+ "name": "stdout",
191
+ "text": [
192
+ "Overwriting app.py\n"
193
+ ]
194
+ }
195
+ ]
196
+ },
197
+ {
198
+ "cell_type": "code",
199
+ "source": [
200
+ "\n",
201
+ "import subprocess\n",
202
+ "import threading\n",
203
+ "\n",
204
+ "# Function to run the Streamlit app\n",
205
+ "def run_streamlit():\n",
206
+ " subprocess.run([\"streamlit\", \"run\", \"app.py\"])\n",
207
+ "\n",
208
+ "# Run Streamlit app in a separate thread\n",
209
+ "thread = threading.Thread(target=run_streamlit)\n",
210
+ "thread.start()"
211
+ ],
212
+ "metadata": {
213
+ "id": "OmYaa3PcNuUX"
214
+ },
215
+ "execution_count": 10,
216
+ "outputs": []
217
+ },
218
+ {
219
+ "cell_type": "code",
220
+ "source": [
221
+ "!ssh -o StrictHostKeyChecking=no -R 80:localhost:8501 serveo.net\n"
222
+ ],
223
+ "metadata": {
224
+ "colab": {
225
+ "base_uri": "https://localhost:8080/"
226
+ },
227
+ "id": "_GJ9yf8RNuZH",
228
+ "outputId": "41d124e2-d629-4798-aee0-8317d94ad9e0"
229
+ },
230
+ "execution_count": null,
231
+ "outputs": [
232
+ {
233
+ "output_type": "stream",
234
+ "name": "stdout",
235
+ "text": [
236
+ "\u001b[32mForwarding HTTP traffic from https://2617ecb8e4b6eb044d3540ac0a6929a7.serveo.net\n",
237
+ "\u001b[0mHTTP request from 102.52.154.248 to https://2617ecb8e4b6eb044d3540ac0a6929a7.serveo.net/\n",
238
+ "HTTP request from 102.52.154.248 to https://2617ecb8e4b6eb044d3540ac0a6929a7.serveo.net/static/media/SourceSansPro-Regular.0d69e5ff5e92ac64a0c9.woff2\n",
239
+ "HTTP request from 102.52.154.248 to https://2617ecb8e4b6eb044d3540ac0a6929a7.serveo.net/static/css/main.29bca1b5.css\n",
240
+ "HTTP request from 102.52.154.248 to https://2617ecb8e4b6eb044d3540ac0a6929a7.serveo.net/static/media/SourceSansPro-SemiBold.abed79cd0df1827e18cf.woff2\n",
241
+ "HTTP request from 102.52.154.248 to https://2617ecb8e4b6eb044d3540ac0a6929a7.serveo.net/static/media/SourceSansPro-Bold.118dea98980e20a81ced.woff2\n",
242
+ "HTTP request from 102.52.154.248 to https://2617ecb8e4b6eb044d3540ac0a6929a7.serveo.net/static/js/main.d55f6a3c.js\n",
243
+ "HTTP request from 102.52.154.248 to https://2617ecb8e4b6eb044d3540ac0a6929a7.serveo.net/_stcore/health\n",
244
+ "HTTP request from 102.52.154.248 to https://2617ecb8e4b6eb044d3540ac0a6929a7.serveo.net/_stcore/host-config\n",
245
+ "HTTP request from 102.52.154.248 to https://2617ecb8e4b6eb044d3540ac0a6929a7.serveo.net/favicon.png\n",
246
+ "HTTP request from 102.52.154.248 to https://2617ecb8e4b6eb044d3540ac0a6929a7.serveo.net/_stcore/stream\n",
247
+ "HTTP request from 102.52.154.248 to https://2617ecb8e4b6eb044d3540ac0a6929a7.serveo.net/static/js/9656.8c935274.chunk.js\n",
248
+ "HTTP request from 102.52.154.248 to https://2617ecb8e4b6eb044d3540ac0a6929a7.serveo.net/static/js/8691.cb9c04cf.chunk.js\n",
249
+ "HTTP request from 102.52.154.248 to https://2617ecb8e4b6eb044d3540ac0a6929a7.serveo.net/static/js/1792.8bd6ce2a.chunk.js\n"
250
+ ]
251
+ }
252
+ ]
253
+ },
254
+ {
255
+ "cell_type": "code",
256
+ "source": [],
257
+ "metadata": {
258
+ "id": "hgbDk_jlNuc1"
259
+ },
260
+ "execution_count": null,
261
+ "outputs": []
262
+ },
263
+ {
264
+ "cell_type": "code",
265
+ "source": [],
266
+ "metadata": {
267
+ "id": "L16MZR1MNugL"
268
+ },
269
+ "execution_count": null,
270
+ "outputs": []
271
+ },
272
+ {
273
+ "cell_type": "code",
274
+ "source": [],
275
+ "metadata": {
276
+ "id": "FofgP0TXNuiV"
277
+ },
278
+ "execution_count": null,
279
+ "outputs": []
280
+ }
281
+ ]
282
+ }