Hyphonical commited on
Commit
516038a
·
1 Parent(s): 4224594

✨ Add Ping and Hash functionalities; update Gradio interface for new features

Browse files
Files changed (1) hide show
  1. app.py +60 -19
app.py CHANGED
@@ -2,12 +2,13 @@ from pymongo import MongoClient
2
  from typing import Literal
3
  from bson import ObjectId
4
  from io import StringIO
 
5
  import datetime
6
  import requests
 
7
  import random
8
  import base64
9
  import gradio
10
- import qrcode
11
  import sys
12
  import os
13
 
@@ -45,7 +46,7 @@ def Weather(Location: str) -> str:
45
  Args:
46
  Location (str): The location for which to get the weather. E.g., "London", "France", "50.85045,4.34878" "~NASA"
47
  Returns:
48
- str: The current weather in the format "{Location}: ⛅️ 🌡️+{Deg}°C 🌬️↖{WindSpeed}km/h".
49
  '''
50
  return requests.get(f'https://wttr.in/{Location}?A&format=4').text
51
 
@@ -107,6 +108,34 @@ def ExecuteCode(Code: str) -> str:
107
  finally:
108
  sys.stdout = OldStdout
109
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
110
  # ╭───────────────────────────────────╮
111
  # │ Fun and Entertainment Tools │
112
  # ╰───────────────────────────────────╯
@@ -143,8 +172,6 @@ def WordCount(Text: str, Choice: Literal['words', 'characters']) -> int:
143
  Args:
144
  Text (str): The text to count words in.
145
  Choice (Literal['words', 'characters']): The type of count to perform.
146
- - 'words': Count words.
147
- - 'characters': Count characters.
148
  Returns:
149
  int: The number of words in the text.
150
  '''
@@ -172,18 +199,24 @@ def Base64(Text: str, Choice: Literal['encode', 'decode']) -> str:
172
  else:
173
  return 'Error: Invalid choice.'
174
 
175
- def QRCode(Text: str) -> str:
176
- '''Generate a QR code for the given text.
177
  Args:
178
- Text (str): The text to encode in the QR code.
 
179
  Returns:
180
- str: The ASCII representation of the QR code.
181
  '''
182
- qr = qrcode.QRCode(border=0, version=2, error_correction=qrcode.constants.ERROR_CORRECT_L) # type: ignore
183
- IO = StringIO()
184
- qr.print_ascii(out=IO)
185
- IO.seek(0)
186
- return IO.read()
 
 
 
 
 
187
 
188
  # ╭────────────────────╮
189
  # │ Memory Tools │
@@ -263,7 +296,7 @@ def SearchMemories(Query: str, Password: str) -> str:
263
  return 'Error: Invalid password'
264
 
265
  try:
266
- Collection.create_index([("text", "text")])
267
  except Exception:
268
  pass
269
 
