huohguohbo commited on
Commit
d6bab40
·
1 Parent(s): 0dd0be4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +94 -100
app.py CHANGED
@@ -1,105 +1,99 @@
1
- import openai
2
- import transformers
3
  import gradio as gr
 
4
 
5
- # Set up the OpenAI API client
6
- openai.api_key = "YOUR_API_KEY"
7
-
8
- # Define the chat function for OpenAI API
9
- def openai_chat(api_key, model, message):
10
- # Check if an API key has been provided
11
- if api_key is None:
12
- return "Please enter your OpenAI API key and try again."
13
-
14
- # Set up the OpenAI API request
15
- response = openai.Completion.create(
16
- engine=model,
17
- prompt=message,
18
- max_tokens=1024,
19
- n=1,
20
- stop=None,
21
- temperature=0.5,
22
- api_key=api_key,
23
- )
24
-
25
- # Extract the bot's response from the API request
26
- bot_response = response.choices[0].text.strip()
27
-
28
- return bot_response
29
-
30
- # Define the chat function for Hugging Face API
31
- def hf_chat(model_name, message):
32
- # Load the model and tokenizer
33
- model = transformers.pipeline("text-generation", model=model_name)
34
-
35
- # Generate a response from the model
36
- bot_response = model(message, max_length=1024, do_sample=True, temperature=0.7)[0]["generated_text"]
37
-
38
- return bot_response
39
-
40
- # Define the Gradio interface for chatbot
41
- api_key_input = gr.inputs.Textbox(label="OpenAI API Key", default=None, block="sidebar")
42
- model_input = gr.inputs.Dropdown(
43
- label="Select OpenAI model",
44
- choices=["davinci", "curie", "babbage"],
45
- default="davinci",
46
- block="sidebar"
47
- )
48
- hf_model_input = gr.inputs.Dropdown(
49
- label="Select Hugging Face model",
50
- choices=["microsoft/DialoGPT-large", "Salesforce/codegen-2B-multi", "microsoft/DialoGPT-small"],
51
- default="microsoft/DialoGPT-large",
52
- block="sidebar"
53
- )
54
- mode_input = gr.inputs.Dropdown(
55
- label="Select chatbot mode",
56
- choices=["OpenAI", "Hugging Face"],
57
- default="OpenAI",
58
- block="sidebar"
59
- )
60
- message_input = gr.inputs.Textbox(label="Enter your message here", block="input")
61
- output = gr.outputs.Textbox(label="Bot response", block="output")
62
-
63
- # Define the chat window
64
- chat_window = []
65
-
66
- def chatbot(chat_window, message, mode, model, hf_model, api_key, send_button, clear_button):
67
- if clear_button:
68
- chat_window.clear()
69
- return "Chat history cleared."
70
- if send_button:
71
- if message:
72
- if mode == "Hugging Face":
73
- bot_response = hf_chat(hf_model, message)
74
- else:
75
- bot_response = openai_chat(api_key, model, message)
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
- outputs=output,
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
- # Launch the page
105
- chat_interface.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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()