Alexandre Huynh commited on
Commit
b8a38bc
·
1 Parent(s): fb4596e

Commit app.py

Browse files
Files changed (1) hide show
  1. app.py +285 -0
app.py ADDED
@@ -0,0 +1,285 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # -*- coding: utf-8 -*-
2
+ """export_gradio_spaces.ipynb
3
+
4
+ Automatically generated by Colaboratory.
5
+
6
+ Original file is located at
7
+ https://colab.research.google.com/drive/1mI9gAr_Vtpl2mZsoZoRc56muGboXdsMi
8
+
9
+ # Chargement du modèle GPT-2 entrainé
10
+ """
11
+
12
+ !pip install --upgrade typing-extensions
13
+ !pip install gradio -q
14
+ !pip install keras_nlp -q
15
+
16
+ import os
17
+ from tensorflow import keras
18
+ import keras_nlp
19
+ #from google.colab import drive
20
+ import time
21
+
22
+ os.environ["KERAS_BACKEND"] = "tensorflow" # or "tensorflow" or "torch"
23
+
24
+ keras.mixed_precision.set_global_policy("mixed_float16")
25
+
26
+ preprocessor = keras_nlp.models.GPT2CausalLMPreprocessor.from_preset(
27
+ "gpt2_base_en",
28
+ sequence_length=128,
29
+ )
30
+ gpt2_lm = keras_nlp.models.GPT2CausalLM.from_preset(
31
+ "gpt2_base_en", preprocessor=preprocessor
32
+ )
33
+
34
+ #drive.mount('/content/drive', force_remount=True)
35
+ checkpoint_path = "/aloqas_model/cp.ckpt"
36
+
37
+ gpt2_lm.load_weights(checkpoint_path)
38
+
39
+ """# Chargement et configuration de gradio"""
40
+
41
+ def generate_text(prompt):
42
+ return gpt2_lm.generate(prompt, max_length=100)
43
+
44
+ # CSS styles
45
+ css = """
46
+ body, html {
47
+ height: 100%;
48
+ margin: 0;
49
+ font-family: 'Arial', sans-serif;
50
+ background-color: #131722; /* Dark background color */
51
+ color: #ffffff;
52
+ }
53
+
54
+ /* Container for the entire chat interface */
55
+ #chat-interface {
56
+ display: flex;
57
+ flex-direction: column;
58
+ max-width: 80%; /* Ensure maximum width */
59
+ height: 100vh;
60
+ justify-content: space-between;
61
+ margin: 0 auto; /* Center the chat interface */
62
+ }
63
+
64
+ /* Container for the chat messages */
65
+ #chat-messages {
66
+ flex-grow: 1;
67
+ overflow-y: auto;
68
+ background: none;
69
+ max-width: 100%; /* Ensure maximum width */
70
+ }
71
+
72
+ /* Styling for the chatbot bubble messages */
73
+ .gr-chatbot .chatbubble {
74
+ max-width: 85%;
75
+ margin-bottom: 12px;
76
+ border-radius: 16px;
77
+ padding: 12px 16px;
78
+ position: relative;
79
+ font-size: 1rem;
80
+ }
81
+
82
+ .gr-chatbot .chatbubble:before {
83
+ content: '';
84
+ position: absolute;
85
+ width: 0;
86
+ height: 0;
87
+ border-style: solid;
88
+ }
89
+
90
+ /* Chatbot message bubble */
91
+ .gr-chatbot .bot .chatbubble {
92
+ background-color: #2d3e55; /* Darker bubble background */
93
+ }
94
+
95
+ /* User message bubble */
96
+ .gr-chatbot .user .chatbubble {
97
+ background-color: #4CAF50; /* Green bubble background */
98
+ }
99
+
100
+ /* Input area styling */
101
+ #input-area {
102
+ display: flex;
103
+ align-items: center;
104
+ padding: 20px;
105
+ }
106
+
107
+ /* Text input field styling */
108
+ #input-area .gr-textbox {
109
+ flex: 1;
110
+ margin-right: 12px;
111
+ padding: 12px 16px;
112
+ border: 5px solid #627385;
113
+ border-radius: 16px;
114
+ font-size: 1rem;
115
+ }
116
+
117
+ /* Send button styling */
118
+ #input-area button {
119
+ padding: 12px 20px;
120
+ background-color: #4CAF50; /* Green button color */
121
+ border: none;
122
+ border-radius: 16px;
123
+ cursor: pointer;
124
+ font-size: 1rem;
125
+ color: #fff;
126
+ }
127
+
128
+ /* Suggestion buttons styling */
129
+ .suggestion-btn {
130
+ background-color: #2d3e55; /* Dark button color */
131
+ color: #ffffff;
132
+ padding: 10px 50px;
133
+ margin: 5px;
134
+ border: 2px solid #627385;
135
+ border-radius: 20px;
136
+ cursor: pointer;
137
+ font-size: 14px;
138
+ display: inline-block;
139
+ }
140
+
141
+ /* Suggestions container */
142
+ #suggestions {
143
+ padding: 20px;
144
+ }
145
+
146
+ /* Style the avatar images if needed */
147
+ .gr-chatbot .gr-chatbot-avatar-image {
148
+ border-radius: 50%;
149
+ }
150
+
151
+ /* Style for the chatbot avatar */
152
+ .gr-chatbot .bot .gr-chatbot-avatar-image {
153
+ background-image: url('/content/drive/MyDrive/img/ALOQAS logo.png');
154
+ }
155
+
156
+ /* Style for the user avatar */
157
+ .gr-chatbot .user .gr-chatbot-avatar-image {
158
+ background-image: url('/content/drive/MyDrive/img/pp discord copie.png');
159
+ }
160
+
161
+ /* Additional CSS for layout adjustments */
162
+ #header {
163
+ display: flex;
164
+ flex-direction: column;
165
+ max-width: 100%; /* Ensure maximum width */
166
+ gap: 20px;
167
+ justify-content: center;
168
+ align-items: center;
169
+ margin: 0 auto; /* Center the chat interface */
170
+ }
171
+
172
+ #main-title {
173
+ font-size: 2.5em;
174
+ margin-bottom: 0.5em;
175
+ color: #ffffff;
176
+ }
177
+
178
+ #sub-title {
179
+ font-size: 1.5em;
180
+ margin-bottom: 1em;
181
+ color: #ffffff;
182
+ }
183
+
184
+ /* Adjust the chat interface to not grow beyond its container */
185
+ #chat-interface {
186
+ flex: 1;
187
+ overflow: auto; /* Add scrolling to the chat interface if needed */
188
+ }
189
+
190
+ .logo {
191
+ width: 300px; /* Width of the logo */
192
+ height: 300px; /* Height of the logo, should be equal to width for a perfect circle */
193
+ background-image: url('https://github.com/LucasAguetai/ALOQAS/blob/main/Ressources/ALOQAS%20logo.png?raw=trueg');
194
+ background-size: cover; /* Cover the entire area of the div without stretching */
195
+ background-position: center; /* Center the background image within the div */
196
+ border-radius: 50%; /* This will make it circular */
197
+ display: inline-block; /* Allows the div to be inline with text and other inline elements */
198
+ margin-bottom: 1em; /* Space below the logo */
199
+ }
200
+
201
+ #input-area > *{
202
+ padding: 0px;
203
+ border: 3px solid #627385;
204
+ }
205
+
206
+ #input-area > * > *{
207
+ padding: 0px;
208
+ background-color: #091E37;
209
+ }
210
+
211
+ #input-area > * * {
212
+ border-radius: 0px !important;
213
+ }
214
+
215
+ .dark{
216
+ --background-fill-primary: #091E37 !important;
217
+ }
218
+
219
+ .send{
220
+ max-width: 10px;
221
+ background-color: #627385 !important;
222
+ }
223
+ """
224
+
225
+ import gradio as gr
226
+
227
+ theme = gr.themes.Base(primary_hue="slate")
228
+
229
+ suggestion_text_1 = "What's the weather like today?"
230
+ suggestion_text_2 = "Can you provide stock market updates?"
231
+ suggestion_text_3 = "I need assistance with my account."
232
+
233
+ # Assuming generate_text is a function that generates a text response
234
+ def respond(message):
235
+ # You will need to implement generate_response to create a response to the user's message.
236
+ response = generate_text(message)
237
+ return response
238
+
239
+ def respond(message):
240
+ response = generate_text(message)
241
+ return [["bot", response]]
242
+
243
+ def suggestion1():
244
+ response = generate_text(suggestion_text_1)
245
+ return [["user", suggestion_text_1], ["bot", response]]
246
+
247
+ def suggestion2():
248
+ response = generate_text(suggestion_text_2)
249
+ return [["user", suggestion_text_2], ["bot", response]]
250
+
251
+ def suggestion3():
252
+ response = generate_text(suggestion_text_3)
253
+ return [["user", suggestion_text_3], ["bot", response]]
254
+
255
+ with gr.Blocks(theme=theme, css=css) as demo:
256
+ gr.Markdown("""
257
+ <div id='header'>
258
+ <h1 id='main-title'>ALOQAS</h1>
259
+ <div class='logo'></div>
260
+ <h2 id='sub-title'>How can I help you?</h2>
261
+ </div>
262
+ """)
263
+ with gr.Column(elem_id="chat-interface"):
264
+ chat = gr.Chatbot(elem_id="chat-messages", show_label=False)
265
+ with gr.Row(elem_id="suggestions"):
266
+ sugg1 = gr.Button(suggestion_text_1, elem_classes="suggestion-btn").click(
267
+ suggestion1, outputs=chat
268
+ )
269
+ sugg2 = gr.Button(suggestion_text_2, elem_classes="suggestion-btn").click(
270
+ suggestion2, outputs=chat
271
+ )
272
+ sugg3 = gr.Button(suggestion_text_3, elem_classes="suggestion-btn").click(
273
+ suggestion3, outputs=chat
274
+ )
275
+ with gr.Row(elem_id="input-area"):
276
+ text_input = gr.Textbox(placeholder="Write to ALOQAS...", show_label=False)
277
+ send_button = gr.Button("Send", elem_classes="send")
278
+ send_button.click(
279
+ fn=respond,
280
+ inputs=text_input,
281
+ outputs=chat
282
+ )
283
+
284
+ demo.launch(share=False)
285
+