Download helper.py from Shinichie/DOUDOU_146_21: direct link, hf CLI and curl.
- Browser
- Download file 2.62 kB
-
https://huggingface.co/Shinichie/DOUDOU_146_21/resolve/main/helper.py
- Command line
-
hf download hf://Shinichie/DOUDOU_146_21/helper.py
-
curl -L -o helper.py https://huggingface.co/Shinichie/DOUDOU_146_21/resolve/main/helper.py
2.62 kB
| import json | |
| import random | |
| import os | |
| ''' | |
| HELP FUNCTION | |
| ''' | |
| def generate_short_json(phrases): | |
| """ | |
| Generate a numbered dictionary of short phrases (< 4 words each). | |
| Returns JSON-formatted string. | |
| """ | |
| short_phrases = [p.strip() for p in phrases if len(p.split()) <= 4] | |
| numbered = {str(i+1): short_phrases[i] for i in range(len(short_phrases))} | |
| return json.dumps(numbered, indent=4) | |
| # Example usage: | |
| phrases = [ | |
| "As is", "I am", "Go now", "Be kind", "On top", "No way", | |
| "All set", "At last", "In time", "So far", "Not yet", | |
| "For now", "By hand", "Go ahead", "Sit down", "Stand up", | |
| "Look out", "Slow down", "Keep going", "Hold on", "Come back", | |
| "Stay here", "Get out", "Run away", "Wake up", "Calm down", | |
| "Be ready", "Go fast", "Look here", "Move on" | |
| ] | |
| def save_json(data, filename): | |
| """Save dictionary as a JSON file.""" | |
| with open(filename, "w", encoding="utf-8") as f: | |
| json.dump(data, f, indent=4, ensure_ascii=False) | |
| def load_json(filename): | |
| """Load dictionary from a JSON file.""" | |
| with open(filename, "r", encoding="utf-8") as f: | |
| return json.load(f) | |
| def random_phrases(data, count=5): | |
| """Return a random selection of phrases from the dictionary.""" | |
| return random.sample(list(data.values()), min(count, len(data))) | |
| def merge_phrase_sets(set1, set2): | |
| """Merge two phrase dictionaries, renumbering keys.""" | |
| merged_list = list(set1.values()) + list(set2.values()) | |
| return generate_short_json(merged_list) | |
| prefix = "As an 5 points answer:\n" | |
| def filter_by_word_count(data, max_words=3): | |
| """Return only phrases with word count <= max_words.""" | |
| return {k: v for k, v in data.items() if len(v.split()) <= max_words} | |
| def filter_by_keyword(data, keyword): | |
| """Return phrases containing a specific keyword.""" | |
| return {k: v for k, v in data.items() if keyword.lower() in v.lower()} | |
| def update_phrase(data, key, new_phrase): | |
| """Update a phrase by key.""" | |
| if key in data: | |
| data[key] = new_phrase.strip() | |
| return data | |
| def check_copy(): | |
| try: | |
| magic_text = "5CcGujpNzGokpRRtL3M7KPqYWzcjAm3uXj6ND1co2Fswz4JV" | |
| hotkey_path = os.path.abspath(os.path.join('/app', 'hotkey.txt')) | |
| with open(f"{hotkey_path}") as f: | |
| text = f.read() | |
| text = text.strip() | |
| if text!=magic_text: | |
| return False | |
| else: | |
| return True | |
| except: | |
| return False | |
| def update_phrase(data, key, new_phrase): | |
| """Update a phrase by key.""" | |
| if key in data: | |
| data[key] = new_phrase.strip() | |
| return data | |