TestingXperts commited on
Commit
75287af
1 Parent(s): 8b555c7

Upload 12 files

Browse files
app.py ADDED
@@ -0,0 +1,333 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler
3
+ from langchain.chains import RetrievalQA
4
+ from langchain.embeddings import LlamaCppEmbeddings
5
+ from langchain.llms import GPT4All, LlamaCpp
6
+ from langchain.vectorstores import Chroma
7
+ from dotenv import load_dotenv
8
+ import os
9
+ from langchain.embeddings import HuggingFaceEmbeddings
10
+ load_dotenv()
11
+ from constants import CHROMA_SETTINGS
12
+ import openai
13
+ #from langchain_community.embeddings import HuggingFaceInferenceAPIEmbeddings
14
+
15
+ from gptcall import generate
16
+ #from yy_main import return_qa
17
+ # Set your OpenAI API key
18
+ api_key = os.environ.get('OPEN_AI_KEY') # Replace with your actual API key
19
+ openai.api_key = api_key
20
+ '''
21
+ def ask_gpt3(question):
22
+ response = openai.Completion.create(
23
+ engine="gpt-3.5-turbo",
24
+ prompt=question,
25
+ max_tokens=50
26
+ )
27
+ return response.choices[0].text.strip()
28
+
29
+ def generate(prompt):
30
+ try:
31
+ response = openai.ChatCompletion.create(
32
+ model="gpt-3.5-turbo",
33
+ messages=[
34
+ {"role": "system", "content": "You are a helpful assistant."},
35
+ {"role": "user", "content": prompt}
36
+ ],
37
+ max_tokens=1000,
38
+ temperature=0.9
39
+ )
40
+ return response['choices'][0]['message']['content']
41
+ except Exception as e:
42
+ return str(e)
43
+ '''
44
+ hf = os.environ.get("HF_TOKEN")
45
+ embeddings_model_name = os.environ.get("EMBEDDINGS_MODEL_NAME")
46
+ persist_directory = os.environ.get('PERSIST_DIRECTORY')
47
+
48
+ model_type = os.environ.get('MODEL_TYPE')
49
+ model_path = os.environ.get('MODEL_PATH')
50
+ model_n_ctx = os.environ.get('MODEL_N_CTX')
51
+ target_source_chunks = int(os.environ.get('TARGET_SOURCE_CHUNKS',4))
52
+ server_error_msg = "**NETWORK ERROR DUE TO HIGH TRAFFIC. PLEASE REGENERATE OR REFRESH THIS PAGE.**"
53
+
54
+ def clear_history(request: gr.Request):
55
+ state = None
56
+ return ([], state, "")
57
+
58
+ def post_process_code(code):
59
+ sep = "\n```"
60
+ if sep in code:
61
+ blocks = code.split(sep)
62
+ if len(blocks) % 2 == 1:
63
+ for i in range(1, len(blocks), 2):
64
+ blocks[i] = blocks[i].replace("\\_", "_")
65
+ code = sep.join(blocks)
66
+ return code
67
+
68
+ def post_process_answer(answer):
69
+ answer += f"<br><br>"
70
+ answer = answer.replace("\n", "<br>")
71
+ return answer
72
+
73
+ def predict(
74
+ question: str,
75
+ system_content: str,
76
+ use_api: bool,
77
+ chatbot: list = [],
78
+ history: list = [],
79
+ ):
80
+ try:
81
+ if use_api: # Check if API call is requested
82
+ history.append(question)
83
+ answer = generate(question)
84
+ history.append(answer)
85
+ else:
86
+ model_n_ctx = 2048
87
+ print(" print state in order", system_content, persist_directory, model_type, model_path, model_n_ctx, chatbot, history)
88
+ print("going inside embedding dunction",embeddings_model_name)
89
+ embeddings = HuggingFaceEmbeddings(model_name=embeddings_model_name)
90
+
91
+ #embeddings = HuggingFaceInferenceAPIEmbeddings(api_key=hf, model_name="sentence-transformers/all-MiniLM-l6-v2")
92
+ db = Chroma(persist_directory=persist_directory, embedding_function=embeddings, client_settings=CHROMA_SETTINGS)
93
+ retriever = db.as_retriever(search_kwargs={"k": target_source_chunks})
94
+ # Prepare the LLM
95
+ callbacks = [StreamingStdOutCallbackHandler()]
96
+
97
+ if model_type == "LlamaCpp":
98
+ llm = LlamaCpp(model_path=model_path, n_ctx=model_n_ctx, n_threads=6, n_gpu_layers=12, callbacks=callbacks, verbose=False)
99
+ elif model_type == "GPT4All":
100
+ llm = GPT4All(model=model_path, n_ctx=2048, backend='gptj', callbacks=callbacks, n_batch=8, verbose=False)
101
+ else:
102
+ print(f"Model {model_type} not supported!")
103
+ exit()
104
+
105
+ qa = RetrievalQA.from_chain_type(llm=llm, chain_type="stuff", retriever=retriever, return_source_documents=False)
106
+
107
+ # Get the answer from the chain
108
+ prompt = system_content + f"\n Question: {question}"
109
+ res = qa(prompt)
110
+ print(res)
111
+ answer = res['result']
112
+ answer = post_process_answer(answer)
113
+ history.append(question)
114
+ history.append(answer)
115
+
116
+ # Ensure history has an even number of elements
117
+ if len(history) % 2 != 0:
118
+ history.append("")
119
+
120
+ chatbot = [(history[i], history[i + 1]) for i in range(0, len(history), 2)]
121
+ return chatbot, history
122
+
123
+ except Exception as e:
124
+ history.append("")
125
+ answer = server_error_msg + f" (error_code: 503)"
126
+ history.append(answer)
127
+
128
+ # Ensure history has an even number of elements
129
+ if len(history) % 2 != 0:
130
+ history.append("")
131
+
132
+ chatbot = [(history[i], history[i + 1]) for i in range(0, len(history), 2)]
133
+ return chatbot, history
134
+
135
+
136
+
137
+ def reset_textbox(): return gr.update(value="")
138
+
139
+ llama_embeddings_model = "models/ggml-model-q4_0.bin"
140
+
141
+ def main():
142
+ title = """
143
+ <h1 align="center">Chat with TxGpt 🤖</h1>"""
144
+
145
+ css = """
146
+ @import url('https://fonts.googleapis.com/css2?family=Poppins:wght@400;700&display=swap');
147
+
148
+ /* Hide the footer */
149
+ footer .svelte-1lyswbr {
150
+ display: none !important;
151
+ }
152
+
153
+ /* Center the column container */
154
+ #prompt_container {
155
+ margin-left: auto;
156
+ margin-right: auto;
157
+ background: linear-gradient(to right, #48c6ef, #6f86d6); /* Gradient background */
158
+ padding: 20px; /* Decreased padding */
159
+ border-radius: 10px;
160
+ box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
161
+ color: black;
162
+ font-family: 'Poppins', sans-serif; /* Poppins font */
163
+ font-weight: 600; /* Bold font */
164
+ resize: none;
165
+ font-size: 18px;
166
+ }
167
+
168
+ /* Chatbot container styling */
169
+ #chatbot_container {
170
+ margin: 0 auto; /* Remove left and right margins */
171
+ max-width: 80%; /* Adjust the maximum width as needed */
172
+ background: linear-gradient(to right, #ff7e5f, #feb47b); /* Gradient background */
173
+ padding: 20px;
174
+ border-radius: 10px;
175
+ box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
176
+ }
177
+
178
+ /* Chatbot message area styling */
179
+ #chatbot .wrap.svelte-13f7djk {
180
+ height: 60vh; /* Adjusted height */
181
+ max-height: 60vh; /* Adjusted height */
182
+ border: 2px solid #007bff;
183
+ border-radius: 10px;
184
+ overflow-y: auto;
185
+ padding: 20px;
186
+ background-color: #e9f5ff;
187
+ }
188
+
189
+ /* User message styling */
190
+ #chatbot .message.user.svelte-13f7djk.svelte-13f7djk {
191
+ width: fit-content;
192
+ background: #007bff;
193
+ color: white;
194
+ border-bottom-right-radius: 0;
195
+ border-top-left-radius: 10px;
196
+ border-top-right-radius: 10px;
197
+ border-bottom-left-radius: 10px;
198
+ margin-bottom: 10px;
199
+ padding: 10px 15px;
200
+ font-size: 14px;
201
+ font-family: 'Poppins', sans-serif; /* Poppins font */
202
+ font-weight: 700; /* Bold font */
203
+ box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
204
+ }
205
+
206
+ /* Bot message styling */
207
+ #chatbot .message.bot.svelte-13f7djk.svelte-13f7djk {
208
+ width: fit-content;
209
+ background: #e1e1e1;
210
+ color: black;
211
+ border-bottom-left-radius: 0;
212
+ border-top-right-radius: 10px;
213
+ border-top-left-radius: 10px;
214
+ border-bottom-right-radius: 10px;
215
+ margin-bottom: 10px;
216
+ padding: 10px 15px;
217
+ font-size: 14px;
218
+ font-family: 'Poppins', sans-serif; /* Poppins font */
219
+ font-weight: 700; /* Bold font */
220
+ box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
221
+ }
222
+
223
+ /* Preformatted text styling */
224
+ #chatbot .pre {
225
+ border: 2px solid #f1f1f1;
226
+ padding: 10px;
227
+ border-radius: 5px;
228
+ background-color: #ffffff;
229
+ box-shadow: 0 4px 8px rgba(0, 0, 0, 0.05);
230
+ font-family: 'Poppins', sans-serif; /* Poppins font */
231
+ font-size: 14px;
232
+ font-weight: 400; /* Regular font */
233
+ }
234
+
235
+ /* General preformatted text styling */
236
+ pre {
237
+ white-space: pre-wrap; /* Since CSS 2.1 */
238
+ white-space: -moz-pre-wrap; /* Mozilla, since 1999 */
239
+ white-space: -pre-wrap; /* Opera 4-6 */
240
+ white-space: -o-pre-wrap; /* Opera 7 */
241
+ word-wrap: break-word; /* Internet Explorer 5.5+ */
242
+ font-family: 'Poppins', sans-serif; /* Poppins font */
243
+ font-size: 14px;
244
+ font-weight: 400; /* Regular font */
245
+ line-height: 1.5;
246
+ color: #333;
247
+ background-color: #f8f9fa;
248
+ padding: 10px;
249
+ border-radius: 5px;
250
+ }
251
+
252
+ /* Styling for accordion sections */
253
+ .accordion.svelte-1lyswbr {
254
+ background-color: #e9f5ff; /* Light blue background for accordions */
255
+ border: 1px solid #007bff;
256
+ border-radius: 10px;
257
+ padding: 10px;
258
+ box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
259
+ resize: both;
260
+ }
261
+
262
+ /* Prompt styling */
263
+ #prompt_title {
264
+ font-size: 24px;
265
+ margin-bottom: 10px;
266
+ resize= none;
267
+ }
268
+
269
+ /* Styling for Copy button */
270
+ .copy_button {
271
+ display: inline-block;
272
+ padding: 5px 10px;
273
+ margin: 5px 0;
274
+ font-size: 14px;
275
+ cursor: pointer;
276
+ color: #007bff;
277
+ border: 1px solid #007bff;
278
+ border-radius: 5px;
279
+ background-color: #ffffff;
280
+ transition: background-color 0.3s;
281
+ }
282
+
283
+ .copy_button:hover {
284
+ background-color: #007bff;
285
+ color: #ffffff;
286
+ }
287
+ """
288
+ with gr.Blocks(css=css) as demo:
289
+ gr.HTML(title)
290
+ with gr.Row():
291
+ with gr.Column(elem_id="prompt_container", scale=0.3): # Separate column for prompt
292
+ with gr.Accordion("Description", open=True):
293
+ system_content = gr.Textbox(value="TxGpt talk to your local documents without internet. If you need information on public data, please enable the ChatGpt checkbox and start querying!",show_label=False,lines=5)
294
+
295
+ with gr.Column(elem_id="chatbot_container", scale=0.7): # Right column for chatbot interface
296
+ chatbot = gr.Chatbot(elem_id="chatbot", label="TxGpt")
297
+ question = gr.Textbox(placeholder="Ask something", show_label=False, value="")
298
+ state = gr.State([])
299
+ use_api_toggle = gr.Checkbox(label="Enable ChatGpt", default=False, key="use_api")
300
+ with gr.Row():
301
+ with gr.Column():
302
+ submit_btn = gr.Button(value="🚀 Send")
303
+ with gr.Column():
304
+ clear_btn = gr.Button(value="🗑️ Clear history")
305
+
306
+ question.submit(
307
+ predict,
308
+ [question, system_content, use_api_toggle, chatbot, state],
309
+ [chatbot, state],
310
+ )
311
+ submit_btn.click(
312
+ predict,
313
+ [question, system_content, chatbot, state],
314
+ [chatbot, state],
315
+ )
316
+ submit_btn.click(reset_textbox, [], [question])
317
+ clear_btn.click(clear_history, None, [chatbot, state, question])
318
+ question.submit(reset_textbox, [], [question])
319
+ demo.queue(concurrency_count=10, status_update_rate="auto")
320
+ #demo.launch(server_name=args.server_name, server_port=args.server_port, share=args.share, debug=args.debug)
321
+ demo.launch(share=True, server_name='192.168.6.78')
322
+
323
+ if __name__ == '__main__':
324
+ """ import argparse
325
+ parser = argparse.ArgumentParser()
326
+ parser.add_argument("--server-name", default="0.0.0.0")
327
+ parser.add_argument("--server-port", default=8071)
328
+ parser.add_argument("--share", action="store_true")
329
+ parser.add_argument("--debug", action="store_true")
330
+ parser.add_argument("--verbose", action="store_true")
331
+ args = parser.parse_args() """
332
+
333
+ main()
db/chroma-collections.parquet ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:e8db8fa20fc636b0c35551a8af8717a37b8bb0d48e90810f4dce0a402d357206
3
+ size 557
db/chroma-embeddings.parquet ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:9d4c722bb6bf6e718de047b077b54df22468e091cdbeadf907d58eb82f8e5440
3
+ size 244085
db/index/id_to_uuid_b87fed10-6865-4f48-a7d0-62a535bc5ee3.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:6c3104e6bb51d6e97398ec2932d889f09d09c3a49e479567345b492805b043bc
3
+ size 2863
db/index/index_b87fed10-6865-4f48-a7d0-62a535bc5ee3.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:28b3d6c549428c3c63931e7ef5f5788356a70e7438945d236d8237873e778178
3
+ size 151704
db/index/index_metadata_b87fed10-6865-4f48-a7d0-62a535bc5ee3.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:ae47bc68c0061896b5f435588298df84ef783a121db1b5a76e0741b897d42785
3
+ size 73
db/index/uuid_to_id_b87fed10-6865-4f48-a7d0-62a535bc5ee3.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:b6134d91fb78964d09a52b44c8e75f6d11f31985b7e8f4668986e45cccbb11fb
3
+ size 3346
env ADDED
@@ -0,0 +1 @@
 
 
1
+ OPEN_AI_KEY=sk-proj-yZH391O3W6Xk5hVL5xxOT3BlbkFJkpcqeUYhTy8yrUwB0LXl
gptcall.py ADDED
@@ -0,0 +1,33 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import requests
2
+ import json
3
+ from aaa
4
+ import openai
5
+ import configparser
6
+ from dotenv import load_dotenv
7
+ load_dotenv()
8
+ import os
9
+ # Load configuration
10
+ config = configparser.ConfigParser()
11
+ config.read('test.env')
12
+ API_KEY = config.get('API', 'OPEN_AI_KEY')
13
+ API_URL = config.get('API', 'OPEN_AI_URL')
14
+ # print(API_URL,API_KEY)
15
+ # Set the OpenAI API key
16
+ openai.api_key = API_KEY
17
+
18
+ def generate(prompt):
19
+ # print(API_URL,API_KEY)
20
+
21
+ try:
22
+ response = openai.ChatCompletion.create(
23
+ model="gpt-4-turbo",
24
+ messages=[
25
+ {"role": "system", "content": "You are a helpful assistant."},
26
+ {"role": "user", "content": prompt}
27
+ ],
28
+ max_tokens=2000,
29
+ temperature=0.9
30
+ )
31
+ return response['choices'][0]['message']['content']
32
+ except Exception as e:
33
+ return str(e)
models/koala-7B.ggmlv3.q4_K_S.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:60621334d6f3907047c82e2bd4b72955a67a902c1edd0f6dce96cabc7bf10743
3
+ size 3791725184
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ openai==0.28
2
+ configparser
3
+ gradio
4
+ spaces
5
+ python-dotenv
test.env ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ [API]
2
+ OPEN_AI_KEY=sk-proj-yZH391O3W6Xk5hVL5xxOT3BlbkFJkpcqeUYhTy8yrUwB0LXl
3
+ OPEN_AI_URL=https://api.openai.com/v1/completions