bsh-api / app.py
sds-bosch's picture
Adding files
2ee74e7
raw
history blame
1.71 kB
from llama_index import SimpleDirectoryReader, ServiceContext, VectorStoreIndex, set_global_service_context, \
StorageContext, load_index_from_storage
from llama_index.llms import OpenAI
import gradio as gr
service_context = ServiceContext.from_defaults(
llm=OpenAI(model="gpt-3.5-turbo-0613")
)
set_global_service_context(service_context)
def construct_index(directory_path):
docs = SimpleDirectoryReader(directory_path).load_data()
index = VectorStoreIndex.from_documents(docs, service_context=service_context)
index.set_index_id("vector_index")
index.storage_context.persist("./storage")
return index
def chatbot(input_text):
storage_context = StorageContext.from_defaults(persist_dir="storage")
# load index
index = load_index_from_storage(storage_context, index_id="vector_index")
query_engine = index.as_query_engine()
response = query_engine.query(input_text)
return response.response
iface = gr.Interface(fn=chatbot,
inputs=gr.components.Textbox(lines=7, label="Ask a question!"),
outputs="text",
title="Bosch - Home Appliances ",
description = "Effortless living with Bosch Home appliances chatbot - troubleshoot, maintain, and get answers with ease.",
css=".main {background-image: url('https://media3.bosch-home.com/Images/3200x/21473378_Washing_Machine_Product.webp'); background-size: cover;} .header {background-image: url('https://upload.wikimedia.org/wikipedia/commons/c/c3/Bosch_logo.png'); background-repeat: no-repeat; background-position: center; height: 200px;}",
)
index = construct_index("docs")
iface.launch(share=False)