Spaces:
Runtime error
Runtime error
:poop: avoid imports to anything not a package ...
Browse files- README.md +1 -1
- app/__main__.py → app.py +71 -2
- app/klimbr.py +0 -66
README.md
CHANGED
@@ -5,7 +5,7 @@ colorFrom: green
|
|
5 |
colorTo: red
|
6 |
sdk: gradio
|
7 |
sdk_version: 4.37.2
|
8 |
-
app_file: app
|
9 |
pinned: false
|
10 |
---
|
11 |
|
|
|
5 |
colorTo: red
|
6 |
sdk: gradio
|
7 |
sdk_version: 4.37.2
|
8 |
+
app_file: app.py
|
9 |
pinned: false
|
10 |
---
|
11 |
|
app/__main__.py → app.py
RENAMED
@@ -5,7 +5,6 @@ import json
|
|
5 |
import html
|
6 |
import random
|
7 |
import datetime
|
8 |
-
from .klimbr import randomize as klimbrize_string
|
9 |
|
10 |
api_key = os.environ.get('FEATHERLESS_API_KEY')
|
11 |
client = OpenAI(
|
@@ -13,11 +12,81 @@ client = OpenAI(
|
|
13 |
api_key=api_key
|
14 |
)
|
15 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
klimbr_cache = {}
|
17 |
def memoized_klimbr(message, percentage, extra):
|
18 |
key = (message, percentage, extra)
|
19 |
if key not in klimbr_cache:
|
20 |
-
klimbr_cache[key] =
|
21 |
|
22 |
return klimbr_cache[key]
|
23 |
|
|
|
5 |
import html
|
6 |
import random
|
7 |
import datetime
|
|
|
8 |
|
9 |
api_key = os.environ.get('FEATHERLESS_API_KEY')
|
10 |
client = OpenAI(
|
|
|
12 |
api_key=api_key
|
13 |
)
|
14 |
|
15 |
+
# from https://github.com/av/klmbr/blob/ca2967123d171fc6d91c329c40e5050a86088446/klmbr/main.py
|
16 |
+
# I sure which I could import this, but can't figure out how to make HF spaces run this as a module
|
17 |
+
# and not a file.
|
18 |
+
import random
|
19 |
+
|
20 |
+
mods = [
|
21 |
+
"capitalize",
|
22 |
+
"diacritic",
|
23 |
+
'leetspeak',
|
24 |
+
"remove_vowel",
|
25 |
+
]
|
26 |
+
|
27 |
+
def klimbr_randomize(text, percentage):
|
28 |
+
if not text:
|
29 |
+
return "", {} # Return empty string and empty mapping if input is empty
|
30 |
+
|
31 |
+
if not 0 <= percentage <= 100:
|
32 |
+
raise ValueError("Percentage must be between 0 and 100")
|
33 |
+
|
34 |
+
words = text.split()
|
35 |
+
chars = list(text)
|
36 |
+
num_chars_to_modify = max(1, int(len(chars) * (percentage / 100)))
|
37 |
+
indices_to_modify = random.sample(range(len(chars)), num_chars_to_modify)
|
38 |
+
word_mapping = {}
|
39 |
+
|
40 |
+
for idx in indices_to_modify:
|
41 |
+
modification = random.choice(mods)
|
42 |
+
|
43 |
+
# Find the word that contains the current character
|
44 |
+
current_length = 0
|
45 |
+
for word_idx, word in enumerate(words):
|
46 |
+
if current_length <= idx < current_length + len(word):
|
47 |
+
original_word = word
|
48 |
+
word_start_idx = current_length
|
49 |
+
break
|
50 |
+
current_length += len(word) + 1 # +1 for the space
|
51 |
+
else:
|
52 |
+
# If we're here, we're likely dealing with a space or the last character
|
53 |
+
continue
|
54 |
+
|
55 |
+
if modification == "capitalize":
|
56 |
+
chars[idx] = chars[idx].swapcase()
|
57 |
+
elif modification == "diacritic":
|
58 |
+
if chars[idx].isalpha():
|
59 |
+
diacritics = ["̀", "́", "̂", "̃", "̈", "̄", "̆", "̇", "̊", "̋"]
|
60 |
+
chars[idx] = chars[idx] + random.choice(diacritics)
|
61 |
+
elif modification == "leetspeak":
|
62 |
+
leetspeak_map = {
|
63 |
+
"a": "4", "e": "3", "i": "1", "o": "0", "s": "5",
|
64 |
+
"t": "7", "b": "8", "g": "9", "l": "1",
|
65 |
+
}
|
66 |
+
chars[idx] = leetspeak_map.get(chars[idx].lower(), chars[idx])
|
67 |
+
elif modification == "remove_vowel":
|
68 |
+
if chars[idx].lower() in "aeiou":
|
69 |
+
chars[idx] = ""
|
70 |
+
|
71 |
+
modified_word = "".join(
|
72 |
+
chars[word_start_idx : word_start_idx + len(original_word)]
|
73 |
+
)
|
74 |
+
|
75 |
+
if modified_word != original_word:
|
76 |
+
# Clean up both the modified word and the original word
|
77 |
+
cleaned_modified_word = modified_word.rstrip('.,')
|
78 |
+
cleaned_original_word = original_word.rstrip('.,')
|
79 |
+
word_mapping[cleaned_modified_word] = cleaned_original_word
|
80 |
+
|
81 |
+
modified_text = "".join(chars)
|
82 |
+
return modified_text, word_mapping
|
83 |
+
## end of klimbr inclusion
|
84 |
+
|
85 |
klimbr_cache = {}
|
86 |
def memoized_klimbr(message, percentage, extra):
|
87 |
key = (message, percentage, extra)
|
88 |
if key not in klimbr_cache:
|
89 |
+
klimbr_cache[key] = klimbr_randomize(message, percentage)[0]
|
90 |
|
91 |
return klimbr_cache[key]
|
92 |
|
app/klimbr.py
DELETED
@@ -1,66 +0,0 @@
|
|
1 |
-
# from https://github.com/av/klmbr/blob/ca2967123d171fc6d91c329c40e5050a86088446/klmbr/main.py
|
2 |
-
import random
|
3 |
-
|
4 |
-
mods = [
|
5 |
-
"capitalize",
|
6 |
-
"diacritic",
|
7 |
-
'leetspeak',
|
8 |
-
"remove_vowel",
|
9 |
-
]
|
10 |
-
|
11 |
-
def randomize(text, percentage):
|
12 |
-
if not text:
|
13 |
-
return "", {} # Return empty string and empty mapping if input is empty
|
14 |
-
|
15 |
-
if not 0 <= percentage <= 100:
|
16 |
-
raise ValueError("Percentage must be between 0 and 100")
|
17 |
-
|
18 |
-
words = text.split()
|
19 |
-
chars = list(text)
|
20 |
-
num_chars_to_modify = max(1, int(len(chars) * (percentage / 100)))
|
21 |
-
indices_to_modify = random.sample(range(len(chars)), num_chars_to_modify)
|
22 |
-
word_mapping = {}
|
23 |
-
|
24 |
-
for idx in indices_to_modify:
|
25 |
-
modification = random.choice(mods)
|
26 |
-
|
27 |
-
# Find the word that contains the current character
|
28 |
-
current_length = 0
|
29 |
-
for word_idx, word in enumerate(words):
|
30 |
-
if current_length <= idx < current_length + len(word):
|
31 |
-
original_word = word
|
32 |
-
word_start_idx = current_length
|
33 |
-
break
|
34 |
-
current_length += len(word) + 1 # +1 for the space
|
35 |
-
else:
|
36 |
-
# If we're here, we're likely dealing with a space or the last character
|
37 |
-
continue
|
38 |
-
|
39 |
-
if modification == "capitalize":
|
40 |
-
chars[idx] = chars[idx].swapcase()
|
41 |
-
elif modification == "diacritic":
|
42 |
-
if chars[idx].isalpha():
|
43 |
-
diacritics = ["̀", "́", "̂", "̃", "̈", "̄", "̆", "̇", "̊", "̋"]
|
44 |
-
chars[idx] = chars[idx] + random.choice(diacritics)
|
45 |
-
elif modification == "leetspeak":
|
46 |
-
leetspeak_map = {
|
47 |
-
"a": "4", "e": "3", "i": "1", "o": "0", "s": "5",
|
48 |
-
"t": "7", "b": "8", "g": "9", "l": "1",
|
49 |
-
}
|
50 |
-
chars[idx] = leetspeak_map.get(chars[idx].lower(), chars[idx])
|
51 |
-
elif modification == "remove_vowel":
|
52 |
-
if chars[idx].lower() in "aeiou":
|
53 |
-
chars[idx] = ""
|
54 |
-
|
55 |
-
modified_word = "".join(
|
56 |
-
chars[word_start_idx : word_start_idx + len(original_word)]
|
57 |
-
)
|
58 |
-
|
59 |
-
if modified_word != original_word:
|
60 |
-
# Clean up both the modified word and the original word
|
61 |
-
cleaned_modified_word = modified_word.rstrip('.,')
|
62 |
-
cleaned_original_word = original_word.rstrip('.,')
|
63 |
-
word_mapping[cleaned_modified_word] = cleaned_original_word
|
64 |
-
|
65 |
-
modified_text = "".join(chars)
|
66 |
-
return modified_text, word_mapping
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|