Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,7 +1,76 @@
|
|
1 |
import gradio as gr
|
2 |
|
3 |
-
def
|
4 |
-
|
|
|
|
|
|
|
5 |
|
6 |
-
|
7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
|
3 |
+
def auto(name):
|
4 |
+
class TrieNode:
|
5 |
+
def __init__(self):
|
6 |
+
self.children = {}
|
7 |
+
self.is_end_of_word = False
|
8 |
|
9 |
+
|
10 |
+
class Autocomplete:
|
11 |
+
def __init__(self):
|
12 |
+
self.root = TrieNode()
|
13 |
+
|
14 |
+
def insert(self, word):
|
15 |
+
node = self.root
|
16 |
+
for char in word:
|
17 |
+
if char not in node.children:
|
18 |
+
node.children[char] = TrieNode()
|
19 |
+
node = node.children[char]
|
20 |
+
node.is_end_of_word = True
|
21 |
+
|
22 |
+
def autocomplete(self, prefix):
|
23 |
+
node = self.root
|
24 |
+
for char in prefix:
|
25 |
+
if char not in node.children:
|
26 |
+
return []
|
27 |
+
node = node.children[char]
|
28 |
+
return self._find_completions(node, prefix)
|
29 |
+
|
30 |
+
def _find_completions(self, node, prefix):
|
31 |
+
completions = []
|
32 |
+
if node.is_end_of_word:
|
33 |
+
completions.append(prefix)
|
34 |
+
for char, child_node in node.children.items():
|
35 |
+
completions.extend(self._find_completions(child_node, prefix + char))
|
36 |
+
return completions
|
37 |
+
|
38 |
+
|
39 |
+
def load_words_and_numbers_from_file(file_path):
|
40 |
+
words_and_numbers = []
|
41 |
+
with open(file_path, "r") as file:
|
42 |
+
for line in file:
|
43 |
+
word_or_number = line.strip().lower()
|
44 |
+
words_and_numbers.append(word_or_number)
|
45 |
+
return words_and_numbers
|
46 |
+
|
47 |
+
|
48 |
+
if __name__ == "__main__":
|
49 |
+
# Specify the path to the text file containing words and numbers
|
50 |
+
file_path = "/drugs.txt"
|
51 |
+
|
52 |
+
# Load words and numbers from the file
|
53 |
+
words_and_numbers = load_words_and_numbers_from_file(file_path)
|
54 |
+
|
55 |
+
# Create an autocomplete system and insert words and numbers
|
56 |
+
autocomplete_system = Autocomplete()
|
57 |
+
for item in words_and_numbers:
|
58 |
+
autocomplete_system.insert(item)
|
59 |
+
|
60 |
+
# Input a prefix for autocomplete suggestions
|
61 |
+
|
62 |
+
|
63 |
+
# Get autocomplete suggestions
|
64 |
+
suggestions = autocomplete_system.autocomplete(name)
|
65 |
+
|
66 |
+
|
67 |
+
|
68 |
+
# Display suggestions
|
69 |
+
if suggestions:
|
70 |
+
return suggestions
|
71 |
+
else:
|
72 |
+
return "No suggestions found."
|
73 |
+
|
74 |
+
demo = gr.Interface(fn=auto, inputs="text", outputs="text")
|
75 |
+
|
76 |
+
demo.launch()
|