Spaces:
Paused
Paused
dtrejopizzo
commited on
Commit
·
5669f53
1
Parent(s):
70b83c6
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from llama_index import SimpleDirectoryReader, LLMPredictor, PromptHelper, StorageContext, ServiceContext, GPTVectorStoreIndex, load_index_from_storage
|
2 |
+
from langchain.chat_models import ChatOpenAI
|
3 |
+
import gradio as gr
|
4 |
+
import sys
|
5 |
+
import os
|
6 |
+
|
7 |
+
#from langchain.chat_models import ChatOpenAI
|
8 |
+
|
9 |
+
os.environ["OPENAI_API_KEY"] = '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'
|
10 |
+
|
11 |
+
def construct_index(directory_path):
|
12 |
+
max_input_size = 4096
|
13 |
+
num_outputs = 512
|
14 |
+
max_chunk_overlap = 0.2
|
15 |
+
chunk_size_limit = 600
|
16 |
+
|
17 |
+
prompt_helper = PromptHelper(max_input_size, num_outputs, max_chunk_overlap, chunk_size_limit=chunk_size_limit)
|
18 |
+
|
19 |
+
llm_predictor = LLMPredictor(llm=ChatOpenAI(temperature=0.7, model_name="gpt-3.5-turbo", max_tokens=num_outputs))
|
20 |
+
|
21 |
+
documents = SimpleDirectoryReader(directory_path).load_data()
|
22 |
+
|
23 |
+
index = GPTVectorStoreIndex(documents, llm_predictor=llm_predictor, prompt_helper=prompt_helper)
|
24 |
+
|
25 |
+
#index.save_to_disk('index.json')
|
26 |
+
index.storage_context.persist(persist_dir="index.json")
|
27 |
+
|
28 |
+
return index
|
29 |
+
|
30 |
+
index = construct_index("docs")
|
31 |
+
|
32 |
+
def chatbot(input_text):
|
33 |
+
query_engine = index.as_query_engine()
|
34 |
+
response = query_engine.query(input_text)
|
35 |
+
return response.response
|
36 |
+
|
37 |
+
iface = gr.Interface(fn=chatbot,
|
38 |
+
inputs=gr.components.Textbox(lines=7, label="Enter your question"),
|
39 |
+
outputs="text",
|
40 |
+
title="Starkbot")
|
41 |
+
|
42 |
+
|
43 |
+
iface.launch(share=True, debug=True)
|