File size: 8,814 Bytes
b8a38bc
 
 
 
 
 
 
 
 
 
 
933284e
d2d2033
cb046d0
 
 
 
 
eb96f5a
2bc1564
fca544c
 
b8a38bc
 
 
 
 
 
fba408d
b8a38bc
 
 
 
 
 
 
 
 
 
 
 
 
 
067b986
b8a38bc
 
 
 
 
fba408d
b8a38bc
 
fba408d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b8a38bc
 
 
2854468
b8a38bc
 
 
2854468
 
b8a38bc
 
2854468
 
 
 
 
 
 
 
a6dc130
b8a38bc
 
fa2f2a0
b8a38bc
 
 
 
 
 
 
 
 
 
 
 
6b0e3ec
fa2f2a0
b8a38bc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a6dc130
 
8708baa
b8a38bc
 
 
 
 
 
 
1d62012
 
 
 
 
 
 
 
b8a38bc
 
 
 
 
 
 
b443631
b8a38bc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1d62012
b8a38bc
 
 
 
b8c30e3
 
 
 
 
b8a38bc
b8c30e3
b8a38bc
6db3d70
f2d254d
04fd816
b8c30e3
da2afc2
d8c5053
b8c30e3
 
 
 
d8c5053
b8c30e3
 
 
 
d8c5053
b8c30e3
865e3fd
d8c5053
 
 
 
07ced5a
c7f57c0
0f3561e
b8c30e3
b8a38bc
 
 
 
 
 
 
f15c9a6
 
 
 
 
 
b8a38bc
943f6a8
504db4c
04fd816
 
504db4c
04fd816
 
504db4c
b8a38bc
504db4c
f15c9a6
504db4c
 
b8c30e3
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
# -*- coding: utf-8 -*-
"""export_gradio_spaces.ipynb

Automatically generated by Colaboratory.

Original file is located at
    https://colab.research.google.com/drive/1mI9gAr_Vtpl2mZsoZoRc56muGboXdsMi

# Chargement du modèle GPT-2 entrainé
"""


#import pip
import subprocess
import sys

def install(package):
    subprocess.check_call([sys.executable, "-m", "pip", "install", package])

install("typing-extensions")
install("gradio")
install("keras_nlp")

import os
from tensorflow import keras
import keras_nlp
#from google.colab import drive
import time
import re

os.environ["KERAS_BACKEND"] = "tensorflow"  # or "tensorflow" or "torch"

keras.mixed_precision.set_global_policy("mixed_float16")

preprocessor = keras_nlp.models.GPT2CausalLMPreprocessor.from_preset(
    "gpt2_base_en",
    sequence_length=128,
)
gpt2_lm = keras_nlp.models.GPT2CausalLM.from_preset(
    "gpt2_base_en", preprocessor=preprocessor
)

#drive.mount('/content/drive', force_remount=True)
checkpoint_path = "./aloqas_model_checkpoints/cp.ckpt"

gpt2_lm.load_weights(checkpoint_path)

"""# Chargement et configuration de gradio"""

"""
def generate_text(prompt):
    return gpt2_lm.generate(prompt, max_length=100)
"""

# Expérimental
def format_text(text, to_remove):
    # Function to format generated text
    # - removes the prompt from the answer
    # - removes unnecessary space chars before punctuation
    # - capitalize the first letter of each sentence
    text = text.replace(to_remove, '')
    text = re.sub(r' +,', ',', text) # comma
    text = re.sub(r' +\.', '.', text) # full stop
    text = '. '.join(sentence.strip().capitalize() for sentence in text.split('.'))

    return text

# Expérimental
def generate_text(prompt):
  output = gpt2_lm.generate(prompt, max_length=150)
  formatted_output = format_text(output, prompt)
  print(f"DEBUG - GPT-2 Output : {output}")
  print(f"DEBUG - Formatted GPT-2 Output : {formatted_output}")
  return formatted_output

# CSS styles
css = """
@import url('https://fonts.googleapis.com/css2?family=Poppins&display=swap')
body, html {
  height: 100%;
  margin: 0;
  font-family : 'Poppins', sans-serif;
  # font-family: 'Arial', sans-serif;
}

:root{
  --body-background-fill: none !important;
}

body,html {
    background-color: #131722; /* Dark background color */
    color: #ffffff;
  }
  
/* Container for the entire chat interface */
#chat-interface {
  display: flex;
  flex-direction: column;
  max-width: 80%; /* Ensure maximum width */
  height: 100vh;
  justify-content: space-between;
  margin: 0 auto; /* Center the chat interface */
}

/* Container for the chat messages */
#chat-messages {
  flex-grow: 1;
  overflow-y: auto;
  background: none;
  border: 1px solid #627385 !important;
  max-height: 25%;
}

/* Styling for the chatbot bubble messages */
.gr-chatbot .chatbubble {
  max-width: 85%;
  margin-bottom: 12px;
  border-radius: 16px;
  padding: 12px 16px;
  position: relative;
  font-size: 1rem;
}

.gr-chatbot .chatbubble:before {
  content: '';
  position: absolute;
  width: 0;
  height: 0;
  border-style: solid;
}

/* Chatbot message bubble */
.gr-chatbot .bot .chatbubble {
  background-color: #2d3e55; /* Darker bubble background */
}

/* User message bubble */
.gr-chatbot .user .chatbubble {
  background-color: #4CAF50; /* Green bubble background */
}

/* Input area styling */
#input-area {
  display: flex;
  align-items: center;
  padding: 20px;
}

/* Text input field styling */
#input-area .gr-textbox {
  flex: 1;
  margin-right: 12px;
  padding: 12px 16px;
  border: 5px solid #627385;
  border-radius: 16px;
  font-size: 1rem;
}

/* Send button styling */
#input-area button {
  padding: 12px 20px;
  background-color: #4CAF50; /* Green button color */
  border: none;
  border-radius: 16px;
  cursor: pointer;
  font-size: 1rem;
  color: #fff;
}

/* Suggestion buttons styling */
.suggestion-btn {
  background-color: #2d3e55; /* Dark button color */
  color: #ffffff;
  padding: 10px 50px;
  margin: 5px;
  border: 2px solid #627385;
  border-radius: 20px;
  cursor: pointer;
  font-size: 14px;
  display: inline-block;
}

/* Suggestions container */
#suggestions {
  padding: 20px;
}

/* Style the avatar images if needed */
.gr-chatbot .gr-chatbot-avatar-image {
  border-radius: 50%;
}

/* Style for the chatbot avatar */
.gr-chatbot .bot .gr-chatbot-avatar-image {
  background-image: url('/content/drive/MyDrive/img/ALOQAS logo.png');
}

/* Style for the user avatar */
.gr-chatbot .user .gr-chatbot-avatar-image {
  background-image: url('/content/drive/MyDrive/img/pp discord copie.png');
}

/* Additional CSS for layout adjustments */
#header {
  display: flex;
  flex-direction: column;
  max-width: 100%; /* Ensure maximum width */
  gap: 20px;
  justify-content: center;
  align-items: center;
  margin: 0 auto; /* Center the chat interface */
}

#main-title {
  font-size: 2.5em;
  margin-bottom: 0.5em;
  color: #ffffff;
}

#sub-title {
  font-size: 1.5em;
  margin-bottom: 1em;
  color: #ffffff;
}

/* Adjust the chat interface to not grow beyond its container */
#chat-interface {
  flex: 1;
  overflow: auto; /* Add scrolling to the chat interface if needed */
}

.logo {
  width: 150px; /* Width of the logo */
  height: 150px; /* Height of the logo, should be equal to width for a perfect circle */
  background-image: url('https://github.com/LucasAguetai/ALOQAS/blob/main/Ressources/Gif%20Aloqas%20Logo.gif?raw=true');
  background-size: cover; /* Cover the entire area of the div without stretching */
  background-position: center; /* Center the background image within the div */
  border-radius: 50%; /* This will make it circular */
  display: inline-block; /* Allows the div to be inline with text and other inline elements */
  margin-bottom: 1em; /* Space below the logo */
}

.message{
  max-width: 50% !important;
}

.pending{
    max-width: 100% !important;
}

#input-area > *{
  padding: 0px;
  border: 3px solid #627385;
}

#input-area > * > *{
  padding: 0px;
  background-color: #091E37 !important;
}

#input-area > * * {
  border-radius: 0px !important;
}

.dark{
  --background-fill-primary: #091E37 !important;
}

.send{
  max-width: 10px;
  background-color: #627385 !important;
}
"""


import gradio as gr

theme = gr.themes.Base(primary_hue="slate")

suggestion = [
    "What are the latest advancements in cancer research ?",
    "What is the impact of diet on heart disease according to recent studies ?",
    "What are the usual causes of lung pain ?"
]

def respond(message, chat_history):
    response = generate_text(message)
    chat_history.append((message,response+"."))
    return "",chat_history

def suggestion1(chat_history):
    response = generate_text(suggestion[0])
    chat_history.append((suggestion[0], response+"."))
    return chat_history

def suggestion2(chat_history):
    response = generate_text(suggestion[1])
    chat_history.append((suggestion[1], response+"."))
    return chat_history

def suggestion3(chat_history):
    response = generate_text(suggestion[2])
    chat_history.append((suggestion[2], response+"."))
    return chat_history

"""
chat_history.append((None, suggestion[2]))
chat_history.append((response, None))
"""
user_profile_image = "https://huggingface.co/spaces/ALOQAS/aloqas-gradio/resolve/main/img/user.png"
bot_profile_image = "https://huggingface.co/spaces/ALOQAS/aloqas-gradio/resolve/main/img/bot.png"

with gr.Blocks(theme=theme, css=css) as demo:  # Suppression de 'css=css' si 'css' n'est pas défini
    gr.Markdown("""
    <div id='header'>
      <h1 id='main-title'>ALOQAS</h1>
      <div class='logo'></div>
    </div>
    """)
    with gr.Column(elem_id="chat-interface"):
        chat = gr.Chatbot( 
            elem_id="chat-messages",
            show_label=False,
            avatar_images=[user_profile_image,bot_profile_image],
            value=[[None,"Hi there ! I'm ALOQAS, a chatbot trained on over 119.000 PubMed scientific papers. Ask me anything about medicine or scientific research !"]]
        )
        with gr.Row(elem_id="suggestions"):
            sugg1 = gr.Button(suggestion[0], elem_classes="suggestion-btn").click(
                suggestion1, inputs=[chat], outputs=[chat]
            )
            sugg2 = gr.Button(suggestion[1], elem_classes="suggestion-btn").click(
                suggestion2, inputs=[chat], outputs=[chat]
            )
            sugg3 = gr.Button(suggestion[2], elem_classes="suggestion-btn").click(
                suggestion3, inputs=[chat], outputs=[chat]
            )
        with gr.Row(elem_id="input-area"):
            text_input = gr.Textbox(placeholder="Type a question, a sentence or keywords to ALOQAS...", show_label=False)
            text_input.submit(respond, inputs=[text_input, chat], outputs=[text_input,chat])
            
demo.launch(share=False)