Spaces:
Runtime error
Runtime error
Commit
·
7b6b0f6
1
Parent(s):
69d2db7
⚙️ Add text processing tools: WordCount, Base64 encoding/decoding, and QR code generation
Browse files- app.py +71 -5
- requirements.txt +1 -0
app.py
CHANGED
|
@@ -1,19 +1,21 @@
|
|
| 1 |
from typing import Literal
|
| 2 |
-
from gradio import themes
|
| 3 |
from io import StringIO
|
| 4 |
import datetime
|
| 5 |
import requests
|
| 6 |
import random
|
|
|
|
| 7 |
import gradio
|
|
|
|
| 8 |
import json
|
| 9 |
import sys
|
| 10 |
import os
|
| 11 |
|
| 12 |
MemoryFile = 'Memories.json'
|
| 13 |
-
Theme = themes.Citrus(
|
| 14 |
primary_hue='blue',
|
| 15 |
secondary_hue='blue',
|
| 16 |
-
radius_size=themes.sizes.radius_xxl
|
|
|
|
| 17 |
).set(
|
| 18 |
link_text_color='blue'
|
| 19 |
)
|
|
@@ -121,6 +123,53 @@ def Reverse(Text: str) -> str:
|
|
| 121 |
'''
|
| 122 |
return Text[::-1]
|
| 123 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 124 |
# ╭────────────────────╮
|
| 125 |
# │ Memory Tools │
|
| 126 |
# ╰────────────────────╯
|
|
@@ -253,10 +302,27 @@ with gradio.Blocks(
|
|
| 253 |
ReverseOutput = gradio.Text(label='Reversed Text ↩️', interactive=False)
|
| 254 |
ReverseBtn = gradio.Button('Reverse Text 🔄', variant='primary')
|
| 255 |
ReverseBtn.click(Reverse, inputs=ReverseInput, outputs=ReverseOutput)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 256 |
|
| 257 |
with gradio.Column():
|
| 258 |
-
|
| 259 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 260 |
|
| 261 |
with gradio.TabItem('Memory Tools 💾'):
|
| 262 |
with gradio.Row():
|
|
|
|
| 1 |
from typing import Literal
|
|
|
|
| 2 |
from io import StringIO
|
| 3 |
import datetime
|
| 4 |
import requests
|
| 5 |
import random
|
| 6 |
+
import base64
|
| 7 |
import gradio
|
| 8 |
+
import qrcode
|
| 9 |
import json
|
| 10 |
import sys
|
| 11 |
import os
|
| 12 |
|
| 13 |
MemoryFile = 'Memories.json'
|
| 14 |
+
Theme = gradio.themes.Citrus( # type: ignore
|
| 15 |
primary_hue='blue',
|
| 16 |
secondary_hue='blue',
|
| 17 |
+
radius_size=gradio.themes.sizes.radius_xxl, # type: ignore
|
| 18 |
+
font=[gradio.themes.GoogleFont('Nunito'), 'Arial', 'sans-serif'] # type: ignore
|
| 19 |
).set(
|
| 20 |
link_text_color='blue'
|
| 21 |
)
|
|
|
|
| 123 |
'''
|
| 124 |
return Text[::-1]
|
| 125 |
|
| 126 |
+
def WordCount(Text: str, Choice: Literal['words', 'characters']) -> int:
|
| 127 |
+
'''Count the number of words or characters in the input text.
|
| 128 |
+
Args:
|
| 129 |
+
Text (str): The text to count words in.
|
| 130 |
+
Choice (Literal['words', 'characters']): The type of count to perform.
|
| 131 |
+
- 'words': Count words.
|
| 132 |
+
- 'characters': Count characters.
|
| 133 |
+
Returns:
|
| 134 |
+
int: The number of words in the text.
|
| 135 |
+
'''
|
| 136 |
+
if Choice == 'words':
|
| 137 |
+
return len(Text.split())
|
| 138 |
+
elif Choice == 'characters':
|
| 139 |
+
return len(Text)
|
| 140 |
+
else:
|
| 141 |
+
return 0
|
| 142 |
+
|
| 143 |
+
def Base64(Text: str, Choice: Literal['encode', 'decode']) -> str:
|
| 144 |
+
'''Encode or decode text using Base64.
|
| 145 |
+
Args:
|
| 146 |
+
Text (str): The text to encode or decode.
|
| 147 |
+
Choice (Literal['encode', 'decode']): The operation to perform.
|
| 148 |
+
- 'encode': Encode the text to Base64.
|
| 149 |
+
- 'decode': Decode the Base64 text back to original text.
|
| 150 |
+
Returns:
|
| 151 |
+
str: The encoded or decoded text.
|
| 152 |
+
'''
|
| 153 |
+
if Choice == 'encode':
|
| 154 |
+
return base64.b64encode(Text.encode()).decode()
|
| 155 |
+
elif Choice == 'decode':
|
| 156 |
+
return base64.b64decode(Text.encode()).decode()
|
| 157 |
+
else:
|
| 158 |
+
return 'Error: Invalid choice.'
|
| 159 |
+
|
| 160 |
+
def QRCode(Text: str) -> str:
|
| 161 |
+
'''Generate a QR code for the given text.
|
| 162 |
+
Args:
|
| 163 |
+
Text (str): The text to encode in the QR code.
|
| 164 |
+
Returns:
|
| 165 |
+
str: The ASCII representation of the QR code.
|
| 166 |
+
'''
|
| 167 |
+
qr = qrcode.QRCode(border=0, version=2, error_correction=qrcode.constants.ERROR_CORRECT_L) # type: ignore
|
| 168 |
+
IO = StringIO()
|
| 169 |
+
qr.print_ascii(out=IO)
|
| 170 |
+
IO.seek(0)
|
| 171 |
+
return IO.read()
|
| 172 |
+
|
| 173 |
# ╭────────────────────╮
|
| 174 |
# │ Memory Tools │
|
| 175 |
# ╰────────────────────╯
|
|
|
|
| 302 |
ReverseOutput = gradio.Text(label='Reversed Text ↩️', interactive=False)
|
| 303 |
ReverseBtn = gradio.Button('Reverse Text 🔄', variant='primary')
|
| 304 |
ReverseBtn.click(Reverse, inputs=ReverseInput, outputs=ReverseOutput)
|
| 305 |
+
|
| 306 |
+
with gradio.Group():
|
| 307 |
+
WordCountInput = gradio.Textbox(label='Text for Word Count 📏', placeholder='Enter text to count words', lines=3)
|
| 308 |
+
WordCountChoice = gradio.Radio(label='Count Type 🔢', choices=['words', 'characters'], value='words', interactive=True)
|
| 309 |
+
WordCountOutput = gradio.Text(label='Word Count Result 📊', interactive=False)
|
| 310 |
+
WordCountBtn = gradio.Button('Count Words 📈', variant='primary')
|
| 311 |
+
WordCountBtn.click(WordCount, inputs=[WordCountInput, WordCountChoice], outputs=WordCountOutput)
|
| 312 |
|
| 313 |
with gradio.Column():
|
| 314 |
+
with gradio.Group():
|
| 315 |
+
Base64Input = gradio.Textbox(label='Text for Base64 📦', placeholder='Enter text to encode/decode', lines=3)
|
| 316 |
+
Base64Choice = gradio.Radio(label='Operation 🔄', choices=['encode', 'decode'], value='encode', interactive=True)
|
| 317 |
+
Base64Output = gradio.Text(label='Base64 Result 📦', interactive=False)
|
| 318 |
+
Base64Btn = gradio.Button('Process Base64 📦', variant='primary')
|
| 319 |
+
Base64Btn.click(Base64, inputs=[Base64Input, Base64Choice], outputs=Base64Output)
|
| 320 |
+
|
| 321 |
+
with gradio.Group():
|
| 322 |
+
QRInput = gradio.Textbox(label='Text for QR Code 📱', placeholder='Enter text to generate QR code', lines=3)
|
| 323 |
+
QROutput = gradio.Text(label='QR Code Output 📱', interactive=False)
|
| 324 |
+
QRBtn = gradio.Button('Generate QR Code 📱', variant='primary')
|
| 325 |
+
QRBtn.click(QRCode, inputs=QRInput, outputs=QROutput)
|
| 326 |
|
| 327 |
with gradio.TabItem('Memory Tools 💾'):
|
| 328 |
with gradio.Row():
|
requirements.txt
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
qrcode
|