Upload Deploy_AskLAQ3.py
Browse files- deploy/Deploy_AskLAQ3.py +46 -0
deploy/Deploy_AskLAQ3.py
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os, torch, pandas as pd, gradio as gr, uvicorn, nest_asyncio
|
| 2 |
+
from flask import Flask, render_template, request, jsonify
|
| 3 |
+
from sentence_transformers import SentenceTransformer, util
|
| 4 |
+
from fastapi import FastAPI
|
| 5 |
+
from fastapi.middleware.wsgi import WSGIMiddleware
|
| 6 |
+
|
| 7 |
+
# Configuration
|
| 8 |
+
BASE_DIR = os.path.abspath(os.path.dirname(__file__))
|
| 9 |
+
flask_app = Flask(__name__,
|
| 10 |
+
template_folder=os.path.join(BASE_DIR, "templates"),
|
| 11 |
+
static_folder=os.path.join(BASE_DIR, "static"))
|
| 12 |
+
|
| 13 |
+
# Chargement IA
|
| 14 |
+
model = SentenceTransformer("OrdalieTech/Solon-embeddings-mini-beta-1.1", device="cpu", trust_remote_code=True)
|
| 15 |
+
|
| 16 |
+
@flask_app.route("/")
|
| 17 |
+
def index():
|
| 18 |
+
return render_template("index.html")
|
| 19 |
+
|
| 20 |
+
@flask_app.route("/ask", methods=["POST"])
|
| 21 |
+
def ask():
|
| 22 |
+
try:
|
| 23 |
+
data = request.get_json()
|
| 24 |
+
question = data.get("question", "")
|
| 25 |
+
|
| 26 |
+
df = pd.read_csv("dataset_2026.csv")
|
| 27 |
+
emb_base = torch.load("embeddings_questions.pt", map_location="cpu")
|
| 28 |
+
|
| 29 |
+
emb_q = model.encode(question, convert_to_tensor=True, normalize_embeddings=True)
|
| 30 |
+
scores = util.pytorch_cos_sim(emb_q, emb_base)[0]
|
| 31 |
+
idx = torch.argmax(scores).item()
|
| 32 |
+
|
| 33 |
+
return jsonify({
|
| 34 |
+
"response": df["rationale"].iloc[idx],
|
| 35 |
+
"confidence": int(scores[idx].item() * 100)
|
| 36 |
+
})
|
| 37 |
+
except Exception as e:
|
| 38 |
+
return jsonify({"error": str(e)})
|
| 39 |
+
|
| 40 |
+
# Montage FastAPI (pour Hugging Face)
|
| 41 |
+
app = FastAPI()
|
| 42 |
+
app.mount("/", WSGIMiddleware(flask_app))
|
| 43 |
+
|
| 44 |
+
if __name__ == "__main__":
|
| 45 |
+
nest_asyncio.apply()
|
| 46 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|