Spaces:
Sleeping
Sleeping
Commit
•
9f0f035
1
Parent(s):
4a1997a
App.py file
Browse files
app.py
ADDED
@@ -0,0 +1,84 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# -*- coding: utf-8 -*-
|
2 |
+
"""usemodel.ipynb
|
3 |
+
|
4 |
+
Automatically generated by Colaboratory.
|
5 |
+
|
6 |
+
Original file is located at
|
7 |
+
https://colab.research.google.com/drive/1c8Qtf9TWr3apElEv2uDgCD_MQwHnmw0B
|
8 |
+
"""
|
9 |
+
|
10 |
+
import pickle
|
11 |
+
from mygrad import Neuron, Value
|
12 |
+
import streamlit as st
|
13 |
+
def convertToOneHotEncode(tags):
|
14 |
+
tag1 = tags[0]
|
15 |
+
tag2 = tags[1]
|
16 |
+
vec1 = [0]*5
|
17 |
+
vec2 = [0]*4
|
18 |
+
vec1[tag1] = 1
|
19 |
+
vec2[tag2-1] = 1
|
20 |
+
vec1.extend(vec2)
|
21 |
+
|
22 |
+
|
23 |
+
|
24 |
+
return vec1
|
25 |
+
def loadModel():
|
26 |
+
neuron1weightsbias = []
|
27 |
+
with open(f'weights.pkl', 'rb') as file:
|
28 |
+
neuron1weightsbias = pickle.load(file)
|
29 |
+
neuron = Neuron(10)
|
30 |
+
|
31 |
+
neuron.w = [Value(i) for i in neuron1weightsbias[:-1]]
|
32 |
+
neuron.b = Value(neuron1weightsbias[-1])
|
33 |
+
return neuron
|
34 |
+
|
35 |
+
import json
|
36 |
+
def loadjson(filepath):
|
37 |
+
data = []
|
38 |
+
with open(filepath, 'rb') as file:
|
39 |
+
for line in file:
|
40 |
+
data.append(json.loads(line))
|
41 |
+
return data
|
42 |
+
|
43 |
+
data = loadjson('data/train.jsonl')
|
44 |
+
data2 = loadjson('data/test.jsonl')
|
45 |
+
X = [element['pos_tags'] for element in data] + [element['pos_tags'] for element in data2]
|
46 |
+
Y = [element['chunk_tags'] for element in data] + [element['chunk_tags'] for element in data2]
|
47 |
+
|
48 |
+
n = loadModel()
|
49 |
+
|
50 |
+
def predictsentence(postagsOfSentence):
|
51 |
+
if postagsOfSentence:
|
52 |
+
postagsOfSentence = [0] + postagsOfSentence
|
53 |
+
else:
|
54 |
+
return
|
55 |
+
xnew = []
|
56 |
+
for ix in range(1, len(postagsOfSentence)):
|
57 |
+
xnew.append([ postagsOfSentence[ix-1], postagsOfSentence[ix]])
|
58 |
+
for ix, pair in enumerate(xnew):
|
59 |
+
xnew[ix] = convertToOneHotEncode(pair)
|
60 |
+
w = Value(0)
|
61 |
+
chunks = []
|
62 |
+
for ix2, wordpair in enumerate(xnew):
|
63 |
+
xinput = [w] + wordpair
|
64 |
+
w = n(xinput)
|
65 |
+
if w.data > 0.5:
|
66 |
+
chunks.append(1)
|
67 |
+
else:
|
68 |
+
chunks.append(0)
|
69 |
+
return chunks
|
70 |
+
def input_(input):
|
71 |
+
input = input.split(',')
|
72 |
+
inputs = [int(i) for i in input]
|
73 |
+
return inputs
|
74 |
+
st.title('Chunck tagging')
|
75 |
+
input = st.text_input('Input the pos tags')
|
76 |
+
inputs = input_(input)
|
77 |
+
output = predictsentence(inputs)
|
78 |
+
st.write(output)
|
79 |
+
|
80 |
+
# import pandas as pd
|
81 |
+
# data = output
|
82 |
+
# df = pd.DataFrame.from_dict(data)
|
83 |
+
# st.dataframe(df)
|
84 |
+
|