File size: 1,809 Bytes
28149c3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import openai, os
openai.api_key = "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"
# openai.api_key = os.environ["OPENAI_API_KEY"]
from llama_index.core import SimpleDirectoryReader

documents = SimpleDirectoryReader(
    input_files = ["./HFNWAY.pdf"]
).load_data()

from llama_index.core.schema import Document
document = Document(text = "\n\n".join([doc.text for doc in documents]))
# above step merges it the 218 different documents into 1 document

from llama_index.core import VectorStoreIndex
from llama_index.core import ServiceContext
from llama_index.llms.openai import OpenAI

# resp = OpenAI().complete("Lord Ram is ")
# print(resp)
# does not need api key
# the embedding model we use is hugging face  bge small
# the service context class already hass this

llm = OpenAI(model= "gpt-3.5-turbo", temperature=0.2)
service_context = ServiceContext.from_defaults(
    llm = llm, embed_model = "local:BAAI/bge-small-en-v1.5"
)
# https://huggingface.co/BAAI/bge-small-en
index = VectorStoreIndex.from_documents([document], service_context=service_context)
# pip install llama-index-embeddings-huggingface
# this cell takes care of chunking embedding, indexing

query_engine = index.as_query_engine()

import gradio as gr

def chat_interface(message, history):
  
  # Update history with the current message
  # Call the query function with the user's message
  response = str(query_engine.query(message))
  # Update history with the bot's response
  
  history.append([message, response])
  return response


# Initialize an empty history
history = []
# Create the chat interface using gr.ChatInterface
interface = gr.ChatInterface(chat_interface, examples = ["Who is DAAJI?", "What is Heartfulness way of meditation all about?"] , title = "HFN BOT")
# Launch the interface
interface.launch()