Ahmed-14 commited on
Commit
6a4246a
·
1 Parent(s): 9fccbf0

Delete app.py

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