Rahulad3 commited on
Commit
fdf80b4
·
1 Parent(s): 911653a

Rename BotRite.ipynb to app.py

Browse files
Files changed (2) hide show
  1. BotRite.ipynb +0 -567
  2. app.py +362 -0
BotRite.ipynb DELETED
@@ -1,567 +0,0 @@
1
- {
2
- "cells": [
3
- {
4
- "cell_type": "code",
5
- "execution_count": 15,
6
- "id": "c61d3ebd",
7
- "metadata": {},
8
- "outputs": [
9
- {
10
- "name": "stdout",
11
- "output_type": "stream",
12
- "text": [
13
- "Running on local URL: http://127.0.0.1:7874\n",
14
- "\n",
15
- "To create a public link, set `share=True` in `launch()`.\n"
16
- ]
17
- },
18
- {
19
- "data": {
20
- "text/html": [
21
- "<div><iframe src=\"http://127.0.0.1:7874/\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
22
- ],
23
- "text/plain": [
24
- "<IPython.core.display.HTML object>"
25
- ]
26
- },
27
- "metadata": {},
28
- "output_type": "display_data"
29
- },
30
- {
31
- "data": {
32
- "text/plain": []
33
- },
34
- "execution_count": 15,
35
- "metadata": {},
36
- "output_type": "execute_result"
37
- },
38
- {
39
- "name": "stderr",
40
- "output_type": "stream",
41
- "text": [
42
- "Traceback (most recent call last):\n",
43
- " File \"C:\\Users\\bjawa\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\gradio\\routes.py\", line 401, in run_predict\n",
44
- " output = await app.get_blocks().process_api(\n",
45
- " ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n",
46
- " File \"C:\\Users\\bjawa\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\gradio\\blocks.py\", line 1305, in process_api\n",
47
- " data = self.postprocess_data(fn_index, result[\"prediction\"], state)\n",
48
- " ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n",
49
- " File \"C:\\Users\\bjawa\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\gradio\\blocks.py\", line 1209, in postprocess_data\n",
50
- " self.validate_outputs(fn_index, predictions) # type: ignore\n",
51
- " ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n",
52
- " File \"C:\\Users\\bjawa\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\gradio\\blocks.py\", line 1184, in validate_outputs\n",
53
- " raise ValueError(\n",
54
- "ValueError: An event handler (loadbotdataasdf) didn't receive enough output values (needed: 2, received: 1).\n",
55
- "Wanted outputs:\n",
56
- " [dataframe, dropdown]\n",
57
- "Received outputs:\n",
58
- " [None]\n",
59
- "Traceback (most recent call last):\n",
60
- " File \"C:\\Users\\bjawa\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\gradio\\routes.py\", line 401, in run_predict\n",
61
- " output = await app.get_blocks().process_api(\n",
62
- " ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n",
63
- " File \"C:\\Users\\bjawa\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\gradio\\blocks.py\", line 1305, in process_api\n",
64
- " data = self.postprocess_data(fn_index, result[\"prediction\"], state)\n",
65
- " ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n",
66
- " File \"C:\\Users\\bjawa\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\gradio\\blocks.py\", line 1209, in postprocess_data\n",
67
- " self.validate_outputs(fn_index, predictions) # type: ignore\n",
68
- " ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n",
69
- " File \"C:\\Users\\bjawa\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\gradio\\blocks.py\", line 1184, in validate_outputs\n",
70
- " raise ValueError(\n",
71
- "ValueError: An event handler (loadbotdataasdf) didn't receive enough output values (needed: 2, received: 1).\n",
72
- "Wanted outputs:\n",
73
- " [dataframe, dropdown]\n",
74
- "Received outputs:\n",
75
- " [None]\n"
76
- ]
77
- },
78
- {
79
- "name": "stdout",
80
- "output_type": "stream",
81
- "text": [
82
- "Bot1\n",
83
- " b1\n",
84
- "Bot_Handle_Name b1\n",
85
- "Bot_Image C:\\Users\\bjawa\\AppData\\Local\\Temp\\3e5bb8de0c29...\n",
86
- "Bot_Name Bot1\n",
87
- "Created_Time 05/07/2023 20:53:23\n",
88
- "Created_by Jawahar\n",
89
- "Initial_Message test\n",
90
- "Intro_Message test\n",
91
- "OpenAI_API_key test\n",
92
- "Organizationame Cognizant\n",
93
- "Rules test\n",
94
- "Tools [OpenAI]\n"
95
- ]
96
- },
97
- {
98
- "name": "stderr",
99
- "output_type": "stream",
100
- "text": [
101
- "Traceback (most recent call last):\n",
102
- " File \"C:\\Users\\bjawa\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\gradio\\routes.py\", line 401, in run_predict\n",
103
- " output = await app.get_blocks().process_api(\n",
104
- " ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n",
105
- " File \"C:\\Users\\bjawa\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\gradio\\blocks.py\", line 1302, in process_api\n",
106
- " result = await self.call_function(\n",
107
- " ^^^^^^^^^^^^^^^^^^^^^^^^^\n",
108
- " File \"C:\\Users\\bjawa\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\gradio\\blocks.py\", line 1025, in call_function\n",
109
- " prediction = await anyio.to_thread.run_sync(\n",
110
- " ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n",
111
- " File \"C:\\Users\\bjawa\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\anyio\\to_thread.py\", line 31, in run_sync\n",
112
- " return await get_asynclib().run_sync_in_worker_thread(\n",
113
- " ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n",
114
- " File \"C:\\Users\\bjawa\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\anyio\\_backends\\_asyncio.py\", line 937, in run_sync_in_worker_thread\n",
115
- " return await future\n",
116
- " ^^^^^^^^^^^^\n",
117
- " File \"C:\\Users\\bjawa\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\anyio\\_backends\\_asyncio.py\", line 867, in run\n",
118
- " result = context.run(func, *args)\n",
119
- " ^^^^^^^^^^^^^^^^^^^^^^^^\n",
120
- " File \"C:\\Users\\bjawa\\AppData\\Local\\Temp\\ipykernel_2208\\3954756524.py\", line 164, in upload_file\n",
121
- " shutil.copy(file_paths, path)\n",
122
- " File \"C:\\Users\\bjawa\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\shutil.py\", line 419, in copy\n",
123
- " copyfile(src, dst, follow_symlinks=follow_symlinks)\n",
124
- " File \"C:\\Users\\bjawa\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\shutil.py\", line 235, in copyfile\n",
125
- " if _samefile(src, dst):\n",
126
- " ^^^^^^^^^^^^^^^^^^^\n",
127
- " File \"C:\\Users\\bjawa\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\shutil.py\", line 212, in _samefile\n",
128
- " return os.path.samefile(src, dst)\n",
129
- " ^^^^^^^^^^^^^^^^^^^^^^^^^^\n",
130
- " File \"<frozen genericpath>\", line 100, in samefile\n",
131
- "TypeError: stat: path should be string, bytes, os.PathLike or integer, not list\n"
132
- ]
133
- }
134
- ],
135
- "source": [
136
- "import numpy as np\n",
137
- "import gradio as gr\n",
138
- "import json\n",
139
- "import socket\n",
140
- "import os\n",
141
- "from datetime import datetime\n",
142
- "import pandas as pd\n",
143
- "from langchain.chains import RetrievalQA\n",
144
- "from langchain.llms import OpenAI\n",
145
- "from langchain.document_loaders import TextLoader\n",
146
- "from langchain.document_loaders import PyPDFLoader\n",
147
- "from langchain.indexes import VectorstoreIndexCreator\n",
148
- "from langchain.text_splitter import CharacterTextSplitter\n",
149
- "from langchain.embeddings import OpenAIEmbeddings\n",
150
- "from langchain.vectorstores import Chroma\n",
151
- "from langchain.chains import ConversationalRetrievalChain\n",
152
- "import shutil\n",
153
- "demo = gr.Blocks()\n",
154
- "options_org=[]\n",
155
- "options_bot=['','','']\n",
156
- "\n",
157
- "\n",
158
- "isExist = os.path.exists(\"Organizations\")\n",
159
- "if(isExist==False):\n",
160
- " os.mkdir(\"Organizations\")\n",
161
- " \n",
162
- "if(os.path.isfile('Organizationdetails.json')):\n",
163
- " #Getting organization name\n",
164
- " f = open('Organizationdetails.json', encoding='utf-8', errors='ignore')\n",
165
- " data = json.load(f,strict=False)\n",
166
- " for p_id, p_info in data.items():\n",
167
- " options_org.append(p_id)\n",
168
- " f.close()\n",
169
- " \n",
170
- " \n",
171
- "if(os.path.isfile('Botdetails.json')):\n",
172
- " #Getting organization name\n",
173
- " f1 = open('Botdetails.json')\n",
174
- " data = json.load(f1)\n",
175
- " for p_id, p_info in data.items():\n",
176
- " options_bot.append(p_id)\n",
177
- " f1.close()\n",
178
- " \n",
179
- "\n",
180
- "def Create_Organization(org_name, org_handle):\n",
181
- " \n",
182
- " o=org_handle\n",
183
- " path = o\n",
184
- " isExist = os.path.exists(path)\n",
185
- " hostname=socket.gethostname()\n",
186
- " now = datetime.now()\n",
187
- " tim=now.strftime(\"%d/%m/%Y %H:%M:%S\")\n",
188
- " Organizationdetails={}\n",
189
- " Organization_required_details = [\"Organizationame\",\"OrganizationHandle\" \"Created_by\", \"Created_Time\"]\n",
190
- " Organizationdetails[org_handle] = {}\n",
191
- " Organizationdetails[org_handle]['Organizationame']=org_name\n",
192
- " Organizationdetails[org_handle]['OrganizationHandle']=org_handle\n",
193
- " Organizationdetails[org_handle]['Created_by']=hostname\n",
194
- " Organizationdetails[org_handle]['Created_Time']=tim\n",
195
- " isfileE = os.path.isfile(\"Organizationdetails.json\")\n",
196
- " if isfileE: #If file present \"rb\" and w\n",
197
- " with open('Organizationdetails.json', 'rb') as txtfile:\n",
198
- " d=json.load(txtfile)\n",
199
- " d.update(Organizationdetails)\n",
200
- " for p_id, p_info in d.items():\n",
201
- " options_org.append(p_id)\n",
202
- " with open('Organizationdetails.json', 'w') as txtfile: \n",
203
- " json.dump(d, txtfile)\n",
204
- " if not isExist: #create folder for working\n",
205
- " os.makedirs(os.path.join('Organizations', org_handle))\n",
206
- " else: #if file not present then create with 'w'\n",
207
- " with open('Organizationdetails.json', 'w') as txtfile: \n",
208
- " json.dump(Organizationdetails, txtfile)\n",
209
- " if not isExist: #create folder for working\n",
210
- " os.makedirs(os.path.join('Organizations', org_handle)) \n",
211
- " return \"Organization Created : \"+ o\n",
212
- "\n",
213
- "def clear():\n",
214
- " return None, None, None\n",
215
- "\n",
216
- "def Create_Bot(Organizationame,Bot_Name,Bot_Handle_Name,Bot_Image,Tools,OpenAI_API_key,\n",
217
- " Initial_Message,Intro_Message,Rules):\n",
218
- " \n",
219
- " botim =Bot_Image.name\n",
220
- " print(Bot_Name)\n",
221
- " b=Bot_Handle_Name\n",
222
- " bo=Organizationame\n",
223
- " hostname=socket.gethostname()\n",
224
- " now = datetime.now()\n",
225
- " tim=now.strftime(\"%d/%m/%Y %H:%M:%S\") \n",
226
- " Botdetails = { }\n",
227
- " Bot_required_details = [\"Bot_Name\", \"Organizationame\", \"Created_by\", \"Created_Time\",\"Bot_Handle_Name\",\"Bot_Image\",\"Tools\",\"OpenAI_API_key\",\n",
228
- " \"Initial_Message\",\"Intro_Message\",\"Rules\"]\n",
229
- " Botdetails[Bot_Handle_Name] = {}\n",
230
- " Botdetails[Bot_Handle_Name]['Bot_Name']=Bot_Name\n",
231
- " Botdetails[Bot_Handle_Name]['Organizationame']=Organizationame\n",
232
- " Botdetails[Bot_Handle_Name]['Created_by']=hostname\n",
233
- " Botdetails[Bot_Handle_Name]['Created_Time']=tim\n",
234
- " Botdetails[Bot_Handle_Name]['Bot_Handle_Name']=Bot_Handle_Name\n",
235
- " Botdetails[Bot_Handle_Name]['Bot_Image']=botim\n",
236
- " Botdetails[Bot_Handle_Name]['Tools']=Tools\n",
237
- " Botdetails[Bot_Handle_Name]['OpenAI_API_key']=OpenAI_API_key\n",
238
- " Botdetails[Bot_Handle_Name]['Initial_Message']=Initial_Message\n",
239
- " Botdetails[Bot_Handle_Name]['Intro_Message']=Intro_Message\n",
240
- " Botdetails[Bot_Handle_Name]['Rules']=Rules\n",
241
- " path = os.path.join(os.getcwd()+'\\\\Organizations\\\\', Organizationame,Bot_Name)\n",
242
- " Inputpath = os.path.join(os.getcwd()+'\\\\Organizations\\\\', Organizationame,Bot_Name,'Inputs')\n",
243
- " Outputpath = os.path.join(os.getcwd()+'\\\\Organizations\\\\', Organizationame,Bot_Name,'Outputs')\n",
244
- " \n",
245
- " isExist = os.path.exists(path)\n",
246
- "\n",
247
- " pp=os.path.join('Organizations',Organizationame+'\\\\Botdetails.json')\n",
248
- "\n",
249
- " isfileE = os.path.isfile(pp)\n",
250
- " if isfileE: #If file present \"rb\" and 'w'\n",
251
- " with open(pp, 'rb') as txtfile:\n",
252
- " d=json.load(txtfile)\n",
253
- " d.update(Botdetails)\n",
254
- " with open(pp, 'w') as txtfile: \n",
255
- " json.dump(d, txtfile)\n",
256
- " if not isExist:\n",
257
- " os.makedirs(path)\n",
258
- " os.makedirs(Inputpath) \n",
259
- " os.makedirs(Outputpath) \n",
260
- " else: #if file not present then create with 'w'\n",
261
- " with open(pp, 'w') as txtfile: \n",
262
- " json.dump(Botdetails, txtfile)\n",
263
- " if not isExist:\n",
264
- " os.makedirs(path)\n",
265
- " os.makedirs(Inputpath) \n",
266
- " os.makedirs(Outputpath) \n",
267
- " return \"Bot Created : \" + b +\" in \"+bo +\" Organization \"\n",
268
- "\n",
269
- "\n",
270
- "\n",
271
- "\n",
272
- "def loadbotdata(SelectOrganizationame):\n",
273
- " new=[]\n",
274
- " if(os.path.isfile(os.getcwd()+'\\\\Organizations\\\\'+SelectOrganizationame+'\\\\Botdetails.json')):\n",
275
- " fd = open(os.getcwd()+'\\\\Organizations\\\\'+SelectOrganizationame+'\\\\Botdetails.json')\n",
276
- " data = json.load(fd)\n",
277
- " for p_id, p_info in data.items():\n",
278
- " new.append(p_id)\n",
279
- " #return new\n",
280
- " return gr.update(choices=new, value=new[0])\n",
281
- "\n",
282
- "\n",
283
- "def loadbotdataasdf(SelectOrganizationame):\n",
284
- " df=[]\n",
285
- " new=[]\n",
286
- " if(os.path.isfile(os.getcwd()+'\\\\Organizations\\\\'+SelectOrganizationame+'\\\\Botdetails.json')):\n",
287
- " fl = open(os.getcwd()+'\\\\Organizations\\\\'+SelectOrganizationame+'\\\\Botdetails.json')\n",
288
- " data = json.load(fl)\n",
289
- " df = pd.DataFrame.from_dict(data, orient='columns')\n",
290
- " print (df)\n",
291
- " for p_id, p_info in data.items():\n",
292
- " new.append(p_id)\n",
293
- " return df,gr.update(choices=new, value=new[0]) \n",
294
- "\n",
295
- "def upload_file(org, bot , files):\n",
296
- " file_paths = [file.name for file in files]\n",
297
- " allfiles=file_paths\n",
298
- " path = os.path.join(os.getcwd()+'\\\\Organizations\\\\', org,bot,'Inputs')\n",
299
- " shutil.copy(file_paths.name, path)\n",
300
- " return file_paths\n",
301
- " \n",
302
- "def train(files):\n",
303
- " for file in files:\n",
304
- " print(file.name)\n",
305
- " if file.name.endswith(\".pdf\"):\n",
306
- " loader = PyPDFLoader(file.name)\n",
307
- " documents = loader.load()\n",
308
- " return \"Training Done\"\n",
309
- "chat_history = []\n",
310
- "\n",
311
- "def construct_index(directory_path):\n",
312
- " file_paths=[]\n",
313
- " for root, directories, files in os.walk(directory_path):\n",
314
- " for file_name in files:\n",
315
- " file_path = os.path.join(root, file_name)\n",
316
- " file_paths.append(file_path)\n",
317
- "\n",
318
- "\n",
319
- " for file in file_paths: \n",
320
- " if file.endswith(\".pdf\"):\n",
321
- " loader = PyPDFLoader(file)\n",
322
- " documents = loader.load()\n",
323
- " return documents\n",
324
- "\n",
325
- "\n",
326
- "def chatbot(input_text):\n",
327
- " global chat_history\n",
328
- " query = input_text\n",
329
- " text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)\n",
330
- " texts = text_splitter.split_documents(documents)\n",
331
- " embeddings = OpenAIEmbeddings()\n",
332
- " db = Chroma.from_documents(texts, embeddings)\n",
333
- " retriever = db.as_retriever(search_type=\"similarity\", search_kwargs={\"k\":2})\n",
334
- " vectordbkwargs = {\"search_distance\": 0.9}\n",
335
- " qa = ConversationalRetrievalChain.from_llm(OpenAI(), retriever,return_source_documents=True)\n",
336
- " print(chat_history)\n",
337
- " if chat_history==[]:\n",
338
- " result = qa({\"question\": query,\"chat_history\": chat_history, \"vectordbkwargs\": {\"search_distance\": 0.9}})\n",
339
- " else:\n",
340
- " result = qa({\"question\": query, \"chat_history\": chat_history, \"vectordbkwargs\": {\"search_distance\": 0.9}})\n",
341
- " chat_history = [(query, result[\"answer\"])]\n",
342
- " return result[\"answer\"]\n",
343
- "\n",
344
- "\n",
345
- "with demo:\n",
346
- " gr.Markdown(\"BotRite\")\n",
347
- " \n",
348
- " with gr.Tabs() :\n",
349
- " with gr.TabItem(\"ChatBot\"):\n",
350
- " with gr.Row():\n",
351
- " SelectOrg = gr.Dropdown(options_org ,label=\"Select Organization\" )\n",
352
- " \n",
353
- " with gr.Row():\n",
354
- " Selectbot = gr.Dropdown( label=\"Select Bot\" ,choices=options_bot,\n",
355
- " value=options_bot[0], interactive=True)\n",
356
- " # Selectbot = gr.Radio(options_bot ,label=\"Select Bot\")\n",
357
- " \n",
358
- " with gr.Column():\n",
359
- " query_input = gr.Textbox(lines=7, label=\"Enter your text\")\n",
360
- " ask_button = gr.Button(\"Ask\")\n",
361
- " with gr.TabItem(\"Settings\"):\n",
362
- " with gr.Tabs():\n",
363
- " \n",
364
- " with gr.TabItem(\"Create Organization:\"):\n",
365
- " with gr.Row():\n",
366
- " org_name = gr.Textbox(label=\"Name\",info=\"Your name / Nickname\",placeholder=\"Enter you organization full name\")\n",
367
- " org_handle = gr.Textbox(label=\"Handle Name\",info=\"Your unique organization name\", placeholder=\"Enter Organization handle name\")\n",
368
- " output_org =gr.Textbox(label='Status')\n",
369
- " with gr.Row():\n",
370
- " Createorg_button = gr.Button(\"Create Organization\")\n",
371
- " Clearorg_button = gr.Button(\"Clear\", variant=\"stop\")\n",
372
- " \n",
373
- " with gr.TabItem(\"Bot Details\"):\n",
374
- " SelectOrganizationame = gr.Dropdown(options_org, label=\"Select Organization\")\n",
375
- " with gr.Tabs():\n",
376
- " with gr.TabItem(\"Your Bots:\"):\n",
377
- " with gr.Row():\n",
378
- " with gr.Column():\n",
379
- " #SelectOrganizationame = gr.components.Dropdown(options_org, label=\"Select Organization\")\n",
380
- " botdf=gr.Dataframe(headers=[\"Bot_Name\", \"Organizationame\", \"Created_by\", \"Created_Time\",\"Bot_Handle_Name\",\"Bot_Image\",\"Tools\",\"OpenAI_API_key\",\n",
381
- " \"Initial_Message\",\"Intro_Message\",\"Rules\"], label=\"Bot Details\")\n",
382
- " \n",
383
- " with gr.TabItem(\"Create Bot:\"): \n",
384
- " \n",
385
- " Organizationame =SelectOrganizationame\n",
386
- " botname = gr.Textbox(label=\"Bot Name\",info=\"Your bot name / Nickname\", placeholder=\"Enter bot full name\")\n",
387
- " bothandle = gr.Textbox(label=\"Bot Handle Name\",info=\"Your unique bot name\" ,placeholder=\"Enter bot handle name\")\n",
388
- " image_button = gr.File(label=\"Select bot image\")\n",
389
- " botllm = gr.components.CheckboxGroup(['OpenAI', 'Dolly', 'Q&A Model'],label=\"Tools\")\n",
390
- " # model = gr.components.Dropdown(Options1, label=\"Model\")\n",
391
- " openai_key = gr.Textbox(label=\"You OpenAI API key\", type=\"password\" , info=\"Add your OpenAi Key click the link to create new or copy exsisting key from your openai account https://platform.openai.com/account/api-keys\")\n",
392
- " initailsmsg = gr.Textbox(label=\"Initial Message\", placeholder=\"This message will be shared by bot as intro​\" , info=\"This message will be shared by bot as intro​\")\n",
393
- " intromsg = gr.Textbox(label=\"Intro Message\", placeholder=\"This message will be sent to bot as prefix to first message​\", info=\"This message will be sent to bot as prefix to first message​\")\n",
394
- " rules = gr.Textbox(label=\"Rules\", placeholder=\"These rules will be sent to bot as prefix to first message (after introduction)​\", info=\"These rules will be sent to bot as prefix to first message (after introduction)​\")\n",
395
- " output_bot =gr.Textbox(label='Status')\n",
396
- " Createbot_button = gr.Button(\"Create Bot\") \n",
397
- " with gr.TabItem(\"Bot Configuration\"):\n",
398
- " Selectbotconfig = gr.Dropdown(label=\"Select Bot\",choices=options_bot,\n",
399
- " value=options_bot[0], interactive=True)\n",
400
- " with gr.TabItem(\"Load Data\"):\n",
401
- " with gr.Row():\n",
402
- " with gr.Column():\n",
403
- " Train_Fileselect_button = gr.UploadButton(\"Upload PDF Files\", file_types=[\".pdf\"], file_count=\"multiple\")\n",
404
- " file_output = gr.File()\n",
405
- " with gr.Column(): \n",
406
- " Train_button = gr.Button(\"Train Data\")\n",
407
- " Train_output =gr.Textbox(label='Status')\n",
408
- "\n",
409
- " with gr.TabItem(\"Chat with your bot\"):\n",
410
- " with gr.Row():\n",
411
- " with gr.Column():\n",
412
- " query_input = gr.Textbox(lines=7, label=\"Enter your text\")\n",
413
- " ask_button = gr.Button(\"Ask\")\n",
414
- " with gr.Column():\n",
415
- " text_output=gr.Text(label=\"Your Bot Answer\")\n",
416
- " with gr.TabItem(\"Deploy\"):\n",
417
- " with gr.Row():\n",
418
- " archive_button = gr.Button(\"Archive\")\n",
419
- " publish_button = gr.Button(\"Publish\")\n",
420
- " with gr.TabItem(\"Logs\"):\n",
421
- " with gr.Column():\n",
422
- " Selectlog = gr.Dropdown( label=\"Select Log\")\n",
423
- " logview = gr.Text(label=\"Log\")\n",
424
- " #def Dropdown_Org(x):\n",
425
- " \n",
426
- " SelectOrg.change(fn=loadbotdata, inputs=SelectOrg,outputs=Selectbot ) \n",
427
- " SelectOrganizationame.change(fn=loadbotdataasdf, inputs=SelectOrganizationame,outputs=[botdf,Selectbotconfig]) \n",
428
- " Createorg_button.click(fn=Create_Organization,inputs=[org_name, org_handle], outputs=output_org)\n",
429
- "\n",
430
- " Clearorg_button.click(lambda : [None,None,None], inputs=None, outputs=[org_name,org_handle,output_org]) \n",
431
- " Createbot_button.click(fn=Create_Bot,inputs=[Organizationame, botname, bothandle,image_button,botllm,openai_key,initailsmsg,intromsg,rules], outputs=output_bot)\n",
432
- " \n",
433
- " Train_Fileselect_button.upload(upload_file,inputs=[SelectOrganizationame, Selectbotconfig,Train_Fileselect_button], outputs=[file_output])\n",
434
- " Train_button.click(fn=train,inputs=Train_Fileselect_button, outputs=Train_output)\n",
435
- " \n",
436
- "demo.launch()"
437
- ]
438
- },
439
- {
440
- "cell_type": "code",
441
- "execution_count": null,
442
- "id": "073c8125",
443
- "metadata": {},
444
- "outputs": [],
445
- "source": [
446
- "\n",
447
- "\n",
448
- "def generate_data():\n",
449
- " # Mutate here\n",
450
- " dd.choices = [x for x in range(10)]\n",
451
- " return gr.update(choices = [x for x in range(10)])\n",
452
- "\n",
453
- "with gr.Blocks() as demo:\n",
454
- " bttn.click(\n",
455
- " fn = generate_data,\n",
456
- " outputs = dd\n",
457
- " )\n",
458
- " \n",
459
- "d = gr.Dropdown(render=False)\n",
460
- "bttn = gr.Button(render=False)\n",
461
- "demo.launch()\n"
462
- ]
463
- },
464
- {
465
- "cell_type": "code",
466
- "execution_count": null,
467
- "id": "05603510",
468
- "metadata": {},
469
- "outputs": [],
470
- "source": [
471
- "import gradio as gr\n",
472
- "\n",
473
- "def mock_ocr(f):\n",
474
- " return [[1, 2, 3], [4, 5, 6]]\n",
475
- "\n",
476
- "def export_csv(d):\n",
477
- " d.to_csv(\"output.csv\")\n",
478
- " return gr.File.update(value=\"output.csv\", visible=True)\n",
479
- "\n",
480
- "with gr.Blocks() as demo:\n",
481
- " with gr.Row():\n",
482
- " file = gr.File(label=\"PDF file\", file_types=[\".pdf\"])\n",
483
- " dataframe = gr.Dataframe()\n",
484
- " \n",
485
- " with gr.Column():\n",
486
- " button = gr.Button(\"Export\")\n",
487
- " csv = gr.File(interactive=False, visible=False)\n",
488
- " \n",
489
- " \n",
490
- " file.change(mock_ocr, file, dataframe)\n",
491
- " button.click(export_csv, dataframe, csv)\n",
492
- " \n",
493
- "demo.launch()"
494
- ]
495
- },
496
- {
497
- "cell_type": "code",
498
- "execution_count": null,
499
- "id": "3ee5af32",
500
- "metadata": {},
501
- "outputs": [],
502
- "source": [
503
- "pinecone --> Cost\n",
504
- "\n",
505
- "chromadb --(for both openai & q&a) -->\n",
506
- "OpenAIEmbeddings\n",
507
- "\n",
508
- "\n",
509
- "Q&A --->i/p=---> Embeddings ----> Bert ---> Answers\n",
510
- "\n",
511
- "Openai --->i/p--> Embeddings--->(Storage system for openai ) ---> Openai api key ---> Answers\n",
512
- "\n",
513
- "\n",
514
- "\n",
515
- "Pdf:\n",
516
- " \n",
517
- " Editable --> Ok\n",
518
- " Table --> Ok\n",
519
- " Image , Screen shot etc--> Not working (Image processing opencv , Pytessarct , Ocr ) (LG.pdf)\n",
520
- " chatpdf.io \n",
521
- " pdf.ai\n",
522
- " ilovepdf --> merge,editing \n",
523
- " \n",
524
- "\n",
525
- " pypdf --> Identify the image in every pdf and apply the logic (Image -> Text -> Pg no)\n",
526
- "\n",
527
- "Ajay , Rahul : \n",
528
- " \n",
529
- "Container - Docker (Rahul) \n",
530
- "Cloud - AWS (Rahul)\n",
531
- "Frontend -- react (Ajay)\n",
532
- "Backend ---> Django rest api DRF (Ajay) \n",
533
- "\n",
534
- " api --> Modules (Python )\n",
535
- " creation org(api) (Rahul) (Gradio already present)\n",
536
- " creation bot(api)\n",
537
- " Image to text (LG.pdf) (rahul)\n",
538
- " \n",
539
- "\n",
540
- "Database -> Postgres (Ajay), chroma (rahul)\n",
541
- " \n",
542
- " "
543
- ]
544
- }
545
- ],
546
- "metadata": {
547
- "kernelspec": {
548
- "display_name": "Python 3 (ipykernel)",
549
- "language": "python",
550
- "name": "python3"
551
- },
552
- "language_info": {
553
- "codemirror_mode": {
554
- "name": "ipython",
555
- "version": 3
556
- },
557
- "file_extension": ".py",
558
- "mimetype": "text/x-python",
559
- "name": "python",
560
- "nbconvert_exporter": "python",
561
- "pygments_lexer": "ipython3",
562
- "version": "3.11.0"
563
- }
564
- },
565
- "nbformat": 4,
566
- "nbformat_minor": 5
567
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
app.py ADDED
@@ -0,0 +1,362 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import numpy as np
2
+ import gradio as gr
3
+ import json
4
+ import socket
5
+ import os
6
+ from datetime import datetime
7
+ import pandas as pd
8
+ from langchain.chains import RetrievalQA
9
+ from langchain.llms import OpenAI
10
+ from langchain.document_loaders import TextLoader
11
+ from langchain.document_loaders import PyPDFLoader
12
+ from langchain.indexes import VectorstoreIndexCreator
13
+ from langchain.text_splitter import CharacterTextSplitter
14
+ from langchain.embeddings import OpenAIEmbeddings
15
+ from langchain.vectorstores import Chroma
16
+ from langchain.chains import ConversationalRetrievalChain
17
+ import shutil
18
+ demo = gr.Blocks()
19
+ options_org=[]
20
+ options_bot=['','','']
21
+
22
+
23
+ isExist = os.path.exists("Organizations")
24
+ if(isExist==False):
25
+ os.mkdir("Organizations")
26
+
27
+ if(os.path.isfile('Organizationdetails.json')):
28
+ #Getting organization name
29
+ f = open('Organizationdetails.json', encoding='utf-8', errors='ignore')
30
+ data = json.load(f,strict=False)
31
+ for p_id, p_info in data.items():
32
+ options_org.append(p_id)
33
+ f.close()
34
+
35
+
36
+ if(os.path.isfile('Botdetails.json')):
37
+ #Getting organization name
38
+ f1 = open('Botdetails.json')
39
+ data = json.load(f1)
40
+ for p_id, p_info in data.items():
41
+ options_bot.append(p_id)
42
+ f1.close()
43
+
44
+
45
+ def Create_Organization(org_name, org_handle):
46
+
47
+ o=org_handle
48
+ path = o
49
+ isExist = os.path.exists(path)
50
+ hostname=socket.gethostname()
51
+ now = datetime.now()
52
+ tim=now.strftime("%d/%m/%Y %H:%M:%S")
53
+ Organizationdetails={}
54
+ Organization_required_details = ["Organizationame","OrganizationHandle" "Created_by", "Created_Time"]
55
+ Organizationdetails[org_handle] = {}
56
+ Organizationdetails[org_handle]['Organizationame']=org_name
57
+ Organizationdetails[org_handle]['OrganizationHandle']=org_handle
58
+ Organizationdetails[org_handle]['Created_by']=hostname
59
+ Organizationdetails[org_handle]['Created_Time']=tim
60
+ isfileE = os.path.isfile("Organizationdetails.json")
61
+ if isfileE: #If file present "rb" and w
62
+ with open('Organizationdetails.json', 'rb') as txtfile:
63
+ d=json.load(txtfile)
64
+ d.update(Organizationdetails)
65
+ for p_id, p_info in d.items():
66
+ options_org.append(p_id)
67
+ with open('Organizationdetails.json', 'w') as txtfile:
68
+ json.dump(d, txtfile)
69
+ if not isExist: #create folder for working
70
+ os.makedirs(os.path.join('Organizations', org_handle))
71
+ else: #if file not present then create with 'w'
72
+ with open('Organizationdetails.json', 'w') as txtfile:
73
+ json.dump(Organizationdetails, txtfile)
74
+ if not isExist: #create folder for working
75
+ os.makedirs(os.path.join('Organizations', org_handle))
76
+ return "Organization Created : "+ o
77
+
78
+ def clear():
79
+ return None, None, None
80
+
81
+ def Create_Bot(Organizationame,Bot_Name,Bot_Handle_Name,Bot_Image,Tools,OpenAI_API_key,
82
+ Initial_Message,Intro_Message,Rules):
83
+
84
+ botim =Bot_Image.name
85
+ print(Bot_Name)
86
+ b=Bot_Handle_Name
87
+ bo=Organizationame
88
+ hostname=socket.gethostname()
89
+ now = datetime.now()
90
+ tim=now.strftime("%d/%m/%Y %H:%M:%S")
91
+ Botdetails = { }
92
+ Bot_required_details = ["Bot_Name", "Organizationame", "Created_by", "Created_Time","Bot_Handle_Name","Bot_Image","Tools","OpenAI_API_key",
93
+ "Initial_Message","Intro_Message","Rules"]
94
+ Botdetails[Bot_Handle_Name] = {}
95
+ Botdetails[Bot_Handle_Name]['Bot_Name']=Bot_Name
96
+ Botdetails[Bot_Handle_Name]['Organizationame']=Organizationame
97
+ Botdetails[Bot_Handle_Name]['Created_by']=hostname
98
+ Botdetails[Bot_Handle_Name]['Created_Time']=tim
99
+ Botdetails[Bot_Handle_Name]['Bot_Handle_Name']=Bot_Handle_Name
100
+ Botdetails[Bot_Handle_Name]['Bot_Image']=botim
101
+ Botdetails[Bot_Handle_Name]['Tools']=Tools
102
+ Botdetails[Bot_Handle_Name]['OpenAI_API_key']=OpenAI_API_key
103
+ Botdetails[Bot_Handle_Name]['Initial_Message']=Initial_Message
104
+ Botdetails[Bot_Handle_Name]['Intro_Message']=Intro_Message
105
+ Botdetails[Bot_Handle_Name]['Rules']=Rules
106
+ path = os.path.join(os.getcwd()+'\\Organizations\\', Organizationame,Bot_Name)
107
+ Inputpath = os.path.join(os.getcwd()+'\\Organizations\\', Organizationame,Bot_Name,'Inputs')
108
+ Outputpath = os.path.join(os.getcwd()+'\\Organizations\\', Organizationame,Bot_Name,'Outputs')
109
+
110
+ isExist = os.path.exists(path)
111
+
112
+ pp=os.path.join('Organizations',Organizationame+'\\Botdetails.json')
113
+
114
+ isfileE = os.path.isfile(pp)
115
+ if isfileE: #If file present "rb" and 'w'
116
+ with open(pp, 'rb') as txtfile:
117
+ d=json.load(txtfile)
118
+ d.update(Botdetails)
119
+ with open(pp, 'w') as txtfile:
120
+ json.dump(d, txtfile)
121
+ if not isExist:
122
+ os.makedirs(path)
123
+ os.makedirs(Inputpath)
124
+ os.makedirs(Outputpath)
125
+ else: #if file not present then create with 'w'
126
+ with open(pp, 'w') as txtfile:
127
+ json.dump(Botdetails, txtfile)
128
+ if not isExist:
129
+ os.makedirs(path)
130
+ os.makedirs(Inputpath)
131
+ os.makedirs(Outputpath)
132
+ return "Bot Created : " + b +" in "+bo +" Organization "
133
+
134
+
135
+
136
+
137
+ def loadbotdata(SelectOrganizationame):
138
+ new=[]
139
+ if(os.path.isfile(os.getcwd()+'\\Organizations\\'+SelectOrganizationame+'\\Botdetails.json')):
140
+ fd = open(os.getcwd()+'\\Organizations\\'+SelectOrganizationame+'\\Botdetails.json')
141
+ data = json.load(fd)
142
+ for p_id, p_info in data.items():
143
+ new.append(p_id)
144
+ #return new
145
+ return gr.update(choices=new, value=new[0])
146
+
147
+
148
+ def loadbotdataasdf(SelectOrganizationame):
149
+ df=[]
150
+ new=[]
151
+ if(os.path.isfile(os.getcwd()+'\\Organizations\\'+SelectOrganizationame+'\\Botdetails.json')):
152
+ fl = open(os.getcwd()+'\\Organizations\\'+SelectOrganizationame+'\\Botdetails.json')
153
+ data = json.load(fl)
154
+ df = pd.DataFrame.from_dict(data, orient='columns')
155
+ print (df)
156
+ for p_id, p_info in data.items():
157
+ new.append(p_id)
158
+ return df,gr.update(choices=new, value=new[0])
159
+
160
+ def upload_file(org, bot , files):
161
+ file_paths = [file.name for file in files]
162
+ allfiles=file_paths
163
+ path = os.path.join(os.getcwd()+'\\Organizations\\', org,bot,'Inputs')
164
+ os.makedirs(path, exist_ok=True)
165
+
166
+ for file_path in file_paths:
167
+ destination_path = os.path.join(path, file_path)
168
+ if not os.path.exists(destination_path):
169
+ shutil.copy(file_path, destination_path)
170
+
171
+ return file_paths
172
+
173
+ def train(files):
174
+ for file in files:
175
+ print(file.name)
176
+ if file.name.endswith(".pdf"):
177
+ loader = PyPDFLoader(file.name)
178
+ documents = loader.load()
179
+ return "Training Done"
180
+ chat_history = []
181
+
182
+ def construct_index(directory_path):
183
+ file_paths=[]
184
+ for root, directories, files in os.walk(directory_path):
185
+ for file_name in files:
186
+ file_path = os.path.join(root, file_name)
187
+ file_paths.append(file_path)
188
+
189
+
190
+ for file in file_paths:
191
+ if file.endswith(".pdf"):
192
+ loader = PyPDFLoader(file)
193
+ documents = loader.load()
194
+ return documents
195
+
196
+ data_file_path = "deployment_archive_data.json"
197
+ if not os.path.exists(data_file_path):
198
+ with open(data_file_path, "w") as file:
199
+ json.dump([], file)
200
+
201
+ def deployment_or_archive(action_type):
202
+ timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
203
+
204
+ # Load existing data from the JSON file
205
+ with open(data_file_path, "r") as file:
206
+ data = json.load(file)
207
+
208
+ # Add the new action to the data list
209
+ data.append({"action": action_type, "timestamp": timestamp})
210
+
211
+ # Write back the updated data to the JSON file
212
+ with open(data_file_path, "w") as file:
213
+ json.dump(data, file)
214
+
215
+ def deploy_bot(org_name, bot_name):
216
+
217
+ deployment_path = os.path.join(os.getcwd(), "Deployment")
218
+ os.makedirs(deployment_path, exist_ok=True)
219
+
220
+
221
+ org_details_path = os.path.join(os.getcwd(), "Organizationdetails.json")
222
+ org_deploy_path = os.path.join(deployment_path, "Organizationdetails.json")
223
+ shutil.copy(org_details_path, org_deploy_path)
224
+
225
+
226
+ org_path = os.path.join(os.getcwd(), "Organizations", org_name)
227
+ bot_details_path = os.path.join(org_path, "Botdetails.json")
228
+ bot_deploy_path = os.path.join(deployment_path, "Botdetails.json")
229
+ shutil.copy(bot_details_path, bot_deploy_path)
230
+
231
+
232
+ bot_path = os.path.join(org_path, bot_name)
233
+ model_files = os.listdir(bot_path)
234
+ for file_name in model_files:
235
+ file_path = os.path.join(bot_path, file_name)
236
+ deploy_file_path = os.path.join(deployment_path, file_name)
237
+ shutil.copy(file_path, deploy_file_path)
238
+
239
+ return "Bot deployed successfully to the Deployment directory."
240
+
241
+
242
+
243
+
244
+ def chatbot(input_text):
245
+ global chat_history
246
+ query = input_text
247
+ text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)
248
+ texts = text_splitter.split_documents(documents)
249
+ embeddings = OpenAIEmbeddings()
250
+ db = Chroma.from_documents(texts, embeddings)
251
+ retriever = db.as_retriever(search_type="similarity", search_kwargs={"k":2})
252
+ vectordbkwargs = {"search_distance": 0.9}
253
+ qa = ConversationalRetrievalChain.from_llm(OpenAI(), retriever,return_source_documents=True)
254
+ print(chat_history)
255
+ if chat_history==[]:
256
+ result = qa({"question": query,"chat_history": chat_history, "vectordbkwargs": {"search_distance": 0.9}})
257
+ else:
258
+ result = qa({"question": query, "chat_history": chat_history, "vectordbkwargs": {"search_distance": 0.9}})
259
+ chat_history = [(query, result["answer"])]
260
+ return result["answer"]
261
+
262
+
263
+ with demo:
264
+ gr.Markdown("BotRite")
265
+
266
+ with gr.Tabs() :
267
+ with gr.TabItem("ChatBot"):
268
+ with gr.Row():
269
+ SelectOrg = gr.Dropdown(options_org ,label="Select Organization" )
270
+
271
+ with gr.Row():
272
+ Selectbot = gr.Dropdown( label="Select Bot" ,choices=options_bot,
273
+ value=options_bot[0], interactive=True)
274
+ # Selectbot = gr.Radio(options_bot ,label="Select Bot")
275
+
276
+ with gr.Column():
277
+ query_input = gr.Textbox(lines=7, label="Enter your text")
278
+ ask_button = gr.Button("Ask")
279
+ with gr.TabItem("Settings"):
280
+ with gr.Tabs():
281
+
282
+ with gr.TabItem("Create Organization:"):
283
+ with gr.Row():
284
+ org_name = gr.Textbox(label="Name",info="Your name / Nickname",placeholder="Enter you organization full name")
285
+ org_handle = gr.Textbox(label="Handle Name",info="Your unique organization name", placeholder="Enter Organization handle name")
286
+ output_org =gr.Textbox(label='Status')
287
+ with gr.Row():
288
+ Createorg_button = gr.Button("Create Organization")
289
+ Clearorg_button = gr.Button("Clear", variant="stop")
290
+
291
+ with gr.TabItem("Bot Details"):
292
+ SelectOrganizationame = gr.Dropdown(options_org, label="Select Organization")
293
+ with gr.Tabs():
294
+ with gr.TabItem("Your Bots:"):
295
+ with gr.Row():
296
+ with gr.Column():
297
+ #SelectOrganizationame = gr.components.Dropdown(options_org, label="Select Organization")
298
+ botdf=gr.Dataframe(headers=["Bot_Name", "Organizationame", "Created_by", "Created_Time","Bot_Handle_Name","Bot_Image","Tools","OpenAI_API_key",
299
+ "Initial_Message","Intro_Message","Rules"], label="Bot Details")
300
+
301
+ with gr.TabItem("Create Bot:"):
302
+
303
+ Organizationame =SelectOrganizationame
304
+ botname = gr.Textbox(label="Bot Name",info="Your bot name / Nickname", placeholder="Enter bot full name")
305
+ bothandle = gr.Textbox(label="Bot Handle Name",info="Your unique bot name" ,placeholder="Enter bot handle name")
306
+ image_button = gr.File(label="Select bot image")
307
+ botllm = gr.components.CheckboxGroup(['OpenAI', 'Dolly', 'Q&A Model'],label="Tools")
308
+ # model = gr.components.Dropdown(Options1, label="Model")
309
+ openai_key = gr.Textbox(label="You OpenAI API key", type="password" , info="Add your OpenAi Key click the link to create new or copy exsisting key from your openai account https://platform.openai.com/account/api-keys")
310
+ initailsmsg = gr.Textbox(label="Initial Message", placeholder="This message will be shared by bot as intro​" , info="This message will be shared by bot as intro​")
311
+ intromsg = gr.Textbox(label="Intro Message", placeholder="This message will be sent to bot as prefix to first message​", info="This message will be sent to bot as prefix to first message​")
312
+ rules = gr.Textbox(label="Rules", placeholder="These rules will be sent to bot as prefix to first message (after introduction)​", info="These rules will be sent to bot as prefix to first message (after introduction)​")
313
+ output_bot =gr.Textbox(label='Status')
314
+ Createbot_button = gr.Button("Create Bot")
315
+ with gr.TabItem("Bot Configuration"):
316
+ Selectbotconfig = gr.Dropdown(label="Select Bot",choices=options_bot,
317
+ value=options_bot[0], interactive=True)
318
+ with gr.TabItem("Load Data"):
319
+ with gr.Row():
320
+ with gr.Column():
321
+ Train_Fileselect_button = gr.UploadButton("Upload PDF Files", file_types=[".pdf"], file_count="multiple")
322
+ file_output = gr.File()
323
+ with gr.Column():
324
+ Train_button = gr.Button("Train Data")
325
+ Train_output =gr.Textbox(label='Status')
326
+
327
+ with gr.TabItem("Chat with your bot"):
328
+ with gr.Row():
329
+ with gr.Column():
330
+ query_input = gr.Textbox(lines=7, label="Enter your text")
331
+ ask_button = gr.Button("Ask")
332
+ with gr.Column():
333
+ text_output=gr.Text(label="Your Bot Answer")
334
+ with gr.TabItem("Deploy"):
335
+ with gr.Row():
336
+ archive_button = gr.Button("Archive")
337
+ deploy_button = gr.Button("Deploy")
338
+ deploy_archive_output = gr.Textbox(label='Status')
339
+
340
+ archive_output = gr.Textbox(label='Status')
341
+ publish_button = gr.Button("Publish")
342
+ with gr.TabItem("Logs"):
343
+ with gr.Column():
344
+ Selectlog = gr.Dropdown( label="Select Log")
345
+ logview = gr.Text(label="Log")
346
+ #def Dropdown_Org(x):
347
+
348
+ SelectOrg.change(fn=loadbotdata, inputs=SelectOrg,outputs=Selectbot )
349
+ SelectOrganizationame.change(fn=loadbotdataasdf, inputs=SelectOrganizationame,outputs=[botdf,Selectbotconfig])
350
+ Createorg_button.click(fn=Create_Organization,inputs=[org_name, org_handle], outputs=output_org)
351
+
352
+ archive_button.click(fn=deploy_bot, inputs=[SelectOrganizationame, Selectbotconfig], outputs=archive_output)
353
+ deploy_button.click(lambda: deployment_or_archive("deploy"), outputs=deploy_archive_output)
354
+ archive_button.click(lambda: deployment_or_archive("archive"), outputs=deploy_archive_output)
355
+
356
+ Clearorg_button.click(lambda : [None,None,None], inputs=None, outputs=[org_name,org_handle,output_org])
357
+ Createbot_button.click(fn=Create_Bot,inputs=[Organizationame, botname, bothandle,image_button,botllm,openai_key,initailsmsg,intromsg,rules], outputs=output_bot)
358
+
359
+ Train_Fileselect_button.upload(upload_file,inputs=[SelectOrganizationame, Selectbotconfig,Train_Fileselect_button], outputs=[file_output])
360
+ Train_button.click(fn=train,inputs=Train_Fileselect_button, outputs=Train_output)
361
+
362
+ demo.launch()