giiift commited on
Commit
4375c72
·
1 Parent(s): 76f03c0

Create demo.py

Browse files

Initial pull demo code

Files changed (1) hide show
  1. demo.py +44 -0
demo.py ADDED
@@ -0,0 +1,44 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from llama_index import LLMPredictor, GPTSimpleVectorIndex, PromptHelper, ServiceContext, SimpleDirectoryReader
2
+ from langchain.chat_models import ChatOpenAI
3
+ import gradio as gr
4
+ import sys
5
+ import os
6
+
7
+
8
+ os.environ["OPENAI_API_KEY"] = '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'
9
+
10
+ def construct_index(directory_path):
11
+ max_input_size = 4096
12
+ num_outputs = 512
13
+ max_chunk_overlap = 20
14
+ chunk_size_limit = 600
15
+
16
+ prompt_helper = PromptHelper(max_input_size, num_outputs, max_chunk_overlap, chunk_size_limit=chunk_size_limit)
17
+
18
+ llm_predictor = LLMPredictor(llm=ChatOpenAI(temperature=0.1, model_name="gpt-3.5-turbo", max_tokens=num_outputs))
19
+
20
+ service_context = ServiceContext.from_defaults(llm_predictor=llm_predictor, prompt_helper=prompt_helper)
21
+
22
+ documents = SimpleDirectoryReader(directory_path).load_data()
23
+
24
+ index = GPTSimpleVectorIndex.from_documents(documents, service_context=service_context)
25
+
26
+ index.save_to_disk('index.json')
27
+
28
+ return index
29
+
30
+ def chatbot(input_text):
31
+ index = GPTSimpleVectorIndex.load_from_disk('index.json')
32
+ pre_prompt = ", return no more than 5 products. And answer me as a passionate comestic saleswoman"
33
+ response = index.query(input_text + pre_prompt , response_mode="default")
34
+ return response.response
35
+
36
+
37
+ iface = gr.Interface(fn=chatbot,
38
+ inputs=gr.components.Textbox(lines=7, label="Describe the good you want"),
39
+ outputs="text",
40
+ title="Your AI Comestic Sales Advisor")
41
+
42
+ # As the index has already been constructed, inactive this line to save embedding tokens
43
+ # index = construct_index("docs")
44
+ iface.launch(share=True)