huohguohbo commited on
Commit
4395b89
·
1 Parent(s): 6ccefab

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -94
app.py CHANGED
@@ -1,99 +1,42 @@
 
 
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_generated_password = gr.outputs.Textbox(label='Generated Password')
54
- output_generated_key = gr.outputs.Textbox(label='Generated Key')
55
- output_encrypted_password = gr.outputs.Textbox(label='Encrypted Password')
56
- output_password_strength = gr.outputs.Label(label='Password Strength')
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, f'{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_action(generate_password_callback)
96
- interface.inputs[5].set_action(password_strength_callback)
97
- interface.inputs[6].set_action(password_decrypt_callback)
98
-
99
- interface.launch()
 
1
+ import openai
2
+ import re
3
  import gradio as gr
 
4
 
5
+ # Define a function to solve math problems
6
+ def solve_math_problem(prompt, api_key):
7
+ # Set up the OpenAI API credentials
8
+ openai.api_key = api_key
9
+
10
+ # Send the prompt to OpenAI's GPT-3 engine
11
+ response = openai.Completion.create(
12
+ engine="davinci",
13
+ prompt=prompt,
14
+ max_tokens=100,
15
+ n=1,
16
+ stop=None,
17
+ temperature=0.5,
18
+ )
19
+
20
+ # Extract the answer from the response
21
+ answer = response.choices[0].text.strip()
22
+
23
+ # Remove any non-numeric characters from the answer
24
+ answer = re.sub("[^0-9\.]", "", answer)
25
+
26
+ return float(answer)
27
+
28
+ # Define the Gradio interface
29
+ iface = gr.Interface(
30
+ fn=solve_math_problem,
31
+ inputs=[
32
+ gr.inputs.Textbox(label="Enter a math problem"),
33
+ gr.inputs.Textbox(label="Enter your OpenAI API key", type="password")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34
  ],
35
+ outputs=gr.outputs.Textbox(label="Answer"),
36
+ title="OpenAI Calculator",
37
+ description="Enter a math problem and let OpenAI solve it for you!",
38
+ live=True
 
 
 
 
39
  )
40
 
41
+ # Launch the Gradio interface
42
+ iface.launch()