Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import qdrant_client
|
2 |
+
|
3 |
+
from llama_index.core import VectorStoreIndex, ServiceContext, SimpleDirectoryReader
|
4 |
+
from llama_index.core import load_index_from_storage
|
5 |
+
from llama_index.llms.ollama import Ollama
|
6 |
+
from llama_index.core import StorageContext
|
7 |
+
from llama_index.vector_stores.qdrant import QdrantVectorStore
|
8 |
+
from llama_index.embeddings.huggingface import HuggingFaceEmbedding
|
9 |
+
from llama_index.core import Settings
|
10 |
+
from llama_index.core import set_global_service_context
|
11 |
+
|
12 |
+
import gradio as gr
|
13 |
+
|
14 |
+
DOC_PATH = '/Users/simyinkuan/Documents/rag_llama/ollama-llamaindex-mixtral-python-playground/data/pdf_esg'
|
15 |
+
INDEX_PATH = '//Users/simyinkuan/Documents/rag_llama/ollama-llamaindex-mixtral-python-playground/storage'
|
16 |
+
Settings.llm = Ollama(model="mistral")
|
17 |
+
Settings.embed_model = HuggingFaceEmbedding(model_name="BAAI/bge-small-en-v1.5")
|
18 |
+
embed_model = HuggingFaceEmbedding(model_name="BAAI/bge-small-en-v1.5")
|
19 |
+
service_context = ServiceContext.from_defaults(llm=Ollama(model="mistral"),embed_model = embed_model)
|
20 |
+
set_global_service_context(service_context)
|
21 |
+
|
22 |
+
def construct_index(doc_path=DOC_PATH, index_store=INDEX_PATH, use_cache=False):
|
23 |
+
client = qdrant_client.QdrantClient(path="./qdrant_data")
|
24 |
+
vector_store = QdrantVectorStore(client=client, collection_name="esg")
|
25 |
+
storage_context = StorageContext.from_defaults(vector_store=vector_store)
|
26 |
+
|
27 |
+
if use_cache:
|
28 |
+
# rebuild storage context
|
29 |
+
storage_context = StorageContext.from_defaults(persist_dir=index_store)
|
30 |
+
index = load_index_from_storage(storage_context) # load index
|
31 |
+
else:
|
32 |
+
reader = SimpleDirectoryReader(input_dir="/Users/simyinkuan/Documents/rag_llama/ollama-llamaindex-mixtral-python-playground/data/pdf_esg")
|
33 |
+
documents = reader.load_data()
|
34 |
+
index = VectorStoreIndex.from_documents(documents)
|
35 |
+
index.storage_context.persist(index_store)
|
36 |
+
return None
|
37 |
+
|
38 |
+
def qabot(input_text, index_store = INDEX_PATH):
|
39 |
+
|
40 |
+
|
41 |
+
storage_context = StorageContext.from_defaults(persist_dir=index_store)
|
42 |
+
|
43 |
+
# Load the data
|
44 |
+
index = load_index_from_storage(storage_context)
|
45 |
+
|
46 |
+
query_engine = index.as_query_engine()
|
47 |
+
response = query_engine.query(input_text)
|
48 |
+
return response.response
|
49 |
+
|
50 |
+
if __name__ == "__main__":
|
51 |
+
construct_index(DOC_PATH, use_cache=False)
|
52 |
+
# create_index_retriever_query_engine()
|
53 |
+
iface = gr.Interface(fn=qabot, inputs=gr.Textbox(lines=7, label='Enter your query'),
|
54 |
+
outputs="text",
|
55 |
+
title="ESG Chatbot")
|
56 |
+
iface.launch(share=True)
|