| import streamlit as st |
| import requests |
|
|
| API_URL = "https://missaoui-shopvite-fastapi.hf.space/" |
|
|
| st.set_page_config(page_title="ShopVite Assistant", page_icon="π") |
| st.title("π ShopVite β Assistant FAQ") |
| st.caption("Posez vos questions sur nos produits, livraisons et retours.") |
|
|
| |
| @st.cache_data(ttl=30) |
| def check_health(): |
| try: |
| r = requests.get(f"{API_URL}/health", timeout=5) |
| return r.status_code == 200 |
| except Exception: |
| return False |
|
|
| if not check_health(): |
| st.error("β οΈ API indisponible. VΓ©rifiez que `api.py` est bien lancΓ©.") |
| st.stop() |
|
|
| |
| if "messages" not in st.session_state: |
| st.session_state.messages = [] |
|
|
| for msg in st.session_state.messages: |
| with st.chat_message(msg["role"]): |
| st.markdown(msg["content"]) |
| if msg.get("sources"): |
| with st.expander("π Sources"): |
| for s in msg["sources"]: |
| st.markdown(f"- `{s}`") |
|
|
| |
| CONFIDENCE_BADGE = { |
| "high": "π’ Confiance Γ©levΓ©e", |
| "medium": "π‘ Confiance moyenne", |
| "low": "π΄ Confiance faible", |
| "out_of_context": "β« Hors contexte", |
| } |
|
|
| if question := st.chat_input("Votre question..."): |
| st.session_state.messages.append({"role": "user", "content": question}) |
| with st.chat_message("user"): |
| st.markdown(question) |
|
|
| with st.chat_message("assistant"): |
| with st.spinner("Recherche en cours..."): |
| try: |
| resp = requests.post( |
| f"{API_URL}/ask", |
| json={"question": question}, |
| timeout=30 |
| ) |
| resp.raise_for_status() |
| data = resp.json() |
|
|
| answer = data["answer"] |
| sources = data.get("sources", []) |
| confidence = data.get("confidence", "low") |
|
|
| st.markdown(answer) |
| st.caption(CONFIDENCE_BADGE.get(confidence, "")) |
|
|
| if sources: |
| with st.expander("π Sources"): |
| for s in sources: |
| st.markdown(f"- `{s}`") |
|
|
| st.session_state.messages.append({ |
| "role": "assistant", |
| "content": answer, |
| "sources": sources, |
| }) |
|
|
| except requests.exceptions.Timeout: |
| st.error("β±οΈ L'API met trop de temps Γ rΓ©pondre.") |
| except requests.exceptions.ConnectionError: |
| st.error("π Impossible de joindre l'API.") |
| except Exception as e: |
| st.error(f"β Erreur : {str(e)}") |