Pankaj Singh Rawat commited on
Commit
dbfbc03
0 Parent(s):

Initial commit

Browse files
.gitattributes ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ *.7z filter=lfs diff=lfs merge=lfs -text
2
+ *.arrow filter=lfs diff=lfs merge=lfs -text
3
+ *.bin filter=lfs diff=lfs merge=lfs -text
4
+ *.bz2 filter=lfs diff=lfs merge=lfs -text
5
+ *.ckpt filter=lfs diff=lfs merge=lfs -text
6
+ *.ftz filter=lfs diff=lfs merge=lfs -text
7
+ *.gz filter=lfs diff=lfs merge=lfs -text
8
+ *.h5 filter=lfs diff=lfs merge=lfs -text
9
+ *.joblib filter=lfs diff=lfs merge=lfs -text
10
+ *.lfs.* filter=lfs diff=lfs merge=lfs -text
11
+ *.mlmodel filter=lfs diff=lfs merge=lfs -text
12
+ *.model filter=lfs diff=lfs merge=lfs -text
13
+ *.msgpack filter=lfs diff=lfs merge=lfs -text
14
+ *.npy filter=lfs diff=lfs merge=lfs -text
15
+ *.npz filter=lfs diff=lfs merge=lfs -text
16
+ *.onnx filter=lfs diff=lfs merge=lfs -text
17
+ *.ot filter=lfs diff=lfs merge=lfs -text
18
+ *.parquet filter=lfs diff=lfs merge=lfs -text
19
+ *.pb filter=lfs diff=lfs merge=lfs -text
20
+ *.pickle filter=lfs diff=lfs merge=lfs -text
21
+ *.pkl filter=lfs diff=lfs merge=lfs -text
22
+ *.pt filter=lfs diff=lfs merge=lfs -text
23
+ *.pth filter=lfs diff=lfs merge=lfs -text
24
+ *.rar filter=lfs diff=lfs merge=lfs -text
25
+ *.safetensors filter=lfs diff=lfs merge=lfs -text
26
+ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
27
+ *.tar.* filter=lfs diff=lfs merge=lfs -text
28
+ *.tar filter=lfs diff=lfs merge=lfs -text
29
+ *.tflite filter=lfs diff=lfs merge=lfs -text
30
+ *.tgz filter=lfs diff=lfs merge=lfs -text
31
+ *.wasm filter=lfs diff=lfs merge=lfs -text
32
+ *.xz filter=lfs diff=lfs merge=lfs -text
33
+ *.zip filter=lfs diff=lfs merge=lfs -text
34
+ *.zst filter=lfs diff=lfs merge=lfs -text
35
+ *tfevents* filter=lfs diff=lfs merge=lfs -text
36
+ *.keras filter=lfs diff=lfs merge=lfs -text
.gitignore ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ seasme
2
+ data
C3W3_Assignment.ipynb ADDED
The diff for this file is too large to render. See raw diff
 
README.md ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: Questions Similarity
3
+ emoji: 🐠
4
+ colorFrom: gray
5
+ colorTo: gray
6
+ sdk: gradio
7
+ sdk_version: 4.44.1
8
+ app_file: app.py
9
+ pinned: false
10
+ short_description: Checks similarity among two questions using Seasme network.
11
+ ---
12
+
13
+ Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
__pycache__/inference.cpython-312.pyc ADDED
Binary file (2.04 kB). View file
 
app.py ADDED
@@ -0,0 +1,20 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from inference import predict
2
+ import gradio as gr
3
+
4
+ # Function to call the FastAPI backend
5
+ def predict(question1, question2, threshold):
6
+
7
+ similarity, is_same = predict(question1, question2, threshold, verbose=True)
8
+
9
+ return similarity, is_same
10
+
11
+
12
+ # Launch the Gradio interface
13
+ if __name__ == "__main__":
14
+ gr.Interface(predict,
15
+ inputs=[gr.Textbox(placeholder="Is Leonel Messi the Goat ?"),
16
+ gr.Textbox(placeholder="Is Leonel Messi the best player ever?"),
17
+ gr.Slider(minimum=0.0, maximum=1.0)],
18
+ outputs=["number", "textbox"],
19
+ description="This app tells us whether the two questions are similar or not"
20
+ )
inference.py ADDED
@@ -0,0 +1,45 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import tensorflow as tf
2
+ import numpy
3
+ model = tf.keras.models.load_model('./model/trained_model.keras', safe_mode=False, compile=False)
4
+
5
+ # Show the model architecture
6
+ model.summary()
7
+
8
+ # GRADED FUNCTION: predict
9
+ def predict(question1, question2, threshold, verbose=False):
10
+ """Function for predicting if two questions are duplicates.
11
+
12
+ Args:
13
+ question1 (str): First question.
14
+ question2 (str): Second question.
15
+ threshold (float): Desired threshold.
16
+ verbose (bool, optional): If the results should be printed out. Defaults to False.
17
+
18
+ Returns:
19
+ bool: True if the questions are duplicates, False otherwise.
20
+ """
21
+ generator = tf.data.Dataset.from_tensor_slices((([question1], [question2]),None)).batch(batch_size=1)
22
+
23
+ ### START CODE HERE ###
24
+
25
+ # Call the predict method of your model and save the output into v1v2
26
+ v1v2 = model.predict(generator)
27
+ out_size = v1v2.shape[1]
28
+ # Extract v1 and v2 from the model output
29
+ v1 = v1v2[:,:int(out_size/2)]
30
+ v2 = v1v2[:,int(out_size/2):]
31
+ print(v1.shape)
32
+ # Take the dot product to compute cos similarity of each pair of entries, v1, v2
33
+ # Since v1 and v2 are both vectors, use the function tf.math.reduce_sum instead of tf.linalg.matmul
34
+ d = tf.reduce_sum(v1 * v2)
35
+ # Is d greater than the threshold?
36
+ res = d > threshold
37
+
38
+ ### END CODE HERE ###
39
+
40
+ if(verbose):
41
+ print("Q1 = ", question1, "\nQ2 = ", question2)
42
+ print("d = ", d.numpy())
43
+ print("res = ", res.numpy())
44
+
45
+ return d.numpy(), res.numpy()
model/trained_model.keras ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:882a852a01cf4bfc3c029089855f1082df1944b7ef2b32a142db341fd668fb9b
3
+ size 57860766
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ gradio
2
+ tensorflow==2.13.0
3
+ keras==2.13.1
4
+ numpy
5
+ pandas