patruff commited on
Commit
d53b325
·
verified ·
1 Parent(s): 379f886

Upload tool

Browse files
Files changed (3) hide show
  1. app.py +7 -0
  2. requirements.txt +2 -0
  3. tool.py +72 -0
app.py ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ from smolagents import launch_gradio_demo
2
+ from typing import Optional
3
+ from tool import ParodyWordSuggestionTool
4
+
5
+ tool = ParodyWordSuggestionTool()
6
+
7
+ launch_gradio_demo(tool)
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ smolagents
2
+ pronouncing
tool.py ADDED
@@ -0,0 +1,72 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from smolagents.tools import Tool
2
+ import json
3
+ import pronouncing
4
+
5
+ class ParodyWordSuggestionTool(Tool):
6
+ name = "parody_word_suggester"
7
+ description = """Suggests phonetically similar funny words using CMU dictionary pronunciations.
8
+ Returns similar words from a curated list of funny words, along with their similarity scores."""
9
+ inputs = {'target': {'type': 'string', 'description': 'The word you want to find funny alternatives for'}, 'min_similarity': {'type': 'string', 'description': 'Minimum similarity threshold (0.0-1.0)', 'nullable': True}}
10
+ output_type = "string"
11
+
12
+ def __init__(self):
13
+ super().__init__()
14
+ # Initialize with your funny word list
15
+ self.funny_words = [
16
+ "poop", "loop", "soup", "boop", "dupe", "goop",
17
+ "scoop", "whoop", "droop", "snoop", "coop",
18
+ "fart", "toot", "burp", "squish", "squash",
19
+ "wiggle", "jiggle", "wobble",
20
+ "nincompoop", "dingleberry"
21
+ # Add more words...
22
+ ]
23
+
24
+
25
+ def forward(self, target: str, min_similarity: str = "0.7") -> str:
26
+ """Get phonetically similar funny word suggestions."""
27
+ import pronouncing
28
+ import json
29
+
30
+ target = target.lower().strip()
31
+ min_similarity = float(min_similarity)
32
+ suggestions = []
33
+
34
+ # Get target pronunciation
35
+ target_phones = pronouncing.phones_for_word(target)
36
+ if not target_phones:
37
+ return json.dumps({
38
+ "error": f"'{target}' not found in CMU dictionary",
39
+ "suggestions": []
40
+ }, indent=2)
41
+
42
+ target_phones = target_phones[0].split()
43
+
44
+ # Check each funny word
45
+ for word in self.funny_words:
46
+ phones = pronouncing.phones_for_word(word)
47
+ if phones: # Only process if word is in dictionary
48
+ phones = phones[0].split()
49
+
50
+ # Calculate similarity using overlap
51
+ overlap = len(set(phones) & set(target_phones))
52
+ total = min(len(phones), len(target_phones))
53
+ similarity = overlap / total if total > 0 else 0.0
54
+
55
+ if similarity >= min_similarity:
56
+ suggestions.append({
57
+ "word": word,
58
+ "similarity": round(similarity, 3),
59
+ "syllables": pronouncing.syllable_count(phones)
60
+ })
61
+
62
+ # Sort by similarity score descending
63
+ suggestions.sort(key=lambda x: x["similarity"], reverse=True)
64
+
65
+ result = {
66
+ "target": target,
67
+ "target_syllables": pronouncing.syllable_count(target_phones),
68
+ "suggestions": suggestions
69
+ }
70
+
71
+ return json.dumps(result, indent=2)
72
+