File size: 15,126 Bytes
342f878
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "provenance": []
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "language_info": {
      "name": "python"
    }
  },
  "cells": [
    {
      "cell_type": "code",
      "source": [
        "!pip install streamlit pyngrok\n"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "ZHJdze1JNe-2",
        "outputId": "587a9cb7-bd3f-4779-bafe-3742a0ac0e77"
      },
      "execution_count": 2,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Requirement already satisfied: streamlit in /usr/local/lib/python3.10/dist-packages (1.37.1)\n",
            "Requirement already satisfied: pyngrok in /usr/local/lib/python3.10/dist-packages (7.2.0)\n",
            "Requirement already satisfied: altair<6,>=4.0 in /usr/local/lib/python3.10/dist-packages (from streamlit) (4.2.2)\n",
            "Requirement already satisfied: blinker<2,>=1.0.0 in /usr/lib/python3/dist-packages (from streamlit) (1.4)\n",
            "Requirement already satisfied: cachetools<6,>=4.0 in /usr/local/lib/python3.10/dist-packages (from streamlit) (5.5.0)\n",
            "Requirement already satisfied: click<9,>=7.0 in /usr/local/lib/python3.10/dist-packages (from streamlit) (8.1.7)\n",
            "Requirement already satisfied: numpy<3,>=1.20 in /usr/local/lib/python3.10/dist-packages (from streamlit) (1.26.4)\n",
            "Requirement already satisfied: packaging<25,>=20 in /usr/local/lib/python3.10/dist-packages (from streamlit) (24.1)\n",
            "Requirement already satisfied: pandas<3,>=1.3.0 in /usr/local/lib/python3.10/dist-packages (from streamlit) (2.1.4)\n",
            "Requirement already satisfied: pillow<11,>=7.1.0 in /usr/local/lib/python3.10/dist-packages (from streamlit) (9.4.0)\n",
            "Requirement already satisfied: protobuf<6,>=3.20 in /usr/local/lib/python3.10/dist-packages (from streamlit) (3.20.3)\n",
            "Requirement already satisfied: pyarrow>=7.0 in /usr/local/lib/python3.10/dist-packages (from streamlit) (14.0.2)\n",
            "Requirement already satisfied: requests<3,>=2.27 in /usr/local/lib/python3.10/dist-packages (from streamlit) (2.32.3)\n",
            "Requirement already satisfied: rich<14,>=10.14.0 in /usr/local/lib/python3.10/dist-packages (from streamlit) (13.7.1)\n",
            "Requirement already satisfied: tenacity<9,>=8.1.0 in /usr/local/lib/python3.10/dist-packages (from streamlit) (8.5.0)\n",
            "Requirement already satisfied: toml<2,>=0.10.1 in /usr/local/lib/python3.10/dist-packages (from streamlit) (0.10.2)\n",
            "Requirement already satisfied: typing-extensions<5,>=4.3.0 in /usr/local/lib/python3.10/dist-packages (from streamlit) (4.12.2)\n",
            "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",
            "Requirement already satisfied: pydeck<1,>=0.8.0b4 in /usr/local/lib/python3.10/dist-packages (from streamlit) (0.9.1)\n",
            "Requirement already satisfied: tornado<7,>=6.0.3 in /usr/local/lib/python3.10/dist-packages (from streamlit) (6.3.3)\n",
            "Requirement already satisfied: watchdog<5,>=2.1.5 in /usr/local/lib/python3.10/dist-packages (from streamlit) (4.0.2)\n",
            "Requirement already satisfied: PyYAML>=5.1 in /usr/local/lib/python3.10/dist-packages (from pyngrok) (6.0.2)\n",
            "Requirement already satisfied: entrypoints in /usr/local/lib/python3.10/dist-packages (from altair<6,>=4.0->streamlit) (0.4)\n",
            "Requirement already satisfied: jinja2 in /usr/local/lib/python3.10/dist-packages (from altair<6,>=4.0->streamlit) (3.1.4)\n",
            "Requirement already satisfied: jsonschema>=3.0 in /usr/local/lib/python3.10/dist-packages (from altair<6,>=4.0->streamlit) (4.23.0)\n",
            "Requirement already satisfied: toolz in /usr/local/lib/python3.10/dist-packages (from altair<6,>=4.0->streamlit) (0.12.1)\n",
            "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",
            "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",
            "Requirement already satisfied: pytz>=2020.1 in /usr/local/lib/python3.10/dist-packages (from pandas<3,>=1.3.0->streamlit) (2024.1)\n",
            "Requirement already satisfied: tzdata>=2022.1 in /usr/local/lib/python3.10/dist-packages (from pandas<3,>=1.3.0->streamlit) (2024.1)\n",
            "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",
            "Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.10/dist-packages (from requests<3,>=2.27->streamlit) (3.7)\n",
            "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",
            "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",
            "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",
            "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",
            "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",
            "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",
            "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",
            "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",
            "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",
            "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",
            "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",
            "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"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "%%writefile app.py\n",
        "import streamlit as st\n",
        "from gradio_client import Client\n",
        "\n",
        "# Configuration de la page\n",
        "st.set_page_config(page_title=\"🎓🤖 Interface Chatbot - Activités Hassan II et ses chercheurs\", layout=\"centered\")\n",
        "\n",
        "# Custom CSS for styling\n",
        "st.markdown(\"\"\"\n",
        "    <style>\n",
        "    body {\n",
        "        font-family: 'Arial', sans-serif;\n",
        "    }\n",
        "    .frame {\n",
        "        border: 2px solid #ccc;\n",
        "        border-radius: 12px;\n",
        "        padding: 20px;\n",
        "        margin: 15px 0;\n",
        "        background-color: #f1f5f9;\n",
        "        box-shadow: 0 4px 8px rgba(0,0,0,0.1);\n",
        "    }\n",
        "    .khaoula-label {\n",
        "        font-weight: bold;\n",
        "        color: #1E90FF;\n",
        "        font-size: 20px;\n",
        "        margin-bottom: 8px;\n",
        "    }\n",
        "    .chatbot-label {\n",
        "        font-weight: bold;\n",
        "        color: #FF4500;\n",
        "        font-size: 20px;\n",
        "        margin-bottom: 8px;\n",
        "    }\n",
        "    .message {\n",
        "        font-size: 16px;\n",
        "        line-height: 1.6;\n",
        "    }\n",
        "    .send-button {\n",
        "        background-color: #1E90FF;\n",
        "        color: white;\n",
        "        border-radius: 8px;\n",
        "        padding: 10px 20px;\n",
        "        border: none;\n",
        "        cursor: pointer;\n",
        "        font-size: 16px;\n",
        "    }\n",
        "    .send-button:hover {\n",
        "        background-color: #1C86EE;\n",
        "    }\n",
        "    </style>\n",
        "    \"\"\", unsafe_allow_html=True)\n",
        "\n",
        "# Titre et description\n",
        "st.title(\"🎓🤖 Interface Chatbot - Activités Hassan II et ses chercheurs\")\n",
        "st.write(\"💬 Entrez votre question ou message et obtenez une réponse 🧠\")\n",
        "\n",
        "# Initialize Gradio Client\n",
        "client = Client(\"yousef4/mistral-7b-v0.3-chat\")\n",
        "\n",
        "# Session state to keep track of conversation\n",
        "if 'conversation' not in st.session_state:\n",
        "    st.session_state.conversation = []\n",
        "\n",
        "# Display previous messages\n",
        "for message in st.session_state.conversation:\n",
        "    st.markdown(message, unsafe_allow_html=True)\n",
        "\n",
        "# User input and button\n",
        "user_input = st.text_input(\"🗣️ Khaoula :\")\n",
        "\n",
        "if st.button(\"Envoyer 🚀\", key=\"send_button\", help=\"Cliquez pour envoyer votre message\"):\n",
        "    if user_input.strip():\n",
        "        try:\n",
        "            response = client.predict(user_input)\n",
        "            user_message = f\"<div class='frame'><p class='khaoula-label'>🗣️ Khaoula :</p><p class='message'>{user_input}</p></div>\"\n",
        "            bot_message = f\"<div class='frame'><p class='chatbot-label'>🤖 Chatbot :</p><p class='message'>{response} 📚</p></div>\"\n",
        "\n",
        "            # Update conversation\n",
        "            st.session_state.conversation.append(user_message)\n",
        "            st.session_state.conversation.append(bot_message)\n",
        "\n",
        "            st.markdown(user_message, unsafe_allow_html=True)\n",
        "            st.markdown(bot_message, unsafe_allow_html=True)\n",
        "        except Exception as e:\n",
        "            st.error(f\"Une erreur s'est produite : {e}\")\n",
        "    else:\n",
        "        st.error(\"Veuillez entrer un message à envoyer.\")\n",
        "\n",
        "# Button to clear conversation\n",
        "if st.button(\"Effacer la conversation\", key=\"clear_button\"):\n",
        "    st.session_state.conversation = []\n",
        "    st.experimental_rerun()\n"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "MeP7GbHTNlaQ",
        "outputId": "44340a68-6c67-4f3d-8f1e-3e8b65299800"
      },
      "execution_count": 9,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Overwriting app.py\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "\n",
        "import subprocess\n",
        "import threading\n",
        "\n",
        "# Function to run the Streamlit app\n",
        "def run_streamlit():\n",
        "    subprocess.run([\"streamlit\", \"run\", \"app.py\"])\n",
        "\n",
        "# Run Streamlit app in a separate thread\n",
        "thread = threading.Thread(target=run_streamlit)\n",
        "thread.start()"
      ],
      "metadata": {
        "id": "OmYaa3PcNuUX"
      },
      "execution_count": 10,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "!ssh -o StrictHostKeyChecking=no -R 80:localhost:8501 serveo.net\n"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "_GJ9yf8RNuZH",
        "outputId": "41d124e2-d629-4798-aee0-8317d94ad9e0"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "\u001b[32mForwarding HTTP traffic from https://2617ecb8e4b6eb044d3540ac0a6929a7.serveo.net\n",
            "\u001b[0mHTTP request from 102.52.154.248 to https://2617ecb8e4b6eb044d3540ac0a6929a7.serveo.net/\n",
            "HTTP request from 102.52.154.248 to https://2617ecb8e4b6eb044d3540ac0a6929a7.serveo.net/static/media/SourceSansPro-Regular.0d69e5ff5e92ac64a0c9.woff2\n",
            "HTTP request from 102.52.154.248 to https://2617ecb8e4b6eb044d3540ac0a6929a7.serveo.net/static/css/main.29bca1b5.css\n",
            "HTTP request from 102.52.154.248 to https://2617ecb8e4b6eb044d3540ac0a6929a7.serveo.net/static/media/SourceSansPro-SemiBold.abed79cd0df1827e18cf.woff2\n",
            "HTTP request from 102.52.154.248 to https://2617ecb8e4b6eb044d3540ac0a6929a7.serveo.net/static/media/SourceSansPro-Bold.118dea98980e20a81ced.woff2\n",
            "HTTP request from 102.52.154.248 to https://2617ecb8e4b6eb044d3540ac0a6929a7.serveo.net/static/js/main.d55f6a3c.js\n",
            "HTTP request from 102.52.154.248 to https://2617ecb8e4b6eb044d3540ac0a6929a7.serveo.net/_stcore/health\n",
            "HTTP request from 102.52.154.248 to https://2617ecb8e4b6eb044d3540ac0a6929a7.serveo.net/_stcore/host-config\n",
            "HTTP request from 102.52.154.248 to https://2617ecb8e4b6eb044d3540ac0a6929a7.serveo.net/favicon.png\n",
            "HTTP request from 102.52.154.248 to https://2617ecb8e4b6eb044d3540ac0a6929a7.serveo.net/_stcore/stream\n",
            "HTTP request from 102.52.154.248 to https://2617ecb8e4b6eb044d3540ac0a6929a7.serveo.net/static/js/9656.8c935274.chunk.js\n",
            "HTTP request from 102.52.154.248 to https://2617ecb8e4b6eb044d3540ac0a6929a7.serveo.net/static/js/8691.cb9c04cf.chunk.js\n",
            "HTTP request from 102.52.154.248 to https://2617ecb8e4b6eb044d3540ac0a6929a7.serveo.net/static/js/1792.8bd6ce2a.chunk.js\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [],
      "metadata": {
        "id": "hgbDk_jlNuc1"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [],
      "metadata": {
        "id": "L16MZR1MNugL"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [],
      "metadata": {
        "id": "FofgP0TXNuiV"
      },
      "execution_count": null,
      "outputs": []
    }
  ]
}