Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -67,86 +67,61 @@ index.add(text_embeddings)
|
|
67 |
#index = faiss.read_index("./resourse/embeddings_ngap.faiss")
|
68 |
|
69 |
print("Finish the model init process")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
70 |
|
71 |
-
|
72 |
-
model.config["systemPrompt"] = "Tu es un assitant et tu dois répondre en français"
|
73 |
-
model._is_chat_session_activated = False
|
74 |
|
75 |
-
max_new_tokens = 2048
|
76 |
|
77 |
-
def generater(message, history):
|
78 |
-
prompt = "<s>"
|
79 |
-
for user_message, assistant_message in history:
|
80 |
-
prompt += model.config["promptTemplate"].format(user_message)
|
81 |
-
prompt += assistant_message + "</s>"
|
82 |
-
prompt += model.config["promptTemplate"].format(message)
|
83 |
|
84 |
-
|
85 |
|
86 |
-
|
87 |
-
#retrieved_chunk = [chunks[i] for i in I.tolist()[0]]
|
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 |
-
gr.Slider(
|
125 |
-
label="top_k",
|
126 |
-
value=40,
|
127 |
-
minimum=0,
|
128 |
-
maximum=1000,
|
129 |
-
step=1,
|
130 |
-
interactive=True,
|
131 |
-
info="limits candidate tokens to a fixed number after sorting by probability. Setting it higher than the vocabulary size deactivates this limit.",
|
132 |
-
)
|
133 |
-
]
|
134 |
-
"""
|
135 |
-
additional_inputs=[
|
136 |
-
gr.UploadButton(file_types=[".pdf",".csv",".doc"])
|
137 |
-
]
|
138 |
-
iface = gr.ChatInterface(
|
139 |
-
fn = generater,
|
140 |
-
title=title,
|
141 |
-
description = description,
|
142 |
-
chatbot=chatbot,
|
143 |
-
additional_inputs=additional_inputs,
|
144 |
|
145 |
-
)
|
146 |
|
147 |
-
|
148 |
-
|
149 |
-
iface.render()
|
150 |
|
151 |
if __name__ == "__main__":
|
152 |
demo.queue(max_size=3).launch()
|
|
|
67 |
#index = faiss.read_index("./resourse/embeddings_ngap.faiss")
|
68 |
|
69 |
print("Finish the model init process")
|
70 |
+
def format_chat_prompt(message, chat_history):
|
71 |
+
prompt = ""
|
72 |
+
for turn in chat_history:
|
73 |
+
user_message, bot_message = turn
|
74 |
+
prompt = f"{prompt}\nUser: {user_message}\nAssistant: {bot_message}"
|
75 |
+
prompt = f"{prompt}\nUser: {message}\nAssistant:"
|
76 |
+
return prompt
|
77 |
|
78 |
+
def respond(message, chat_history):
|
|
|
|
|
79 |
|
|
|
80 |
|
|
|
|
|
|
|
|
|
|
|
|
|
81 |
|
82 |
+
prompt = message
|
83 |
|
84 |
+
context.append({'role':'user', 'content':f"{prompt}"})
|
|
|
85 |
|
86 |
+
tokenized_chat = tokenizer.apply_chat_template(context, tokenize=True, add_generation_prompt=True, return_tensors="pt")
|
87 |
+
|
88 |
+
outputs = model.generate(tokenized_chat, max_new_tokens=1000, temperature = 0.0)
|
89 |
+
|
90 |
+
bot_message = tokenizer.decode(outputs[0]).split("<|assistant|>")[-1].replace("</s>","")
|
91 |
+
|
92 |
+
context.append({'role':'assistant', 'content':f"{bot_message}"})
|
93 |
+
|
94 |
+
chat_history.append((message, bot_message))
|
95 |
+
return "", chat_history
|
96 |
+
|
97 |
+
with gr.Blocks() as demo:
|
98 |
+
gr.Markdown("# Assistant virtuel Ameli")
|
99 |
+
gr.Markdown("Mes réponses sont générées par IA. Elles peuvent être fausses ou imprécises.")
|
100 |
+
with gr.Row():
|
101 |
+
with gr.Column(scale=1):
|
102 |
+
#msg = gr.Audio(sources=["microphone"])
|
103 |
+
|
104 |
+
audio_file = gr.Audio(sources=["microphone"])
|
105 |
+
b1 = gr.Button("Posez votre question à l'oral")
|
106 |
+
|
107 |
+
text = gr.Textbox(lines =5)
|
108 |
+
|
109 |
+
|
110 |
+
|
111 |
+
b1.click(speech_to_text, inputs=audio_file, outputs=text)
|
112 |
+
|
113 |
+
|
114 |
+
#msg = gr.Textbox(label="Posez votre question")
|
115 |
+
btn = gr.Button("Soumettre la question")
|
116 |
+
|
117 |
+
|
118 |
+
with gr.Column(scale=2, min_width=50):
|
119 |
+
chatbot = gr.Chatbot(height=700) #just to fit the notebook
|
120 |
+
clear = gr.ClearButton(components=[text, chatbot], value="Clear console")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
121 |
|
|
|
122 |
|
123 |
+
btn.click(respond, inputs=[text, chatbot], outputs=[text, chatbot])
|
124 |
+
text.submit(respond, inputs=[text, chatbot], outputs=[text, chatbot]) #Press enter to submit
|
|
|
125 |
|
126 |
if __name__ == "__main__":
|
127 |
demo.queue(max_size=3).launch()
|