import gradio as gr import os from langchain_openai import OpenAIEmbeddings from langchain_community.document_loaders import TextLoader from langchain_openai import ChatOpenAI from langchain.text_splitter import RecursiveCharacterTextSplitter from langchain_community.vectorstores import FAISS from langchain.chains import RetrievalQA from langchain.chains import ConversationalRetrievalChain OpenAIModel = "gpt-3.0" OPENAI_API_KEY = os.environ["OPENAI_API_KEY"] llm = ChatOpenAI(model=OpenAIModel, temperature=0.1, openai_api_key=OPENAI_API_KEY) def ask(text): answer = qa.run(text) return answer loader = TextLoader("test.txt") data = loader.load() embeddings = OpenAIEmbeddings(openai_api_key=OPENAI_API_KEY) text_splitter = RecursiveCharacterTextSplitter(chunk_size=100, chunk_overlap=50) all_splits = text_splitter.split_documents(data) db2 = FAISS.from_documents(all_splits, embeddings) qa = RetrievalQA.from_chain_type(llm=llm, retriever=db2.as_retriever()) iface = gr.Interface(ask,gr.Textbox(label="Question"),gr.Textbox(label="Answer"), title="BiMah Customer Service Chatbot",description="A chatbot that can answer things related to BiMah (Bimbel Mahasiswa)", examples=["How BiMah can enforce students to be better?","Siapa CEO BiMah?", "Bagaimana langkah-langkah pendaftaran di BiMah?"]) iface.launch()