Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
@@ -1,44 +1,41 @@
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
-
|
3 |
-
import
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
gr.inputs.Textbox(label="Input Text"),
|
41 |
-
],
|
42 |
-
outputs=gr.inputs.Textbox(label="Generated Text"),
|
43 |
-
title="Falcon-7B Instruct",
|
44 |
-
).launch()
|
|
|
1 |
+
from gpt_index import SimpleDirectoryReader, GPTListIndex, GPTSimpleVectorIndex, LLMPredictor, PromptHelper
|
2 |
+
from langchain.chat_models import ChatOpenAI
|
3 |
import gradio as gr
|
4 |
+
import sys
|
5 |
+
import os
|
6 |
+
import openai
|
7 |
+
|
8 |
+
os.environ["OPENAI_API_KEY"] = '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'
|
9 |
+
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 = 20
|
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 = GPTSimpleVectorIndex(documents, llm_predictor=llm_predictor, prompt_helper=prompt_helper)
|
24 |
+
|
25 |
+
index.save_to_disk('index.json')
|
26 |
+
|
27 |
+
return index
|
28 |
+
|
29 |
+
|
30 |
+
def chatbot(input_text):
|
31 |
+
index = GPTSimpleVectorIndex.load_from_disk('index.json')
|
32 |
+
response = index.query(input_text, response_mode="compact")
|
33 |
+
return response.response
|
34 |
+
|
35 |
+
iface = gr.Interface(fn=chatbot,
|
36 |
+
inputs=gr.components.Textbox(lines=7, label="Enter your text"),
|
37 |
+
outputs="text",
|
38 |
+
title="Custom-trained AI Chatbot")
|
39 |
+
|
40 |
+
index = construct_index("docs")
|
41 |
+
iface.launch(share=True)
|
|
|
|
|
|
|
|
|
|