Create frontend.py
Browse files- frontend.py +46 -0
frontend.py
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# frontend.py
|
| 2 |
+
import os
|
| 3 |
+
import requests
|
| 4 |
+
import streamlit as st
|
| 5 |
+
|
| 6 |
+
API_URL = os.getenv("API_URL", "http://localhost:8000")
|
| 7 |
+
|
| 8 |
+
st.set_page_config(page_title="StackOverflow Tagger", layout="wide")
|
| 9 |
+
st.title("🔖 StackOverflow Tag Predictor")
|
| 10 |
+
|
| 11 |
+
st.write(
|
| 12 |
+
"Entrez une question (titre + description éventuellement) et récupérez "
|
| 13 |
+
"les probabilités des tags prédits par le modèle."
|
| 14 |
+
)
|
| 15 |
+
|
| 16 |
+
question = st.text_area("Question StackOverflow", height=200, placeholder="Ex: How to fine-tune BERT for multi-label classification?")
|
| 17 |
+
top_k = st.slider("Nombre de tags à afficher (top_k)", min_value=1, max_value=20, value=5)
|
| 18 |
+
|
| 19 |
+
if st.button("Prédire") and question.strip():
|
| 20 |
+
try:
|
| 21 |
+
with st.spinner("Prédiction en cours..."):
|
| 22 |
+
resp = requests.post(
|
| 23 |
+
f"{API_URL}/predict",
|
| 24 |
+
json={"text": question, "top_k": top_k},
|
| 25 |
+
timeout=60,
|
| 26 |
+
)
|
| 27 |
+
resp.raise_for_status()
|
| 28 |
+
data = resp.json()
|
| 29 |
+
tags = data.get("tags", [])
|
| 30 |
+
|
| 31 |
+
if not tags:
|
| 32 |
+
st.warning("Aucun tag renvoyé par l'API.")
|
| 33 |
+
else:
|
| 34 |
+
st.subheader("Résultats")
|
| 35 |
+
for t in tags:
|
| 36 |
+
st.write(f"- **{t['label']}** — probabilité : `{t['score']:.4f}`")
|
| 37 |
+
|
| 38 |
+
# Petit graphe des probas
|
| 39 |
+
st.subheader("Distribution des probabilités (top_k)")
|
| 40 |
+
scores = {t["label"]: t["score"] for t in tags}
|
| 41 |
+
st.bar_chart(scores)
|
| 42 |
+
|
| 43 |
+
except Exception as e:
|
| 44 |
+
st.error(f"Erreur lors de l'appel à l'API : {e}")
|
| 45 |
+
elif st.button("Prédire") and not question.strip():
|
| 46 |
+
st.warning("Merci d'entrer une question.")
|