Canstralian commited on
Commit
9b9c9ac
·
verified ·
1 Parent(s): 535152f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -21
app.py CHANGED
@@ -1,29 +1,26 @@
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
3
 
4
- """
5
- For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
6
- """
7
- client = InferenceClient("bert-base-uncased") # Replace with a relevant model for classification or detection
8
 
9
  def detect_passwords(text, threshold=0.9):
10
  """
11
- Simulates BERT model usage to detect possible passwords.
12
  :param text: Input text containing potential passwords.
13
  :param threshold: Confidence score above which a pattern is flagged.
14
  :return: Flagged patterns and their confidence scores.
15
  """
16
- # Simulated model output (replace with actual BERT inference if supported)
17
- tokens = text.split() # Tokenize the input text (basic example)
18
- flagged_items = []
19
- for token in tokens:
20
- # Simulate a condition where tokens matching a simple regex-like pattern are flagged
21
- if len(token) > 6 and any(c.isdigit() for c in token) and any(c.isalpha() for c in token):
22
- # Simulate confidence score
23
- confidence_score = 0.95 # Example confidence for demo purposes
24
- if confidence_score > threshold:
25
- flagged_items.append((token, confidence_score))
26
 
 
 
 
 
 
 
27
  if not flagged_items:
28
  return "No passwords detected."
29
  else:
@@ -49,16 +46,14 @@ def respond(
49
  if val[1]:
50
  messages.append({"role": "assistant", "content": val[1]})
51
 
52
- # Simulate BERT-driven password detection for the user's message
53
  detected_passwords = detect_passwords(message)
54
  response = detected_passwords
55
 
56
- return response # Outputs directly for demo purposes
57
 
58
 
59
- """
60
- For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
61
- """
62
  demo = gr.ChatInterface(
63
  respond,
64
  additional_inputs=[
@@ -75,6 +70,5 @@ demo = gr.ChatInterface(
75
  ],
76
  )
77
 
78
-
79
  if __name__ == "__main__":
80
  demo.launch()
 
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
3
 
4
+ # Initialize the Hugging Face Inference client with a more relevant model (e.g., a fine-tuned password detection model)
5
+ client = InferenceClient("username/password-detection-model") # Replace with your trained model
 
 
6
 
7
  def detect_passwords(text, threshold=0.9):
8
  """
9
+ Detects potential passwords in text using a model from Hugging Face.
10
  :param text: Input text containing potential passwords.
11
  :param threshold: Confidence score above which a pattern is flagged.
12
  :return: Flagged patterns and their confidence scores.
13
  """
14
+ # Using a model inference to classify potential passwords
15
+ response = client.query({"inputs": text}) # Query the model for classification
16
+ predictions = response.get("predictions", [])
 
 
 
 
 
 
 
17
 
18
+ flagged_items = []
19
+ for pred in predictions:
20
+ token, confidence_score = pred["token"], pred["score"]
21
+ if confidence_score > threshold:
22
+ flagged_items.append((token, confidence_score))
23
+
24
  if not flagged_items:
25
  return "No passwords detected."
26
  else:
 
46
  if val[1]:
47
  messages.append({"role": "assistant", "content": val[1]})
48
 
49
+ # Use Hugging Face model to detect passwords in the user's message
50
  detected_passwords = detect_passwords(message)
51
  response = detected_passwords
52
 
53
+ return response # Output the result
54
 
55
 
56
+ # Gradio Interface for interaction
 
 
57
  demo = gr.ChatInterface(
58
  respond,
59
  additional_inputs=[
 
70
  ],
71
  )
72
 
 
73
  if __name__ == "__main__":
74
  demo.launch()