Piyushmryaa commited on
Commit
b3e8a56
1 Parent(s): aa15917

revert app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -93
app.py CHANGED
@@ -1,99 +1,51 @@
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
- if not input:
72
- return
73
- result = word_tokenize(input)
74
- word_pos= nltk.pos_tag(result)
75
- pos = [ i[1] for i in word_pos]
76
- for i in range(len(pos)):
77
- if pos[i] =='NN':
78
- pos[i] = 1
79
- elif pos[i] =='DT':
80
- pos[i] = 2
81
- elif pos[i] =='JJ':
82
- pos[i] = 3
83
- else:
84
- pos[i]= 4
85
- return pos
86
- st.title('Chunk tagging')
87
- input = st.text_input('Input the pos tags')
88
-
89
- import nltk
90
- from nltk.tokenize import word_tokenize
91
-
92
- inputs = input_(input)
93
- output = predictsentence(inputs)
94
- st.write(output)
95
-
96
- # import pandas as pd
97
- # data = output
98
- # df = pd.DataFrame.from_dict(data)
99
- # st.dataframe(df)
 
1
+ import streamlit as st
2
+ from mygrad import Layer, Value
 
 
 
 
 
 
 
3
  import pickle
 
 
 
 
 
 
 
 
 
 
4
 
5
+ # Define the predict function
6
+ def predict(x):
7
+ x1 = hiddenLayer1(x)
8
+ final = outputLayer([x1] + x)
9
+ return final.data
10
 
11
+ # Load model
 
12
  def loadModel():
13
+ neuron1weightsbias, outputneuronweightsbias = [], []
14
+ with open(f'parameters/neuron1weightsbias_fn_reLu.pckl', 'rb') as file:
15
  neuron1weightsbias = pickle.load(file)
16
+ with open('parameters/outputneuronweightsbias2.pckl', 'rb') as file:
17
+ outputneuronweightsbias = pickle.load(file)
18
+ hiddenLayer1_ = Layer(10, 1, 'reLu')
19
+ outputLayer_ = Layer(11, 1, 'sigmoid')
20
+
21
+ hiddenLayer1_.neurons[0].w = [Value(i) for i in neuron1weightsbias[:-1]]
22
+ hiddenLayer1_.neurons[0].b = Value(neuron1weightsbias[-1])
23
+
24
+ outputLayer_.neurons[0].w = [Value(i) for i in outputneuronweightsbias[:-1]]
25
+ outputLayer_.neurons[0].b = Value(outputneuronweightsbias[-1])
26
+ return hiddenLayer1_, outputLayer_
27
+
28
+ hiddenLayer1, outputLayer = loadModel()
29
+
30
+ st.title("Neural Network Prediction")
31
+
32
+ st.header("Input")
33
+ inputs = st.text_input("Input 10 digits Binary no")
34
+ input = []
35
+ flag = 0
36
+ if len(inputs)!=10:
37
+ st.write("Error: Input not equal to 10 bits")
38
+ flag =1
39
+ for i in inputs:
40
+ if i!='0' and i!='1':
41
+ st.write("Please input Binary number only")
42
+ flag = 1
43
  else:
44
+ input.append(int(i))
45
+
46
+ # Prediction
47
+ if st.button("Predict"):
48
+ if flag:
49
+ st.stop()
50
+ result = predict(input)
51
+ st.success(f"The prediction is: {result}")