import streamlit as st import datetime import requests from streamlit_extras.add_vertical_space import add_vertical_space from streamlit_lottie import st_lottie import streamlit.components.v1 as components # Senin lokal servislerini doğrudan içeri aktarıyoruz (FastAPI veya Render'a gerek yok!) from app.services.nlp_triplet import extract_triplets_from_text from app.services.memory_engine import group_by_author, count_predicates, most_common_subjects from app.services.graph_builder import build_graph_from_triplets from app.services.graph_visualizer import visualize_graph st.set_page_config(page_title="AI Memory Graph", layout="wide", page_icon="🧠") # Lottie Animasyonu Yükleme def load_lottieurl(url: str): r = requests.get(url) if r.status_code != 200: return None return r.json() lottie_ai = load_lottieurl("https://assets9.lottiefiles.com/packages/lf20_touohxv0.json") col1, col2 = st.columns([4, 1]) with col1: st.title("🧠 AI Memory Graph") st.markdown("A visual way to extract and understand multi-user chat memories using NLP + Graphs.") with col2: if lottie_ai: st_lottie(lottie_ai, height=100, key="ai") add_vertical_space(1) # Session State Tanımlamaları if "messages" not in st.session_state: st.session_state["messages"] = [] if "triplets" not in st.session_state: st.session_state["triplets"] = [] # --- SOHBET GİRİŞ ALANI --- st.subheader("💬 Add Chat Messages") with st.form("message_form", clear_on_submit=True): col_sender, col_msg = st.columns([1, 3]) with col_sender: sender = st.text_input("Sender", placeholder="e.g. Erdem") with col_msg: text = st.text_input("Message", placeholder="e.g. I recommend using FastAPI for the backend.") submitted = st.form_submit_button("➕ Add Message") if submitted and sender and text: st.session_state["messages"].append({ "sender": sender, "text": text, "timestamp": datetime.datetime.utcnow().isoformat() }) st.success(f"Message added for {sender}!") # Eklenen mesajları göster if st.session_state["messages"]: with st.expander("📑 View Current Messages", expanded=False): st.json(st.session_state["messages"]) add_vertical_space(1) # --- İŞLEM BUTONLARI --- c1, c2, c3 = st.columns(3) # 1. Triplet Çıkarma İşlemi with c1: if st.button("🔍 Extract Triplets", use_container_width=True): with st.spinner("Analyzing text with SpaCy Transformer..."): all_triplets = [] for msg in st.session_state["messages"]: extracted = extract_triplets_from_text(msg["text"]) for triplet in extracted: triplet["timestamp"] = msg["timestamp"] triplet["author"] = msg["sender"] all_triplets.append(triplet) st.session_state["triplets"] = all_triplets st.success(f"Extracted {len(all_triplets)} triplets!") st.json(all_triplets) # 2. Hafıza Özeti İşlemi with c2: if st.button("📝 Memory Summary", use_container_width=True): if not st.session_state["triplets"]: st.warning("Please extract triplets first!") else: summary = { "total_triplets": len(st.session_state["triplets"]), "by_user": group_by_author(st.session_state["triplets"]), "predicate_counts": count_predicates(st.session_state["triplets"]), "common_subjects": most_common_subjects(st.session_state["triplets"]) } st.write("### 📊 Stats") st.json(summary) # 3. Grafik Çizdirme İşlemi with c3: if st.button("🌐 Show Knowledge Graph", use_container_width=True): if not st.session_state["triplets"]: st.warning("Please extract triplets first!") else: with st.spinner("Building and rendering graph..."): # Grafiği oluştur G = build_graph_from_triplets(st.session_state["triplets"]) # HTML olarak kaydet (webbrowser.open KESİNLİKLE KAPALI OLMALI) visualize_graph(G, output_path="memory_graph.html") # Kaydedilen HTML'i Streamlit içine göm try: with open("memory_graph.html", "r", encoding="utf-8") as f: html_content = f.read() st.write("### 🕸️ Graph Visualization") components.html(html_content, height=600, scrolling=True) except FileNotFoundError: st.error("Graph HTML file could not be generated.") # Footer add_vertical_space(3) st.markdown("---") st.caption("Developed by Erdem Yavuz Hacisoftaoglu | Powered by SpaCy, NetworkX & Streamlit")