import streamlit as st import requests import pandas as pd import html import json # ========================= # 🔗 API & Sheet URLs # ========================= # The FastAPI endpoint API_URL = "https://mazen248-telecom-rag-fastapi.hf.space/ask" # The Google Sheet CSV export link SHEET_URL = "https://docs.google.com/spreadsheets/d/1qgwJYpHE6ehE242_CUC9-iH0SLDK-VPKtp1VDIp4Rqs/export?format=csv&gid=0" st.set_page_config(page_title="NileTel Assistant", layout="wide", page_icon="📡") if "last_response" not in st.session_state: st.session_state.last_response = None st.session_state.last_query = "" st.session_state.ticket_name = "" if "last_page" not in st.session_state: st.session_state.last_page = "" if "tickets_refresh" not in st.session_state: st.session_state.tickets_refresh = 0 # Injecting Custom CSS for a VERY GOOD UI st.markdown( """ """, unsafe_allow_html=True ) # ========================= # 🧭 SIDEBAR NAVIGATION # ========================= with st.sidebar: st.markdown( """ """, unsafe_allow_html=True ) st.markdown("## Navigation") page = st.radio( "Choose a View", ["💬 Chat Assistant", "🎫 Tickets Table"], label_visibility="collapsed" ) st.markdown("---") st.markdown( """ """, unsafe_allow_html=True ) if page == "🎫 Tickets Table" and st.session_state.last_page != page: st.session_state.tickets_refresh += 1 st.session_state.last_page = page # ========================= # 🌟 MAIN VIEW ROUTING # ========================= if page == "💬 Chat Assistant": st.markdown( """
NileTel AI Assistant
Experience next-generation telecom support. Smart routing, instant hybrid-RAG answers, and automated ticket dispatch.
🤖 Llama 3.1 8B
⚡ Hybrid Search
🌍 Arabic & English
""", unsafe_allow_html=True ) st.markdown("### 💬 Ask the AI") st.markdown("

Type your inquiry in Arabic or English. The AI will respond in a beautifully formatted RTL format.

", unsafe_allow_html=True) col_btn, col_input = st.columns([1, 4]) with col_input: query = st.text_input("Your question...", placeholder="مثال: النت فاصل عندي، ممكن أعمل تذكرة؟", label_visibility="collapsed") with col_btn: send_clicked = st.button("🚀 Send", use_container_width=True) if send_clicked: if query.strip(): st.session_state.ticket_name = "" st.session_state.last_response = None st.session_state.last_query = query.strip() with st.spinner("AI is thinking..."): try: response = requests.post( API_URL, json={ "query": st.session_state.last_query } ) if response.status_code == 200: st.session_state.last_response = response.json() else: st.error(f"API Error {response.status_code}: Could not fetch answer.") except Exception as e: st.error(f"Connection Failed: Ensure the FastAPI server is running. Error: {e}") else: st.warning("Please enter a question to get started.") if st.session_state.last_response: data = st.session_state.last_response answer_text = data.get("answer", "") needs_action = data.get("needs_action", "NO") sources = data.get("sources", []) render_answer = True if needs_action == "YES" and not st.session_state.ticket_name: render_answer = False st.markdown("### Provide your name to create the ticket") col_ticket_btn, col_ticket_input = st.columns([1, 4]) with col_ticket_input: ticket_name = st.text_input("Name for ticket", key="ticket_name_input", placeholder="الاسم...", label_visibility="collapsed") with col_ticket_btn: submit_name = st.button("Submit", key="submit_ticket_name", use_container_width=True) if submit_name: if ticket_name.strip(): with st.spinner("Submitting ticket..."): try: response = requests.post( API_URL, json={ "query": st.session_state.last_query, "name": ticket_name.strip() } ) if response.status_code == 200: st.session_state.ticket_name = ticket_name.strip() st.session_state.last_response = response.json() data = st.session_state.last_response answer_text = data.get("answer", "") needs_action = data.get("needs_action", "NO") sources = data.get("sources", []) render_answer = True st.success("Ticket request sent with your name.") else: st.error(f"API Error {response.status_code}: Could not submit ticket.") except Exception as e: st.error(f"Ticket submit failed. Error: {e}") else: st.warning("Please enter your name to continue.") if render_answer: # Convert newlines to HTML breaks answer_html = answer_text.replace("\n", "
") # Render Answer Block st.markdown( f"""
{answer_html}
""", unsafe_allow_html=True ) # Render Action Badge if needs_action == "YES": st.markdown("
⚠️ Action Triggered (Ticket/Engineer)
", unsafe_allow_html=True) else: st.markdown("
✅ No Action Required
", unsafe_allow_html=True) # Render Sources if sources: chips_html = "".join([f"📄 {html.escape(str(s))}" for s in sources]) st.markdown(f"
{chips_html}
", unsafe_allow_html=True) elif page == "🎫 Tickets Table": st.markdown("### 🎫 Tickets Table") st.markdown("

View and manage all telecom support tickets fetched live from the central system.

", unsafe_allow_html=True) def _load_tickets(url, refresh_token): return pd.read_csv(url) with st.spinner("Loading tickets from live database..."): try: df = _load_tickets(SHEET_URL, st.session_state.tickets_refresh) if df.empty: st.info("No tickets available at the moment.") else: st.success(f"Successfully loaded {len(df)} tickets.") st.dataframe( df, use_container_width=True, hide_index=True, height=600 ) except Exception as e: st.error(f"Failed to load tickets. Error: {e}")