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