File size: 2,134 Bytes
9f0f035
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# -*- coding: utf-8 -*-
"""usemodel.ipynb

Automatically generated by Colaboratory.

Original file is located at
    https://colab.research.google.com/drive/1c8Qtf9TWr3apElEv2uDgCD_MQwHnmw0B
"""

import pickle
from mygrad import Neuron, Value
import streamlit as st 
def convertToOneHotEncode(tags):
    tag1 = tags[0]
    tag2 = tags[1]
    vec1 = [0]*5
    vec2 = [0]*4
    vec1[tag1] = 1
    vec2[tag2-1] = 1
    vec1.extend(vec2)



    return vec1
def loadModel():
    neuron1weightsbias = []
    with open(f'weights.pkl', 'rb') as file:
        neuron1weightsbias = pickle.load(file)
    neuron = Neuron(10)

    neuron.w = [Value(i) for i in neuron1weightsbias[:-1]]
    neuron.b = Value(neuron1weightsbias[-1])
    return neuron

import json
def loadjson(filepath):
    data = []
    with open(filepath, 'rb') as file:
        for line in file:
            data.append(json.loads(line))
    return data

data = loadjson('data/train.jsonl')
data2 = loadjson('data/test.jsonl')
X = [element['pos_tags'] for element in data] + [element['pos_tags'] for element in data2]
Y = [element['chunk_tags'] for element in data] + [element['chunk_tags'] for element in data2]

n = loadModel()

def predictsentence(postagsOfSentence):
    if postagsOfSentence:
        postagsOfSentence = [0] + postagsOfSentence
    else:
        return
    xnew = []
    for ix in range(1, len(postagsOfSentence)):
        xnew.append([ postagsOfSentence[ix-1], postagsOfSentence[ix]])
    for ix, pair in enumerate(xnew):
        xnew[ix] = convertToOneHotEncode(pair)
    w = Value(0)
    chunks = []
    for ix2, wordpair in enumerate(xnew):
        xinput = [w] + wordpair
        w = n(xinput)
        if w.data > 0.5:
            chunks.append(1)
        else:
            chunks.append(0)
    return chunks
def input_(input):
    input = input.split(',')
    inputs = [int(i) for i in input]
    return inputs
st.title('Chunck tagging')
input = st.text_input('Input the pos tags')
inputs = input_(input)
output = predictsentence(inputs)
st.write(output)

# import pandas as pd
# data = output
# df = pd.DataFrame.from_dict(data)
# st.dataframe(df)