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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 3 |
+
import torch
|
| 4 |
+
|
| 5 |
+
# Charger le modèle et le tokenizer
|
| 6 |
+
model_name = "MaziyarPanahi/BioMistral-7B-GGUF"
|
| 7 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 8 |
+
model = AutoModelForCausalLM.from_pretrained(model_name)
|
| 9 |
+
model = model.to("cuda" if torch.cuda.is_available() else "cpu")
|
| 10 |
+
|
| 11 |
+
def generate_response(prompt):
|
| 12 |
+
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
| 13 |
+
with torch.no_grad():
|
| 14 |
+
outputs = model.generate(inputs['input_ids'], max_length=150, num_return_sequences=1, no_repeat_ngram_size=2)
|
| 15 |
+
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 16 |
+
return response
|
| 17 |
+
|
| 18 |
+
def add_message(history, message):
|
| 19 |
+
if message["text"] is not None:
|
| 20 |
+
history.append((message["text"], None))
|
| 21 |
+
for x in message["files"]:
|
| 22 |
+
history.append(((x,), None))
|
| 23 |
+
return history, gr.MultimodalTextbox(value=None, interactive=False)
|
| 24 |
+
|
| 25 |
+
def bot(history):
|
| 26 |
+
if history and history[-1][0]:
|
| 27 |
+
history[-1] = (history[-1][0], generate_response(history[-1][0]))
|
| 28 |
+
return history
|
| 29 |
+
|
| 30 |
+
def print_like_dislike(x: gr.LikeData):
|
| 31 |
+
print(x.index, x.value, x.liked)
|
| 32 |
+
|
| 33 |
+
# Création de l'interface Gradio avec le fond animé et des boutons stylisés
|
| 34 |
+
with gr.Blocks(css="""
|
| 35 |
+
|
| 36 |
+
.gradio-container {
|
| 37 |
+
background: url('https://st4.depositphotos.com/8211188/25405/v/450/depositphotos_254059962-stock-illustration-abstract-medical-background-with-flat.jpg')50% 50% no-repeat;
|
| 38 |
+
background-size: cover;
|
| 39 |
+
}
|
| 40 |
+
.chatbox-container {
|
| 41 |
+
max-width: 80%;
|
| 42 |
+
margin: 20px auto;
|
| 43 |
+
padding: 20px 20px;
|
| 44 |
+
background-color: rgb(39 150 160);
|
| 45 |
+
border-radius: 12px;
|
| 46 |
+
box-shadow: 0 0 20px rgba(0, 0, 0, 0.1);
|
| 47 |
+
display: flex;
|
| 48 |
+
flex-direction: row;
|
| 49 |
+
align-items: stretch;
|
| 50 |
+
}
|
| 51 |
+
|
| 52 |
+
.chatbox {
|
| 53 |
+
flex: 1;
|
| 54 |
+
overflow-y: auto;
|
| 55 |
+
padding: 10px;
|
| 56 |
+
display: flex;
|
| 57 |
+
flex-direction: column;
|
| 58 |
+
justify-content: flex-end;
|
| 59 |
+
border-bottom: 1px solid #e39d05;
|
| 60 |
+
height: 400px;
|
| 61 |
+
}
|
| 62 |
+
|
| 63 |
+
.chat-input-container {
|
| 64 |
+
display: flex;
|
| 65 |
+
flex-direction: column;
|
| 66 |
+
padding: 10px;
|
| 67 |
+
border-top: 1px solid #e39d05;
|
| 68 |
+
width: 100%;
|
| 69 |
+
}
|
| 70 |
+
|
| 71 |
+
.chat-input {
|
| 72 |
+
color:blue;
|
| 73 |
+
margin-bottom: 10px;
|
| 74 |
+
flex: 1;
|
| 75 |
+
border-radius: 5px;
|
| 76 |
+
border: 1px solid #e39d05;
|
| 77 |
+
padding: 10px;
|
| 78 |
+
font-size: 16px;
|
| 79 |
+
}
|
| 80 |
+
|
| 81 |
+
.button-container {
|
| 82 |
+
color:#e39d05;
|
| 83 |
+
display: flex;
|
| 84 |
+
flex-direction: row;
|
| 85 |
+
justify-content: flex-start;
|
| 86 |
+
}
|
| 87 |
+
|
| 88 |
+
.button {
|
| 89 |
+
background-color: #e39d05;
|
| 90 |
+
color: black;
|
| 91 |
+
border: none;
|
| 92 |
+
border-radius: 20px;
|
| 93 |
+
padding: 8px 16px;
|
| 94 |
+
margin: 12px;
|
| 95 |
+
cursor: pointer;
|
| 96 |
+
font-size: 20px;
|
| 97 |
+
display: flex;
|
| 98 |
+
align-items: center;
|
| 99 |
+
justify-content: center;
|
| 100 |
+
transition: background-color 0.3s, box-shadow 0.3s;
|
| 101 |
+
}
|
| 102 |
+
|
| 103 |
+
.button:hover {
|
| 104 |
+
background-color: #fa0a0a;
|
| 105 |
+
box-shadow: 0 4px 8px #e39d05;
|
| 106 |
+
}
|
| 107 |
+
|
| 108 |
+
|
| 109 |
+
|
| 110 |
+
|
| 111 |
+
.titre h1 {
|
| 112 |
+
font-family: 'Centaur', serif;
|
| 113 |
+
font-size: 4em;
|
| 114 |
+
margin: 0;
|
| 115 |
+
color: #ad2727;
|
| 116 |
+
text-align: center;
|
| 117 |
+
}
|
| 118 |
+
|
| 119 |
+
.titre p {
|
| 120 |
+
font-size: 3em;
|
| 121 |
+
font-family: 'Centaur', serif;
|
| 122 |
+
margin-top: 10px;
|
| 123 |
+
|
| 124 |
+
color: rgb(9 129 118);
|
| 125 |
+
text-align: center;
|
| 126 |
+
font-weight: bold;
|
| 127 |
+
}
|
| 128 |
+
.titre img{
|
| 129 |
+
display: block;
|
| 130 |
+
margin-left: auto;
|
| 131 |
+
margin-right: auto;
|
| 132 |
+
width: 20%;
|
| 133 |
+
}
|
| 134 |
+
|
| 135 |
+
""") as demo:
|
| 136 |
+
|
| 137 |
+
with gr.Row(elem_classes="titre"):
|
| 138 |
+
|
| 139 |
+
gr.Markdown("<h1>Diagnostique médicale</h1><p class='description'>Bienvenue ! Entrez vos symptômes ou questions pour des conseils médicaux rapides</p><img src='https://imageio.forbes.com/specials-images/imageserve/64b54b7467fcc06271e9bcff/Chatbot-in-a-medical-cap--a-pen-and-a-notebook-in-his-hands-asks-how-he-can-help-/960x0.jpg?height=592&width=711&fit=bounds'>")
|
| 140 |
+
|
| 141 |
+
|
| 142 |
+
with gr.Column(scale=1, elem_classes="chatbox-container"):
|
| 143 |
+
with gr.Row():
|
| 144 |
+
chatbot = gr.Chatbot(
|
| 145 |
+
elem_id="chatbot",
|
| 146 |
+
bubble_full_width=False,
|
| 147 |
+
scale=1,
|
| 148 |
+
elem_classes="chatbox"
|
| 149 |
+
)
|
| 150 |
+
|
| 151 |
+
with gr.Row(elem_classes="chat-input-container"):
|
| 152 |
+
chat_input = gr.MultimodalTextbox(interactive=True,
|
| 153 |
+
file_count="multiple",
|
| 154 |
+
placeholder="Entrez un message ou téléchargez un fichier...",
|
| 155 |
+
show_label=False,
|
| 156 |
+
elem_classes="chat-input")
|
| 157 |
+
|
| 158 |
+
# Container for buttons below the input
|
| 159 |
+
with gr.Row(elem_classes="button-container"):
|
| 160 |
+
clear_button = gr.Button("Effacer", elem_classes="button")
|
| 161 |
+
stop_button = gr.Button("Arrêter", elem_classes="button")
|
| 162 |
+
generate_button = gr.Button("Générer", elem_classes="button")
|
| 163 |
+
|
| 164 |
+
# Configuration des interactions
|
| 165 |
+
chat_msg = chat_input.submit(add_message, [chatbot, chat_input], [chatbot, chat_input])
|
| 166 |
+
bot_msg = chat_msg.then(bot, chatbot, chatbot, api_name="bot_response")
|
| 167 |
+
bot_msg.then(lambda: gr.MultimodalTextbox(interactive=True), None, [chat_input])
|
| 168 |
+
|
| 169 |
+
clear_button.click(lambda: ([], gr.MultimodalTextbox(value=None, interactive=True)), None, [chatbot, chat_input])
|
| 170 |
+
stop_button.click(lambda: "Arrêter cliqué", None, None)
|
| 171 |
+
generate_button.click(lambda: "Générer cliqué", None, None)
|
| 172 |
+
|
| 173 |
+
save_button = gr.Button("Sauvegarder", elem_classes="button")
|
| 174 |
+
save_button.click(fn=lambda history: open("discussion_history.txt", "w").write(str(history)), inputs=chatbot, outputs=None)
|
| 175 |
+
|
| 176 |
+
# Lancement de l'interface
|
| 177 |
+
demo.launch()
|