xavierbarbier commited on
Commit
b57c9e2
·
verified ·
1 Parent(s): ea04ccd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -72
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
- model.config["promptTemplate"] = "[INST] {0} [/INST]"
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
- question_embeddings = np.array([get_text_embedding(message)])
85
 
86
- #D, I = index.search(question_embeddings, k=3) # distance, index
87
- #retrieved_chunk = [chunks[i] for i in I.tolist()[0]]
88
 
89
- #prompt += "Informations de contexte ci-dessous:"
90
- #prompt += [i for i in retrieved_chunk]
91
-
92
- outputs = []
93
- for token in model.generate(prompt=prompt, temp=0.5, top_k = 40, top_p = 1, max_tokens = max_new_tokens, streaming=True):
94
- outputs.append(token)
95
- yield "".join(outputs)
96
-
97
- def vote(data: gr.LikeData):
98
- if data.liked:
99
- return
100
- else:
101
- return
102
-
103
- chatbot = gr.Chatbot(avatar_images=('./resourse/user-icon.png', './resourse/chatbot-icon.png'),bubble_full_width = False)
104
- """
105
- additional_inputs=[
106
- gr.Slider(
107
- label="temperature",
108
- value=0.5,
109
- minimum=0.0,
110
- maximum=2.0,
111
- step=0.05,
112
- interactive=True,
113
- info="Higher values like 0.8 will make the output more random, while lower values like 0.2 will make it more focused and deterministic.",
114
- ),
115
- gr.Slider(
116
- label="top_p",
117
- value=1.0,
118
- minimum=0.0,
119
- maximum=1.0,
120
- step=0.01,
121
- interactive=True,
122
- info="0.1 means only the tokens comprising the top 10% probability mass are considered. Suggest set to 1 and use temperature. 1 means 100% and will disable it",
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
- with gr.Blocks(css="./resourse/style/custom.css") as demo:
148
- chatbot.like(vote, None, None)
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()