Spaces:
Runtime error
Runtime error
Commit ·
0416bf5
1
Parent(s): 263dedb
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from llama_index import SimpleDirectoryReader, GPTSimpleVectorIndex, LLMPredictor, ServiceContext
|
| 2 |
+
from langchain import OpenAI
|
| 3 |
+
import gradio as gr
|
| 4 |
+
import os
|
| 5 |
+
|
| 6 |
+
os.environ["OPENAI_API_KEY"] = ''
|
| 7 |
+
|
| 8 |
+
def construct_index(directory_path):
|
| 9 |
+
num_outputs = 512
|
| 10 |
+
|
| 11 |
+
llm_predictor = LLMPredictor(llm=OpenAI(temperature=0.7, model_name="text-davinci-003", max_tokens=num_outputs))
|
| 12 |
+
|
| 13 |
+
service_context = ServiceContext.from_defaults(llm_predictor=llm_predictor)
|
| 14 |
+
|
| 15 |
+
docs = SimpleDirectoryReader(directory_path).load_data()
|
| 16 |
+
|
| 17 |
+
index = GPTSimpleVectorIndex.from_documents(docs, service_context=service_context)
|
| 18 |
+
|
| 19 |
+
index.save_to_disk('index.json')
|
| 20 |
+
|
| 21 |
+
return index
|
| 22 |
+
|
| 23 |
+
def chatbot(input_text):
|
| 24 |
+
index = GPTSimpleVectorIndex.load_from_disk('index.json')
|
| 25 |
+
response = index.query(input_text, response_mode="compact")
|
| 26 |
+
return response.response
|
| 27 |
+
|
| 28 |
+
iface = gr.Interface(fn=chatbot,
|
| 29 |
+
inputs=gr.inputs.Textbox(lines=7, label="Enter your text"),
|
| 30 |
+
outputs="text",
|
| 31 |
+
title="Custom-trained AI Chatbot")
|
| 32 |
+
|
| 33 |
+
index = construct_index("docs")
|
| 34 |
+
iface.launch(share=True)
|