Spaces:
Paused
Paused
Upload app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,124 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
os.environ['OPENAI_API_KEY'] = "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"
|
| 3 |
+
import openai
|
| 4 |
+
import json
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
from llama_index import GPTSimpleVectorIndex, LLMPredictor, PromptHelper, ServiceContext, QuestionAnswerPrompt
|
| 9 |
+
from langchain import OpenAI
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
# handling data on space
|
| 13 |
+
|
| 14 |
+
from huggingface_hub import HfFileSystem
|
| 15 |
+
fs = HfFileSystem(token='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')
|
| 16 |
+
|
| 17 |
+
text_list = fs.ls("datasets/GoChat/Gochat247_Data/Data", detail=False)
|
| 18 |
+
|
| 19 |
+
data = fs.read_text(text_list[0])
|
| 20 |
+
|
| 21 |
+
from llama_index import Document
|
| 22 |
+
doc = Document(data)
|
| 23 |
+
docs = []
|
| 24 |
+
docs.append(doc)
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
# define LLM
|
| 28 |
+
llm_predictor = LLMPredictor(llm=OpenAI(temperature=0, model_name="text-davinci-003"))
|
| 29 |
+
|
| 30 |
+
# define prompt helper
|
| 31 |
+
# set maximum input size
|
| 32 |
+
max_input_size = 4096
|
| 33 |
+
# set number of output tokens
|
| 34 |
+
num_output = 256
|
| 35 |
+
# set maximum chunk overlap
|
| 36 |
+
max_chunk_overlap = 20
|
| 37 |
+
prompt_helper = PromptHelper(max_input_size, num_output, max_chunk_overlap)
|
| 38 |
+
|
| 39 |
+
service_context = ServiceContext.from_defaults(llm_predictor=llm_predictor, prompt_helper=prompt_helper)
|
| 40 |
+
|
| 41 |
+
index = GPTSimpleVectorIndex.from_documents(docs)
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
## Define Chat BOT Class to generate Response , handle chat history,
|
| 45 |
+
class Chatbot:
|
| 46 |
+
|
| 47 |
+
def __init__(self, api_key, index):
|
| 48 |
+
self.index = index
|
| 49 |
+
openai.api_key = api_key
|
| 50 |
+
self.chat_history = []
|
| 51 |
+
|
| 52 |
+
QA_PROMPT_TMPL = (
|
| 53 |
+
"Answer without 'Answer:' word."
|
| 54 |
+
"you are in a converation with Gochat247's web site visitor\n"
|
| 55 |
+
"user got into this conversation to learn more about Gochat247"
|
| 56 |
+
"you will act like Gochat247 Virtual AI BOT. Be friendy and welcoming\n"
|
| 57 |
+
"you will be friendy and welcoming\n"
|
| 58 |
+
"The Context of the conversstion should be always limited to learing more about Gochat247 as a company providing Business Process Outosuricng and AI Customer expeeince soltuion /n"
|
| 59 |
+
"The below is the previous chat with the user\n"
|
| 60 |
+
"---------------------\n"
|
| 61 |
+
"{context_str}"
|
| 62 |
+
"\n---------------------\n"
|
| 63 |
+
"Given the context information and the chat history, and not prior knowledge\n"
|
| 64 |
+
"\nanswer the question : {query_str}\n"
|
| 65 |
+
"\n it is ok if you don not know the answer. and ask for infomration \n"
|
| 66 |
+
"Please provide a brief and concise but friendly response.")
|
| 67 |
+
|
| 68 |
+
self.QA_PROMPT = QuestionAnswerPrompt(QA_PROMPT_TMPL)
|
| 69 |
+
|
| 70 |
+
|
| 71 |
+
def generate_response(self, user_input):
|
| 72 |
+
|
| 73 |
+
prompt = "\n".join([f"{message['role']}: {message['content']}" for message in self.chat_history[-5:]])
|
| 74 |
+
prompt += f"\nUser: {user_input}"
|
| 75 |
+
self.QA_PROMPT.context_str = prompt
|
| 76 |
+
response = index.query(user_input, text_qa_template=self.QA_PROMPT)
|
| 77 |
+
|
| 78 |
+
message = {"role": "assistant", "content": response.response}
|
| 79 |
+
self.chat_history.append({"role": "user", "content": user_input})
|
| 80 |
+
self.chat_history.append(message)
|
| 81 |
+
return message
|
| 82 |
+
|
| 83 |
+
def load_chat_history(self, filename):
|
| 84 |
+
try:
|
| 85 |
+
with open(filename, 'r') as f:
|
| 86 |
+
self.chat_history = json.load(f)
|
| 87 |
+
except FileNotFoundError:
|
| 88 |
+
pass
|
| 89 |
+
|
| 90 |
+
def save_chat_history(self, filename):
|
| 91 |
+
with open(filename, 'w') as f:
|
| 92 |
+
json.dump(self.chat_history, f)
|
| 93 |
+
|
| 94 |
+
## Define Chat BOT Class to generate Response , handle chat history,
|
| 95 |
+
|
| 96 |
+
bot = Chatbot("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", index=index)
|
| 97 |
+
|
| 98 |
+
import gradio as gr
|
| 99 |
+
import time
|
| 100 |
+
|
| 101 |
+
with gr.Blocks() as demo:
|
| 102 |
+
chatbot = gr.Chatbot(label="GoChat247_Demo")
|
| 103 |
+
msg = gr.Textbox()
|
| 104 |
+
clear = gr.Button("Clear")
|
| 105 |
+
|
| 106 |
+
|
| 107 |
+
def user(user_message, history):
|
| 108 |
+
return "", history + [[user_message, None]]
|
| 109 |
+
|
| 110 |
+
def agent(history):
|
| 111 |
+
last_user_message = history[-1][0]
|
| 112 |
+
agent_message = bot.generate_response(last_user_message)
|
| 113 |
+
history[-1][1] = agent_message ["content"]
|
| 114 |
+
time.sleep(1)
|
| 115 |
+
return history
|
| 116 |
+
|
| 117 |
+
msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
|
| 118 |
+
agent, chatbot, chatbot
|
| 119 |
+
)
|
| 120 |
+
clear.click(lambda: None, None, chatbot, queue=False)
|
| 121 |
+
|
| 122 |
+
|
| 123 |
+
if __name__ == "__main__":
|
| 124 |
+
demo.launch()
|