patruff commited on
Commit
727cf89
·
verified ·
1 Parent(s): 5f54195

Upload tool

Browse files
Files changed (3) hide show
  1. app.py +7 -0
  2. requirements.txt +2 -0
  3. tool.py +41 -0
app.py ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ from smolagents import launch_gradio_demo
2
+ from typing import Optional
3
+ from tool import WordPhoneTool
4
+
5
+ tool = WordPhoneTool()
6
+
7
+ launch_gradio_demo(tool)
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ pronouncing
2
+ smolagents
tool.py ADDED
@@ -0,0 +1,41 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from smolagents.tools import Tool
2
+ import pronouncing
3
+ import string
4
+ import json
5
+
6
+ class WordPhoneTool(Tool):
7
+ name = "word_phonetic_analyzer"
8
+ description = "Analyzes the pronunciation of a word using the CMU dictionary to get its phonemes, syllable count and stress pattern"
9
+ inputs = {'word': {'type': 'string', 'description': 'The word to analyze for pronunciation patterns'}}
10
+ output_type = "string"
11
+
12
+ def forward(self, word: str) -> str:
13
+ import pronouncing
14
+ import string
15
+ import json
16
+
17
+ word = word.lower().strip(string.punctuation)
18
+ phones = pronouncing.phones_for_word(word)
19
+
20
+ if not phones:
21
+ result = {
22
+ 'word': word,
23
+ 'found': False,
24
+ 'error': 'Word not found in dictionary'
25
+ }
26
+ else:
27
+ primary_phones = phones[0]
28
+ result = {
29
+ 'word': word,
30
+ 'found': True,
31
+ 'syllable_count': pronouncing.syllable_count(primary_phones),
32
+ 'phones': primary_phones.split(),
33
+ 'stresses': pronouncing.stresses(primary_phones)
34
+ }
35
+
36
+ return json.dumps(result, indent=2)
37
+
38
+
39
+ def __init__(self, *args, **kwargs):
40
+ self.is_initialized = False
41
+