Spaces:
Sleeping
Sleeping
update avec openai/gpt-oss-20b et SteelBERT
Browse files
app.py
CHANGED
|
@@ -5,9 +5,6 @@ import torch
|
|
| 5 |
import numpy as np
|
| 6 |
import os
|
| 7 |
|
| 8 |
-
hf_token = os.environ.get("HF_TOKEN")
|
| 9 |
-
print("HF_TOKEN =", "present" if hf_token else "absent")
|
| 10 |
-
|
| 11 |
# --- 1. Récupérer le token Hugging Face depuis variable d'environnement ---
|
| 12 |
hf_token = os.environ.get("HF_TOKEN")
|
| 13 |
if hf_token is None:
|
|
@@ -30,7 +27,21 @@ 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 |
def respond(
|
| 35 |
message,
|
| 36 |
history: list[dict[str, str]],
|
|
@@ -40,7 +51,7 @@ def respond(
|
|
| 40 |
top_p,
|
| 41 |
hf_token: gr.OAuthToken,
|
| 42 |
):
|
| 43 |
-
client = InferenceClient(token=hf_token.token, model="
|
| 44 |
|
| 45 |
# Récupérer le contexte pertinent avec SteelBERT
|
| 46 |
best_doc = search_best_doc(message)
|
|
@@ -67,7 +78,7 @@ def respond(
|
|
| 67 |
response += token
|
| 68 |
yield response
|
| 69 |
|
| 70 |
-
# ---
|
| 71 |
chatbot = gr.ChatInterface(
|
| 72 |
respond,
|
| 73 |
type="messages",
|
|
|
|
| 5 |
import numpy as np
|
| 6 |
import os
|
| 7 |
|
|
|
|
|
|
|
|
|
|
| 8 |
# --- 1. Récupérer le token Hugging Face depuis variable d'environnement ---
|
| 9 |
hf_token = os.environ.get("HF_TOKEN")
|
| 10 |
if hf_token is None:
|
|
|
|
| 27 |
outputs = steelbert_model(**inputs, output_hidden_states=True)
|
| 28 |
return outputs.hidden_states[-1][:,0,:].cpu().numpy()[0]
|
| 29 |
|
| 30 |
+
# --- 3. Base documentaire pour la métallurgie ---
|
| 31 |
+
# Remplacer par vos documents techniques
|
| 32 |
+
docs = {
|
| 33 |
+
"doc1": "L’acier X42 a une résistance à la traction de 415 MPa.",
|
| 34 |
+
"doc2": "L’acier inoxydable 304 est résistant à la corrosion et à l’oxydation."
|
| 35 |
+
}
|
| 36 |
+
doc_embeddings = {k: embed(v) for k,v in docs.items()}
|
| 37 |
+
|
| 38 |
+
def search_best_doc(question):
|
| 39 |
+
q_emb = embed(question)
|
| 40 |
+
def cosine(a,b): return np.dot(a,b)/(np.linalg.norm(a)*np.linalg.norm(b))
|
| 41 |
+
best_doc = max(docs, key=lambda k: cosine(q_emb, doc_embeddings[k]))
|
| 42 |
+
return docs[best_doc]
|
| 43 |
+
|
| 44 |
+
# --- 4. Fonction de réponse avec GPT-OSS-20B ---
|
| 45 |
def respond(
|
| 46 |
message,
|
| 47 |
history: list[dict[str, str]],
|
|
|
|
| 51 |
top_p,
|
| 52 |
hf_token: gr.OAuthToken,
|
| 53 |
):
|
| 54 |
+
client = InferenceClient(token=hf_token.token, model="openai/gpt-oss-20b")
|
| 55 |
|
| 56 |
# Récupérer le contexte pertinent avec SteelBERT
|
| 57 |
best_doc = search_best_doc(message)
|
|
|
|
| 78 |
response += token
|
| 79 |
yield response
|
| 80 |
|
| 81 |
+
# --- 5. Interface Gradio ---
|
| 82 |
chatbot = gr.ChatInterface(
|
| 83 |
respond,
|
| 84 |
type="messages",
|