File size: 5,254 Bytes
ee5a6c6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c072928
ee5a6c6
 
 
 
 
 
 
 
 
 
 
 
c072928
ee5a6c6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c072928
ee5a6c6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c072928
ee5a6c6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c072928
ee5a6c6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#!/usr/bin/env python
# coding: utf-8

# In[52]:


# !pip install -q pymilvus towhee gradio


# In[53]:


#!curl -L https://github.com/pankajkishore/Cognitive-Project/blob/master/latest_ticket_data.csv -O


# In[1]:


import pandas as pd
df = pd.read_csv('latest_ticket_data.csv')
df.head()


# In[2]:


df.shape


# In[3]:


df['length'] = df['description'].apply(
    lambda row: min(len(row.split(" ")), len(row)) if isinstance(row, str) else None
)
df['length'].max()


# In[4]:


df.description[14]


# In[5]:


df.shape


# In[6]:


id_category = df.set_index('id')['category'].to_dict()


# In[7]:


id_description = df.set_index('id')['description'].to_dict()


# In[8]:


id_description[12]


# In[9]:


id_category[10]


# In[11]:


from milvus import default_server
from pymilvus import connections, utility
default_server.start()


# In[12]:


from pymilvus import connections, FieldSchema, CollectionSchema, DataType, Collection, utility


# In[24]:


# # Milvus parameters
connections.connect(host='127.0.0.1', port='19530')


# In[25]:


default_server.listen_port


# In[17]:


def create_milvus_collection(collection_name, dim):
    connections.connect(host='127.0.0.1', port='19530')
    if utility.has_collection(collection_name):
        utility.drop_collection(collection_name)
    
    fields = [
    FieldSchema(name='id', dtype=DataType.VARCHAR, descrition='ids', max_length=500, is_primary=True, auto_id=False),
    FieldSchema(name='embedding', dtype=DataType.FLOAT_VECTOR, descrition='embedding vectors', dim=dim)
    ]
    schema = CollectionSchema(fields=fields, description='reverse text search')
    collection = Collection(name=collection_name, schema=schema)

    # create IVF_FLAT index for collection.
    index_params = {
        'metric_type':'L2',
        'index_type':"IVF_FLAT",
        'params':{"nlist":2048}
    }
    collection.create_index(field_name="embedding", index_params=index_params)
    return collection


# In[18]:


collection = create_milvus_collection('latest_ticket_data', 768)


# In[19]:


collection.load()


# In[26]:


from towhee import pipe, ops
import numpy as np
from towhee.datacollection import DataCollection

insert_pipe = (
    pipe.input('id', 'description', 'category')
        .map('description', 'vec', ops.text_embedding.dpr(model_name='facebook/dpr-ctx_encoder-single-nq-base'))
        .map('vec', 'vec', lambda x: x / np.linalg.norm(x, axis=0))
        .map(('id', 'vec'), 'insert_status', ops.ann_insert.milvus_client(host='127.0.0.1', 
                                                                          port='19530',
                                                                          collection_name='latest_ticket_data'))
        .output()
)


# In[ ]:


#  File "/Users/www.abcom.in/Documents/milvus/.milvusenv/lib/python3.11/site-packages/transformers/models/bert/modeling_bert.py", line 238, in forward
#     embeddings += position_embeddings
# RuntimeError: The size of tensor a (1002) must match the size of tensor b (512) at non-singleton dimension 1


# In[27]:


import csv
with open('latest_ticket_data.csv', encoding='utf-8') as f:
    reader = csv.reader(f)
    next(reader)
    for row in reader:
        insert_pipe(*row)


# In[28]:


collection.load()


# In[29]:


print('Total number of inserted data is {}.'.format(collection.num_entities))


# In[30]:


ans_pipe = (
    pipe.input('description')
        .map('description', 'vec', ops.text_embedding.dpr(model_name="facebook/dpr-ctx_encoder-single-nq-base"))
        .map('vec', 'vec', lambda x: x / np.linalg.norm(x, axis=0))
        .map('vec', 'res', ops.ann_search.milvus_client(host='127.0.0.1', 
                                                        port='19530',
                                                        collection_name='latest_ticket_data',
                                                        limit=1))
        .map('res', 'category', lambda x: [id_category[int(i[0])] for i in x])
        .output('description', 'category')
)


# In[31]:


ans = ans_pipe('report hi please attached report user take appropriate actions order agent her computer')


# In[32]:


ans = DataCollection(ans)
ans.show()


# In[33]:


import towhee
def chat(message, history):
    history = history or []
    ans_pipe = (
        pipe.input('description')
            .map('description', 'vec', ops.text_embedding.dpr(model_name="facebook/dpr-ctx_encoder-single-nq-base"))
            .map('vec', 'vec', lambda x: x / np.linalg.norm(x, axis=0))
            .map('vec', 'res', ops.ann_search.milvus_client(host='127.0.0.1', port='19530', collection_name='latest_ticket_data', limit=1))
            .map('res', 'category', lambda x: [id_category[int(i[0])] for i in x])
            .output('description', 'category')
    )

    response = ans_pipe(message).get()[1][0]
    history.append((message, response))
    return history, history


# In[34]:


import gradio

collection.load()
chatbot = gradio.Chatbot(color_map=("green", "gray"))
interface = gradio.Interface(
    chat,
    ["text", "state"],
    [chatbot, "state"],
    allow_screenshot=False,
    allow_flagging="never",
)
interface.launch(inline=True, share=True)


# In[ ]:





# In[ ]:





# In[ ]:





# In[ ]: