Spaces:
Runtime error
Runtime error
Commit
·
d6bab40
1
Parent(s):
0dd0be4
Update app.py
Browse files
app.py
CHANGED
@@ -1,105 +1,99 @@
|
|
1 |
-
import openai
|
2 |
-
import transformers
|
3 |
import gradio as gr
|
|
|
4 |
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
)
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
chat_window.append(("User", message))
|
77 |
-
chat_window.append(("Bot", bot_response))
|
78 |
-
return "\n".join([f"{name}: {text}" for name, text in chat_window])
|
79 |
-
|
80 |
-
# Define the Gradio interface for chatbot
|
81 |
-
send_button = gr.inputs.Button(label="Send")
|
82 |
-
clear_button = gr.inputs.Button(label="Clear Chat History")
|
83 |
-
chat_interface = gr.Interface(
|
84 |
-
fn=chatbot,
|
85 |
-
inputs=[
|
86 |
-
message_input,
|
87 |
-
mode_input,
|
88 |
-
model_input,
|
89 |
-
hf_model_input,
|
90 |
-
api_key_input,
|
91 |
-
send_button,
|
92 |
-
clear_button
|
93 |
],
|
94 |
-
|
95 |
-
title="Chatbot",
|
96 |
-
description="Enter your message below to chat with an AI",
|
97 |
-
theme="compact",
|
98 |
-
allow_flagging=False,
|
99 |
-
allow_screenshot=False,
|
100 |
-
allow_share=False,
|
101 |
-
layout="vertical"
|
102 |
)
|
103 |
|
104 |
-
|
105 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
import random
|
3 |
|
4 |
+
import sentence_transformers
|
5 |
+
from sentence_transformers import SentenceTransformer
|
6 |
+
|
7 |
+
model = SentenceTransformer('distilbert-base-nli-stsb-mean-tokens')
|
8 |
+
|
9 |
+
def get_password_strength(password):
|
10 |
+
embeddings = model.encode([password])
|
11 |
+
vector = embeddings[0]
|
12 |
+
strength = int(round(vector.dot(vector) * 100))
|
13 |
+
return strength
|
14 |
+
|
15 |
+
def generate_password(length):
|
16 |
+
characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*()'
|
17 |
+
password = ''.join(random.choice(characters) for i in range(length))
|
18 |
+
return password
|
19 |
+
|
20 |
+
def encrypt_password(password, key):
|
21 |
+
encrypted_password = []
|
22 |
+
for i in range(len(password)):
|
23 |
+
char_code = ord(password[i]) ^ ord(key[i % len(key)])
|
24 |
+
encrypted_password.append(str(char_code))
|
25 |
+
return ','.join(encrypted_password)
|
26 |
+
|
27 |
+
def decrypt_password(encrypted_password, key):
|
28 |
+
decrypted_password = []
|
29 |
+
encrypted_char_codes = encrypted_password.split(',')
|
30 |
+
for i in range(len(encrypted_char_codes)):
|
31 |
+
char_code = int(encrypted_char_codes[i]) ^ ord(key[i % len(key)])
|
32 |
+
decrypted_password.append(chr(char_code))
|
33 |
+
return ''.join(decrypted_password)
|
34 |
+
|
35 |
+
def password_generator(password_length):
|
36 |
+
password = generate_password(password_length)
|
37 |
+
key = generate_password(password_length)
|
38 |
+
encrypted_password = encrypt_password(password, key)
|
39 |
+
return {'password': password, 'key': key, 'encrypted_password': encrypted_password}
|
40 |
+
|
41 |
+
def password_strength(password):
|
42 |
+
return {'password_strength': get_password_strength(password)}
|
43 |
+
|
44 |
+
def password_decrypt(encrypted_password, key):
|
45 |
+
password = decrypt_password(encrypted_password, key)
|
46 |
+
return {'password': password}
|
47 |
+
|
48 |
+
input_password_length = gr.inputs.Slider(minimum=4, maximum=32, step=1, default=12, label='Password Length')
|
49 |
+
input_password = gr.inputs.Textbox(label='Password')
|
50 |
+
input_encrypted_password = gr.inputs.Textbox(label='Encrypted Password')
|
51 |
+
input_key = gr.inputs.Textbox(label='Key')
|
52 |
+
|
53 |
+
output_password_strength = gr.outputs.ProgressBar(label='Password Strength')
|
54 |
+
output_generated_password = gr.outputs.Textbox(label='Generated Password')
|
55 |
+
output_generated_key = gr.outputs.Textbox(label='Generated Key')
|
56 |
+
output_encrypted_password = gr.outputs.Textbox(label='Encrypted Password')
|
57 |
+
output_decrypted_password = gr.outputs.Textbox(label='Decrypted Password')
|
58 |
+
|
59 |
+
interface = gr.Interface(
|
60 |
+
[
|
61 |
+
input_password_length,
|
62 |
+
input_password,
|
63 |
+
input_encrypted_password,
|
64 |
+
input_key,
|
65 |
+
gr.inputs.Button(label='Generate Password'),
|
66 |
+
gr.inputs.Button(label='Check Password Strength'),
|
67 |
+
gr.inputs.Button(label='Decrypt Password'),
|
68 |
+
],
|
69 |
+
[
|
70 |
+
output_generated_password,
|
71 |
+
output_generated_key,
|
72 |
+
output_encrypted_password,
|
73 |
+
output_password_strength,
|
74 |
+
output_decrypted_password,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
75 |
],
|
76 |
+
title='Password Generator and Encryption/Decryption System',
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
77 |
)
|
78 |
|
79 |
+
def generate_password_callback(password_length):
|
80 |
+
result = password_generator(password_length)
|
81 |
+
return [result['password'], result['key'], result['encrypted_password'], None, None]
|
82 |
+
|
83 |
+
def password_strength_callback(password):
|
84 |
+
result = password_strength(password)
|
85 |
+
return [None, None, None, result['password_strength'], None]
|
86 |
+
|
87 |
+
def password_decrypt_callback(encrypted_password, key):
|
88 |
+
result = password_decrypt(encrypted_password, key)
|
89 |
+
return [None, None, None, None, result['password']]
|
90 |
+
|
91 |
+
interface.inputs[4].set_uploadable(False)
|
92 |
+
interface.inputs[5].set_uploadable(False)
|
93 |
+
interface.inputs[6].set_uploadable(False)
|
94 |
+
|
95 |
+
interface.inputs[4].set_callback(generate_password_callback)
|
96 |
+
interface.inputs[5].set_callback(password_strength_callback)
|
97 |
+
interface.inputs[6].set_callback(password_decrypt_callback)
|
98 |
+
|
99 |
+
interface.launch()
|