@@ -331,6 +364,13 @@ with gradio.Blocks(
331
  MathBtn = gradio.Button('Calculate 🧮', variant='primary')
332
  MathBtn.click(Math, inputs=[Num1Input, Num2Input, OperationInput], outputs=MathOutput)
333
 
 
 
 
 
 
 
 
334
  with gradio.TabItem('Fun & Entertainment 🎭'):
335
  with gradio.Row():
336
  with gradio.Column():
@@ -370,16 +410,17 @@ with gradio.Blocks(
370
  Base64Btn.click(Base64, inputs=[Base64Input, Base64Choice], outputs=Base64Output)
371
 
372
  with gradio.Group():
373
- QRInput = gradio.Textbox(label='Text for QR Code 📱', placeholder='Enter text to generate QR code', lines=3)
374
- QROutput = gradio.Text(label='QR Code Output 📱', interactive=False)
375
- QRBtn = gradio.Button('Generate QR Code 📱', variant='primary')
376
- QRBtn.click(QRCode, inputs=QRInput, outputs=QROutput)
 
377
 
378
  with gradio.TabItem('Memory Tools 💾'):
379
  with gradio.Row():
380
  with gradio.Column():
 
381
  with gradio.Group():
382
- MemoryPasswordInput = gradio.Textbox(label='Password 🔐', placeholder='Enter memory password', type='password', lines=1)
383
  SaveInput = gradio.Textbox(label='Memory Text 💭', placeholder='Enter text to save', lines=3)
384
  SaveBtn = gradio.Button('Save Memory 💾', variant='primary')
385
  SaveBtn.click(SaveMemory, inputs=[SaveInput, MemoryPasswordInput], outputs=gradio.Text(label='Result'))
 
2
  from typing import Literal
3
  from bson import ObjectId
4
  from io import StringIO
5
+ import statistics
6
  import datetime
7
  import requests
8
+ import hashlib
9
  import random
10
  import base64
11
  import gradio
 
12
  import sys
13
  import os
14
 
 
46
  Args:
47
  Location (str): The location for which to get the weather. E.g., "London", "France", "50.85045,4.34878" "~NASA"
48
  Returns:
49
+ str: The current weather.".
50
  '''
51
  return requests.get(f'https://wttr.in/{Location}?A&format=4').text
52
 
 
108
  finally:
109
  sys.stdout = OldStdout
110
 
111
+ def Ping(Host: str, Count: int = 8) -> str:
112
+ '''Ping a host to check its availability.
113
+ Args:
114
+ Host (str): The host to ping (e.g., "google.com", "192.168.1.1").
115
+ Count (int): The number of ping requests to send. Default is 8.
116
+ Returns:
117
+ str: The result of the ping command.
118
+ '''
119
+ Durations = []
120
+ for _ in range(Count):
121
+ try:
122
+ Start = datetime.datetime.now()
123
+ Response = requests.get(f'http://{Host}', timeout=2)
124
+ if Response.status_code != 200:
125
+ continue
126
+ End = datetime.datetime.now()
127
+ Durations.append((End - Start).total_seconds() * 1000)
128
+ except requests.RequestException:
129
+ Durations.append(None)
130
+
131
+ if Durations:
132
+ Durations = [d for d in Durations if d is not None]
133
+ Mean = statistics.mean(Durations)
134
+ Median = statistics.median(Durations)
135
+ return f'Ping to {Host} successful: {Mean} ms (avg), {Median} ms (median)'
136
+ else:
137
+ return f'Ping to {Host} failed: No successful responses'
138
+
139
  # ╭───────────────────────────────────╮
140
  # │ Fun and Entertainment Tools │
141
  # ╰───────────────────────────────────╯
 
172
  Args:
173
  Text (str): The text to count words in.
174
  Choice (Literal['words', 'characters']): The type of count to perform.
 
 
175
  Returns:
176
  int: The number of words in the text.
177
  '''
 
199
  else:
200
  return 'Error: Invalid choice.'
201
 
202
+ def Hash(Text: str, Algorithm: Literal['md5', 'sha1', 'sha256']) -> str:
203
+ '''Hash the input text using the specified algorithm.
204
  Args:
205
+ Text (str): The text to hash.
206
+ Algorithm (Literal['md5', 'sha1', 'sha256']): The hashing algorithm to use.
207
  Returns:
208
+ str: The resulting hash.
209
  '''
210
+ Hashes = {
211
+ 'md5': hashlib.md5,
212
+ 'sha1': hashlib.sha1,
213
+ 'sha256': hashlib.sha256
214
+ }
215
+
216
+ if Algorithm in Hashes:
217
+ return Hashes[Algorithm](Text.encode()).hexdigest()
218
+ else:
219
+ return 'Error: Invalid algorithm.'
220
 
221
  # ╭────────────────────╮
222
  # │ Memory Tools │
 
296
  return 'Error: Invalid password'
297
 
298
  try:
299
+ Collection.create_index([('text', 'text')])
300
  except Exception:
301
  pass
302
 
 
364
  MathBtn = gradio.Button('Calculate 🧮', variant='primary')
365
  MathBtn.click(Math, inputs=[Num1Input, Num2Input, OperationInput], outputs=MathOutput)
366
 
367
+ with gradio.Group():
368
+ PingInput = gradio.Textbox(label='Host to Ping 🌐', placeholder='Enter host (e.g., google.com)', lines=1, max_lines=1)
369
+ PingCount = gradio.Number(label='Ping Count 🔢', value=8, minimum=1, maximum=100)
370
+ PingOutput = gradio.Text(label='Ping Result 📡', interactive=False)
371
+ PingBtn = gradio.Button('Ping Host 📡', variant='primary')
372
+ PingBtn.click(Ping, inputs=[PingInput, PingCount], outputs=PingOutput)
373
+
374
  with gradio.TabItem('Fun & Entertainment 🎭'):
375
  with gradio.Row():
376
  with gradio.Column():
 
410
  Base64Btn.click(Base64, inputs=[Base64Input, Base64Choice], outputs=Base64Output)
411
 
412
  with gradio.Group():
413
+ HashInput = gradio.Textbox(label='Text to Hash 🔐', placeholder='Enter text to hash', lines=3)
414
+ HashChoice = gradio.Radio(label='Hashing Algorithm 🔄', choices=['md5', 'sha1', 'sha256'], value='md5', interactive=True)
415
+ HashOutput = gradio.Text(label='Hash Result 🔐', interactive=False)
416
+ HashBtn = gradio.Button('Generate Hash 🔐', variant='primary')
417
+ HashBtn.click(Hash, inputs=[HashInput, HashChoice], outputs=HashOutput)
418
 
419
  with gradio.TabItem('Memory Tools 💾'):
420
  with gradio.Row():
421
  with gradio.Column():
422
+ MemoryPasswordInput = gradio.Textbox(label='Password 🔐', placeholder='Enter memory password', type='password', lines=1)
423
  with gradio.Group():
 
424
  SaveInput = gradio.Textbox(label='Memory Text 💭', placeholder='Enter text to save', lines=3)
425
  SaveBtn = gradio.Button('Save Memory 💾', variant='primary')
426
  SaveBtn.click(SaveMemory, inputs=[SaveInput, MemoryPasswordInput], outputs=gradio.Text(label='Result'))