aquibmoin commited on
Commit
faf73ae
·
verified ·
1 Parent(s): 7d94a09

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -19
app.py CHANGED
@@ -3,7 +3,7 @@ import fitz # PyMuPDF for extracting text from PDFs
3
  from transformers import AutoTokenizer, AutoModel
4
  import torch
5
  from sklearn.metrics.pairwise import cosine_similarity
6
-
7
 
8
  # Load the NASA-specific bi-encoder model and tokenizer
9
  bi_encoder_model_name = "nasa-impact/nasa-smd-ibm-st-v2"
@@ -18,8 +18,8 @@ def extract_text_from_pdf(pdf_file):
18
  text += page.get_text() # Extract text from each page
19
  return text
20
 
21
- # Function to generate embeddings from the text using the NASA Bi-Encoder
22
- def generate_embedding(text):
23
  # Tokenize the text and create input tensors
24
  inputs = bi_tokenizer(text, return_tensors="pt", padding=True, truncation=True, max_length=512)
25
 
@@ -31,14 +31,10 @@ def generate_embedding(text):
31
  # Mean pooling to get the final embedding for the text
32
  embedding = outputs.last_hidden_state.mean(dim=1).squeeze().numpy()
33
 
34
- return embedding
35
-
36
- # Function to compute the cosine similarity between two embeddings
37
- def compute_cosine_similarity(embedding1, embedding2):
38
- # Reshape the embeddings and calculate cosine similarity
39
- embedding1 = embedding1.reshape(1, -1)
40
- embedding2 = embedding2.reshape(1, -1)
41
- return cosine_similarity(embedding1, embedding2)[0][0]
42
 
43
  # Function to handle the full workflow: extract text, generate embeddings, and compute similarity
44
  def compare_pdfs(pdf1, pdf2):
@@ -46,20 +42,31 @@ def compare_pdfs(pdf1, pdf2):
46
  text1 = extract_text_from_pdf(pdf1)
47
  text2 = extract_text_from_pdf(pdf2)
48
 
49
- # Generate embeddings for both texts using the NASA Bi-Encoder
50
- embedding1 = generate_embedding(text1)
51
- embedding2 = generate_embedding(text2)
52
 
53
  # Compute cosine similarity between the two embeddings
54
  similarity_score = compute_cosine_similarity(embedding1, embedding2)
55
-
56
- # Return the similarity score
57
- return f"The cosine similarity between the two PDF documents is: {similarity_score:.4f}"
58
 
59
- # Gradio interface: accept two PDF files and output cosine similarity score
 
 
 
 
 
 
 
 
 
60
  inputs = [gr.File(label="Upload Human SCDD"), gr.File(label="Upload AI SCDD")]
61
- outputs = gr.Textbox(label="Cosine Similarity Score")
 
 
 
 
62
 
63
  # Set up the Gradio interface
64
  gr.Interface(fn=compare_pdfs, inputs=inputs, outputs=outputs, title="AI-Human SCDD Similarity Checker with NASA Bi-Encoder").launch()
65
 
 
 
3
  from transformers import AutoTokenizer, AutoModel
4
  import torch
5
  from sklearn.metrics.pairwise import cosine_similarity
6
+ import numpy as np
7
 
8
  # Load the NASA-specific bi-encoder model and tokenizer
9
  bi_encoder_model_name = "nasa-impact/nasa-smd-ibm-st-v2"
 
18
  text += page.get_text() # Extract text from each page
19
  return text
20
 
21
+ # Function to generate embeddings and return dimensions
22
+ def generate_embedding_with_dim(text):
23
  # Tokenize the text and create input tensors
24
  inputs = bi_tokenizer(text, return_tensors="pt", padding=True, truncation=True, max_length=512)
25
 
 
31
  # Mean pooling to get the final embedding for the text
32
  embedding = outputs.last_hidden_state.mean(dim=1).squeeze().numpy()
33
 
34
+ # Get the number of dimensions
35
+ embedding_dim = embedding.shape[0]
36
+
37
+ return embedding, f"Embedding Dimensions: {embedding_dim}"
 
 
 
 
38
 
39
  # Function to handle the full workflow: extract text, generate embeddings, and compute similarity
40
  def compare_pdfs(pdf1, pdf2):
 
42
  text1 = extract_text_from_pdf(pdf1)
43
  text2 = extract_text_from_pdf(pdf2)
44
 
45
+ # Generate embeddings and get their dimensions
46
+ embedding1, dim1 = generate_embedding_with_dim(text1)
47
+ embedding2, dim2 = generate_embedding_with_dim(text2)
48
 
49
  # Compute cosine similarity between the two embeddings
50
  similarity_score = compute_cosine_similarity(embedding1, embedding2)
 
 
 
51
 
52
+ # Return similarity score + embedding dimensions
53
+ return f"The cosine similarity between the two PDFs is: {similarity_score:.4f}", dim1, dim2
54
+
55
+ # Function to compute the cosine similarity between two embeddings
56
+ def compute_cosine_similarity(embedding1, embedding2):
57
+ embedding1 = embedding1.reshape(1, -1)
58
+ embedding2 = embedding2.reshape(1, -1)
59
+ return cosine_similarity(embedding1, embedding2)[0][0]
60
+
61
+ # Gradio interface: accept two PDFs, show similarity + embedding dimensions
62
  inputs = [gr.File(label="Upload Human SCDD"), gr.File(label="Upload AI SCDD")]
63
+ outputs = [
64
+ gr.Textbox(label="Cosine Similarity Score"),
65
+ gr.Textbox(label="Embedding Dimensions (PDF 1)"),
66
+ gr.Textbox(label="Embedding Dimensions (PDF 2)")
67
+ ]
68
 
69
  # Set up the Gradio interface
70
  gr.Interface(fn=compare_pdfs, inputs=inputs, outputs=outputs, title="AI-Human SCDD Similarity Checker with NASA Bi-Encoder").launch()
71
 
72
+