Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -30,20 +30,7 @@ def embed(text):
|
|
| 30 |
outputs = steelbert_model(**inputs, output_hidden_states=True)
|
| 31 |
return outputs.hidden_states[-1][:,0,:].cpu().numpy()[0]
|
| 32 |
|
| 33 |
-
# --- 3.
|
| 34 |
-
docs = {
|
| 35 |
-
"doc1": "L’acier X42 a une résistance à la traction de 415 MPa.",
|
| 36 |
-
"doc2": "L’acier inoxydable 304 est résistant à la corrosion et à l’oxydation."
|
| 37 |
-
}
|
| 38 |
-
doc_embeddings = {k: embed(v) for k,v in docs.items()}
|
| 39 |
-
|
| 40 |
-
def search_best_doc(question):
|
| 41 |
-
q_emb = embed(question)
|
| 42 |
-
def cosine(a,b): return np.dot(a,b)/(np.linalg.norm(a)*np.linalg.norm(b))
|
| 43 |
-
best_doc = max(docs, key=lambda k: cosine(q_emb, doc_embeddings[k]))
|
| 44 |
-
return docs[best_doc]
|
| 45 |
-
|
| 46 |
-
# --- 4. Fonction de réponse avec Mistral 7B Instruct ---
|
| 47 |
def respond(
|
| 48 |
message,
|
| 49 |
history: list[dict[str, str]],
|
|
@@ -80,7 +67,7 @@ def respond(
|
|
| 80 |
response += token
|
| 81 |
yield response
|
| 82 |
|
| 83 |
-
# ---
|
| 84 |
chatbot = gr.ChatInterface(
|
| 85 |
respond,
|
| 86 |
type="messages",
|
|
|
|
| 30 |
outputs = steelbert_model(**inputs, output_hidden_states=True)
|
| 31 |
return outputs.hidden_states[-1][:,0,:].cpu().numpy()[0]
|
| 32 |
|
| 33 |
+
# --- 3. Fonction de réponse avec Mistral 7B Instruct ---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
def respond(
|
| 35 |
message,
|
| 36 |
history: list[dict[str, str]],
|
|
|
|
| 67 |
response += token
|
| 68 |
yield response
|
| 69 |
|
| 70 |
+
# --- 4. Interface Gradio ---
|
| 71 |
chatbot = gr.ChatInterface(
|
| 72 |
respond,
|
| 73 |
type="messages",
|