Spaces:
Runtime error
Runtime error
Create OllamaHistory.py
Browse files- pages/OllamaHistory.py +63 -0
pages/OllamaHistory.py
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import sys, os, json, glob, hashlib
|
| 3 |
+
|
| 4 |
+
# ─── Ensure utils/ is importable ──────────────────────────────
|
| 5 |
+
UTILS_PATH = os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))
|
| 6 |
+
if UTILS_PATH not in sys.path:
|
| 7 |
+
sys.path.append(UTILS_PATH)
|
| 8 |
+
|
| 9 |
+
from utils.summarizer import summarize_text
|
| 10 |
+
|
| 11 |
+
st.title("🧠 Ollama Conversation History")
|
| 12 |
+
st.write("Ingest and digest local Ollama conversation blobs for analysis.")
|
| 13 |
+
|
| 14 |
+
# Init session state
|
| 15 |
+
if "ollama_history" not in st.session_state:
|
| 16 |
+
st.session_state.ollama_history = []
|
| 17 |
+
|
| 18 |
+
# Default location (adjust if needed)
|
| 19 |
+
OLLAMA_HISTORY_DIR = os.path.expanduser("~/.ollama/history")
|
| 20 |
+
|
| 21 |
+
def load_conversations():
|
| 22 |
+
conversations = []
|
| 23 |
+
if not os.path.exists(OLLAMA_HISTORY_DIR):
|
| 24 |
+
st.warning(f"No Ollama history found at {OLLAMA_HISTORY_DIR}")
|
| 25 |
+
return []
|
| 26 |
+
|
| 27 |
+
files = glob.glob(os.path.join(OLLAMA_HISTORY_DIR, "*.json"))
|
| 28 |
+
for f in files:
|
| 29 |
+
try:
|
| 30 |
+
with open(f, "r", errors="ignore") as fh:
|
| 31 |
+
data = json.load(fh)
|
| 32 |
+
|
| 33 |
+
# Extract conversation summary
|
| 34 |
+
convo_text = ""
|
| 35 |
+
for msg in data.get("messages", []):
|
| 36 |
+
role = msg.get("role", "unknown")
|
| 37 |
+
content = msg.get("content", "")
|
| 38 |
+
convo_text += f"{role.upper()}: {content}\n"
|
| 39 |
+
|
| 40 |
+
sha1 = hashlib.sha1(convo_text.encode()).hexdigest()
|
| 41 |
+
summary = summarize_text(convo_text)
|
| 42 |
+
|
| 43 |
+
conversations.append({
|
| 44 |
+
"file": os.path.basename(f),
|
| 45 |
+
"sha1": sha1,
|
| 46 |
+
"preview": convo_text[:500],
|
| 47 |
+
"summary": summary,
|
| 48 |
+
})
|
| 49 |
+
except Exception as e:
|
| 50 |
+
st.error(f"⚠️ Error reading {f}: {e}")
|
| 51 |
+
|
| 52 |
+
return conversations
|
| 53 |
+
|
| 54 |
+
# UI
|
| 55 |
+
if st.button("📥 Load Ollama Conversations"):
|
| 56 |
+
st.session_state.ollama_history = load_conversations()
|
| 57 |
+
st.success(f"✅ Loaded {len(st.session_state.ollama_history)} conversations.")
|
| 58 |
+
|
| 59 |
+
if st.session_state.ollama_history:
|
| 60 |
+
for c in st.session_state.ollama_history:
|
| 61 |
+
st.subheader(f"💬 {c['file']} ({c['sha1'][:8]})")
|
| 62 |
+
st.text_area("Preview", c["preview"], height=150)
|
| 63 |
+
st.write("🧠 Summary:", c["summary"])
|