Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import logging
|
| 2 |
+
import sys
|
| 3 |
+
import gradio as gr
|
| 4 |
+
from llama_index import GPTVectorStoreIndex, SimpleDirectoryReader, ServiceContext
|
| 5 |
+
from llama_index import OpenAIEmbedding
|
| 6 |
+
|
| 7 |
+
logging.basicConfig(stream=sys.stdout, level=logging.INFO)
|
| 8 |
+
logging.getLogger().addHandler(logging.StreamHandler(stream=sys.stdout))
|
| 9 |
+
os.environ["OPENAI_API_KEY"] = os.getenv("OPENAI_API_KEY")
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
def load_data():
|
| 13 |
+
return SimpleDirectoryReader("./document/management/").load_data()
|
| 14 |
+
|
| 15 |
+
def build_index(documents):
|
| 16 |
+
return GPTVectorStoreIndex.from_documents(documents)
|
| 17 |
+
|
| 18 |
+
def query_index(index, similarity_top_k=3, streaming=True):
|
| 19 |
+
query_engine = index.as_query_engine(
|
| 20 |
+
similarity_top_k=similarity_top_k,
|
| 21 |
+
streaming=streaming
|
| 22 |
+
)
|
| 23 |
+
return query_engine # return the query engine instance
|
| 24 |
+
|
| 25 |
+
data = load_data()
|
| 26 |
+
index = build_index(data)
|
| 27 |
+
query_engine = query_index(index) # initialize the query engine
|
| 28 |
+
|
| 29 |
+
def get_response(text,history=None):
|
| 30 |
+
# Use the initialized query engine to perform the query
|
| 31 |
+
response = str(query_engine.query(text))
|
| 32 |
+
return response
|
| 33 |
+
|
| 34 |
+
t = gr.ChatInterface(get_response, analytics_enabled=True)
|
| 35 |
+
t.launch(debug=True, share=True)
|