Spaces:
Sleeping
Sleeping
fix caching
Browse files
app.py
CHANGED
@@ -91,16 +91,19 @@ embeddings = HuggingFaceEmbeddings(model_name="LazarusNLP/all-indobert-base-v2")
|
|
91 |
cache_URL = ""
|
92 |
db = None
|
93 |
qa = None
|
|
|
94 |
|
95 |
|
96 |
async def generate(URL, query):
|
97 |
-
global cache_URL, db, qa
|
|
|
98 |
if URL == "" or query == "":
|
99 |
return "Empty input"
|
100 |
else:
|
101 |
try:
|
102 |
product_id = get_product_id(URL)
|
103 |
-
|
|
|
104 |
# Get reviews
|
105 |
try:
|
106 |
reviews = scrape(product_id)
|
@@ -119,18 +122,22 @@ async def generate(URL, query):
|
|
119 |
chunk_size=1000, chunk_overlap=50
|
120 |
)
|
121 |
docs = text_splitter.split_documents(documents)
|
122 |
-
|
123 |
-
cache_URL = URL
|
124 |
# Vector store
|
125 |
db = FAISS.from_documents(docs, embeddings)
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
|
|
|
|
|
|
|
|
|
|
134 |
except:
|
135 |
return "URL tidak valid"
|
136 |
|
|
|
91 |
cache_URL = ""
|
92 |
db = None
|
93 |
qa = None
|
94 |
+
cache = {}
|
95 |
|
96 |
|
97 |
async def generate(URL, query):
|
98 |
+
global cache_URL, db, qa, cache
|
99 |
+
|
100 |
if URL == "" or query == "":
|
101 |
return "Empty input"
|
102 |
else:
|
103 |
try:
|
104 |
product_id = get_product_id(URL)
|
105 |
+
|
106 |
+
if URL not in cache:
|
107 |
# Get reviews
|
108 |
try:
|
109 |
reviews = scrape(product_id)
|
|
|
122 |
chunk_size=1000, chunk_overlap=50
|
123 |
)
|
124 |
docs = text_splitter.split_documents(documents)
|
125 |
+
|
|
|
126 |
# Vector store
|
127 |
db = FAISS.from_documents(docs, embeddings)
|
128 |
+
|
129 |
+
# Store in cache
|
130 |
+
cache[URL] = (docs, db)
|
131 |
+
|
132 |
+
# Retrieve from cache
|
133 |
+
docs, db = cache[URL]
|
134 |
+
|
135 |
+
# Chain to answer questions
|
136 |
+
qa = RetrievalQA.from_chain_type(llm=llm, retriever=db.as_retriever())
|
137 |
+
res = await qa.ainvoke(query)
|
138 |
+
|
139 |
+
# Process result
|
140 |
+
return res["result"]
|
141 |
except:
|
142 |
return "URL tidak valid"
|
143 |
|