johnnyfivefingers commited on
Commit
bab63dc
·
1 Parent(s): 8c6f75a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +61 -4
app.py CHANGED
@@ -1,7 +1,64 @@
1
  import gradio as gr
 
 
 
2
 
3
- def greet(name):
4
- return "Hello " + name + "!!"
5
 
6
- iface = gr.Interface(fn=greet, inputs="text", outputs="text")
7
- iface.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
+ from sentence_transformers import SentenceTransformer
3
+ from sklearn.metrics.pairwise import cosine_similarity
4
+ from transformers import pipeline
5
 
6
+ # Load the pre-trained sentence transformer model
7
+ model = SentenceTransformer('distilbert-base-nli-stsb-mean-tokens')
8
 
9
+ # Load the pre-trained extractive QA model
10
+ qa_pipeline = pipeline('question-answering', model='distilbert-base-cased-distilled-squad')
11
+
12
+ # Define the example documents and the example security-related question
13
+ documents = [
14
+ "Data breaches are a common occurrence in today's digital world. Companies must take measures to protect sensitive information from unauthorized access or disclosure.",
15
+ "Phishing attacks are a type of cyberattack that use social engineering to trick users into divulging confidential information. Employees should be trained to recognize and avoid phishing scams.",
16
+ "The use of encryption can help prevent unauthorized access to data by encrypting it so that it can only be accessed by authorized users who have the decryption key.",
17
+ "A firewall is a network security system that monitors and controls incoming and outgoing network traffic based on predetermined security rules. It can help protect against unauthorized access to a network.",
18
+ "Access control is the process of limiting access to resources to only authorized users. It is an important aspect of information security and can be achieved through the use of authentication and authorization mechanisms.",
19
+ ]
20
+
21
+ question = "What measures can companies take to protect sensitive information from unauthorized access or disclosure?"
22
+
23
+ # Define the function to process the inputs and return the answer
24
+ def answer_question(document, question):
25
+ # Generate embeddings for the documents
26
+ doc_embeddings = model.encode(documents)
27
+
28
+ # Generate the query embedding
29
+ query_embedding = model.encode([question])
30
+
31
+ # Compute the cosine similarity between the query and the document embeddings
32
+ similarity_scores = cosine_similarity(query_embedding, doc_embeddings)
33
+
34
+ # Find the top 3 most similar documents
35
+ most_similar_idxs = similarity_scores.argsort()[0][-3:]
36
+
37
+ # Extract the answers from the top 3 most similar documents
38
+ answers = []
39
+ for idx in most_similar_idxs:
40
+ answer = qa_pipeline({'context': documents[idx], 'question': question})
41
+ answers.append(answer['answer'])
42
+
43
+ # Return the answers
44
+ return answers
45
+
46
+ # Define the input and output interfaces for the Gradio app
47
+ inputs = [
48
+ gr.inputs.Textbox(label="Document"),
49
+ gr.inputs.Textbox(label="Question"),
50
+ ]
51
+ outputs = gr.outputs.Textbox(label="Answer")
52
+
53
+ # Create the Gradio app
54
+ app = gr.Interface(fn=answer_question, inputs=inputs, outputs=outputs,
55
+ title="Security Question Answering",
56
+ description="Enter a security question and a document, and the app will return the answer based on the top 3 most similar documents.",
57
+ examples=[
58
+ ["Data breaches are a common occurrence in today's digital world. Companies must take measures to protect sensitive information from unauthorized access or disclosure.",
59
+ "What measures can companies take to protect sensitive information?"],
60
+ ["Phishing attacks are a type of cyberattack that use social engineering to trick users into divulging confidential information. Employees should be trained to recognize and avoid phishing scams.",
61
+ "What is a phishing attack?"],
62
+ ["The use of encryption can help prevent unauthorized access to data by encrypting it so that it can only be accessed by authorized users who have the decryption key.",
63
+ "What is encryption?"],
64
+ ["A firewall is a network security system that monitors and controls incoming and outgoing network traffic based