Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,20 +2,47 @@ import os
|
|
| 2 |
import streamlit as st
|
| 3 |
from openai import OpenAI
|
| 4 |
from tavily import TavilyClient
|
| 5 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
from llama_index.llms.openai import OpenAI as LlamaOpenAI
|
| 7 |
from llama_index.embeddings.openai import OpenAIEmbedding
|
| 8 |
from llama_index.core.prompts import PromptTemplate
|
| 9 |
import time
|
| 10 |
|
| 11 |
-
# ----------
|
| 12 |
-
st.
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
# ---------- ENHANCED STYLING ----------
|
| 21 |
st.markdown(
|
|
@@ -726,21 +753,6 @@ Answer:"""
|
|
| 726 |
FOOTER_MESSAGE = "\n\n---\n*For detailed analysis and information, visit [trustlogic.center](https://trustlogic.center) for comprehensive copy generation and brand analysis.*"
|
| 727 |
|
| 728 |
# ---------- LOAD KNOWLEDGE BASE ----------
|
| 729 |
-
@st.cache_resource
|
| 730 |
-
def load_index():
|
| 731 |
-
if os.path.exists("./storage"):
|
| 732 |
-
storage_context = StorageContext.from_defaults(persist_dir="./storage")
|
| 733 |
-
return load_index_from_storage(storage_context)
|
| 734 |
-
|
| 735 |
-
docs = SimpleDirectoryReader(input_files=["time_to_rethink_trust_book (3).md"]).load_data()
|
| 736 |
-
embed_model = OpenAIEmbedding(model="text-embedding-3-small")
|
| 737 |
-
service_context = ServiceContext.from_defaults(
|
| 738 |
-
llm=LlamaOpenAI(model="gpt-4-turbo"),
|
| 739 |
-
embed_model=embed_model
|
| 740 |
-
)
|
| 741 |
-
index = VectorStoreIndex.from_documents(docs, service_context=service_context)
|
| 742 |
-
index.storage_context.persist(persist_dir="./storage")
|
| 743 |
-
return index
|
| 744 |
|
| 745 |
# ---------- INITIALIZE SERVICES ----------
|
| 746 |
index = load_index()
|
|
|
|
| 2 |
import streamlit as st
|
| 3 |
from openai import OpenAI
|
| 4 |
from tavily import TavilyClient
|
| 5 |
+
|
| 6 |
+
# llama-index imports - removed ServiceContext usage
|
| 7 |
+
from llama_index.core import (
|
| 8 |
+
VectorStoreIndex,
|
| 9 |
+
SimpleDirectoryReader,
|
| 10 |
+
StorageContext,
|
| 11 |
+
load_index_from_storage,
|
| 12 |
+
)
|
| 13 |
from llama_index.llms.openai import OpenAI as LlamaOpenAI
|
| 14 |
from llama_index.embeddings.openai import OpenAIEmbedding
|
| 15 |
from llama_index.core.prompts import PromptTemplate
|
| 16 |
import time
|
| 17 |
|
| 18 |
+
# ---------- LOAD KNOWLEDGE BASE (fixed) ----------
|
| 19 |
+
@st.cache_resource
|
| 20 |
+
def load_index():
|
| 21 |
+
"""
|
| 22 |
+
Loads an existing index from ./storage if present.
|
| 23 |
+
If not present, builds a new VectorStoreIndex from the provided markdown file,
|
| 24 |
+
using explicit llm and embed_model parameters (no ServiceContext).
|
| 25 |
+
"""
|
| 26 |
+
persist_dir = "./storage"
|
| 27 |
+
|
| 28 |
+
if os.path.exists(persist_dir):
|
| 29 |
+
# Load existing storage context and index (no service_context required)
|
| 30 |
+
storage_context = StorageContext.from_defaults(persist_dir=persist_dir)
|
| 31 |
+
return load_index_from_storage(storage_context)
|
| 32 |
|
| 33 |
+
# Else build index from docs
|
| 34 |
+
docs = SimpleDirectoryReader(input_files=["time_to_rethink_trust_book (3).md"]).load_data()
|
| 35 |
+
|
| 36 |
+
# Create embedding & LLM objects explicitly
|
| 37 |
+
embed_model = OpenAIEmbedding(model="text-embedding-3-small")
|
| 38 |
+
llm = LlamaOpenAI(model="gpt-4-turbo")
|
| 39 |
+
|
| 40 |
+
# Pass llm and embed_model explicitly to avoid global ServiceContext usage
|
| 41 |
+
index = VectorStoreIndex.from_documents(docs, llm=llm, embed_model=embed_model)
|
| 42 |
+
|
| 43 |
+
# persist storage
|
| 44 |
+
index.storage_context.persist(persist_dir=persist_dir)
|
| 45 |
+
return index
|
| 46 |
|
| 47 |
# ---------- ENHANCED STYLING ----------
|
| 48 |
st.markdown(
|
|
|
|
| 753 |
FOOTER_MESSAGE = "\n\n---\n*For detailed analysis and information, visit [trustlogic.center](https://trustlogic.center) for comprehensive copy generation and brand analysis.*"
|
| 754 |
|
| 755 |
# ---------- LOAD KNOWLEDGE BASE ----------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 756 |
|
| 757 |
# ---------- INITIALIZE SERVICES ----------
|
| 758 |
index = load_index()
|