Spaces:
Sleeping
Sleeping
# -*- 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) | |