Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -11,7 +11,6 @@ ssl_context = ssl.create_default_context()
|
|
| 11 |
ssl_context.check_hostname = False
|
| 12 |
ssl_context.verify_mode = ssl.CERT_NONE
|
| 13 |
|
| 14 |
-
# Area data (unchanged)
|
| 15 |
area_data = {
|
| 16 |
'香港': {
|
| 17 |
'中西區': ["西環", "堅尼地城", "石塘咀", "西營盤", "上環", "中環", "金鐘", "西半山", "中半山", "半山", "山頂"],
|
|
@@ -39,7 +38,6 @@ area_data = {
|
|
| 39 |
}
|
| 40 |
}
|
| 41 |
|
| 42 |
-
|
| 43 |
@lru_cache(maxsize=None)
|
| 44 |
def load_user_dict_terms():
|
| 45 |
user_dict_terms = set()
|
|
@@ -58,7 +56,6 @@ def load_user_dict_terms():
|
|
| 58 |
print(f'Error reading file {file_path}: {e}')
|
| 59 |
return user_dict_terms
|
| 60 |
|
| 61 |
-
|
| 62 |
def setup_jieba_dictionaries():
|
| 63 |
dictionaries = [
|
| 64 |
'flag/RVT_AddressCh.txt', 'flag/RVT_AddressEn.txt', 'flag/RVT_Area.txt',
|
|
@@ -68,7 +65,6 @@ def setup_jieba_dictionaries():
|
|
| 68 |
for file_path in dictionaries:
|
| 69 |
jieba.load_userdict(file_path)
|
| 70 |
|
| 71 |
-
|
| 72 |
def process_text(text):
|
| 73 |
setup_jieba_dictionaries()
|
| 74 |
user_dict_terms = load_user_dict_terms()
|
|
@@ -84,18 +80,15 @@ def process_text(text):
|
|
| 84 |
|
| 85 |
return results
|
| 86 |
|
| 87 |
-
|
| 88 |
def reformat_text(text):
|
| 89 |
return [line.strip() for line in text.splitlines() if line.strip()]
|
| 90 |
|
| 91 |
-
|
| 92 |
def process_text_only(text, reformat):
|
| 93 |
extracted_keywords = process_text(text)
|
| 94 |
if reformat:
|
| 95 |
extracted_keywords = reformat_text('\n'.join(extracted_keywords))
|
| 96 |
return '\n'.join(extracted_keywords)
|
| 97 |
|
| 98 |
-
|
| 99 |
async def lookup_address(query, language='zh-Hant'):
|
| 100 |
url = 'https://www.als.gov.hk/lookup'
|
| 101 |
headers = {'Accept': 'application/json', 'Accept-Language': language}
|
|
@@ -116,7 +109,6 @@ async def lookup_address(query, language='zh-Hant'):
|
|
| 116 |
print(f'General Error: {e}')
|
| 117 |
return {'error': f'General Error: {e}'}
|
| 118 |
|
| 119 |
-
|
| 120 |
async def get_address_lookup_results(keywords):
|
| 121 |
results = []
|
| 122 |
tasks = [lookup_address(keyword.strip()) for keyword in keywords if
|
|
@@ -155,13 +147,11 @@ async def get_address_lookup_results(keywords):
|
|
| 155 |
|
| 156 |
return results
|
| 157 |
|
| 158 |
-
|
| 159 |
def is_valid_for_lookup(keyword):
|
| 160 |
return not any(keyword in districts.keys() or keyword in subdistrict
|
| 161 |
for districts in area_data.values()
|
| 162 |
for subdistrict in districts.values())
|
| 163 |
|
| 164 |
-
|
| 165 |
async def gradio_function(text, reformat, perform_lookup):
|
| 166 |
extracted_keywords = process_text_only(text, reformat)
|
| 167 |
keywords_list = extracted_keywords.splitlines()
|
|
@@ -172,11 +162,9 @@ async def gradio_function(text, reformat, perform_lookup):
|
|
| 172 |
|
| 173 |
return extracted_keywords, address_results
|
| 174 |
|
| 175 |
-
|
| 176 |
def gradio_interface(text, reformat, perform_lookup):
|
| 177 |
return asyncio.run(gradio_function(text, reformat, perform_lookup))
|
| 178 |
|
| 179 |
-
|
| 180 |
interface = gr.Interface(
|
| 181 |
fn=gradio_interface,
|
| 182 |
inputs=[
|
|
|
|
| 11 |
ssl_context.check_hostname = False
|
| 12 |
ssl_context.verify_mode = ssl.CERT_NONE
|
| 13 |
|
|
|
|
| 14 |
area_data = {
|
| 15 |
'香港': {
|
| 16 |
'中西區': ["西環", "堅尼地城", "石塘咀", "西營盤", "上環", "中環", "金鐘", "西半山", "中半山", "半山", "山頂"],
|
|
|
|
| 38 |
}
|
| 39 |
}
|
| 40 |
|
|
|
|
| 41 |
@lru_cache(maxsize=None)
|
| 42 |
def load_user_dict_terms():
|
| 43 |
user_dict_terms = set()
|
|
|
|
| 56 |
print(f'Error reading file {file_path}: {e}')
|
| 57 |
return user_dict_terms
|
| 58 |
|
|
|
|
| 59 |
def setup_jieba_dictionaries():
|
| 60 |
dictionaries = [
|
| 61 |
'flag/RVT_AddressCh.txt', 'flag/RVT_AddressEn.txt', 'flag/RVT_Area.txt',
|
|
|
|
| 65 |
for file_path in dictionaries:
|
| 66 |
jieba.load_userdict(file_path)
|
| 67 |
|
|
|
|
| 68 |
def process_text(text):
|
| 69 |
setup_jieba_dictionaries()
|
| 70 |
user_dict_terms = load_user_dict_terms()
|
|
|
|
| 80 |
|
| 81 |
return results
|
| 82 |
|
|
|
|
| 83 |
def reformat_text(text):
|
| 84 |
return [line.strip() for line in text.splitlines() if line.strip()]
|
| 85 |
|
|
|
|
| 86 |
def process_text_only(text, reformat):
|
| 87 |
extracted_keywords = process_text(text)
|
| 88 |
if reformat:
|
| 89 |
extracted_keywords = reformat_text('\n'.join(extracted_keywords))
|
| 90 |
return '\n'.join(extracted_keywords)
|
| 91 |
|
|
|
|
| 92 |
async def lookup_address(query, language='zh-Hant'):
|
| 93 |
url = 'https://www.als.gov.hk/lookup'
|
| 94 |
headers = {'Accept': 'application/json', 'Accept-Language': language}
|
|
|
|
| 109 |
print(f'General Error: {e}')
|
| 110 |
return {'error': f'General Error: {e}'}
|
| 111 |
|
|
|
|
| 112 |
async def get_address_lookup_results(keywords):
|
| 113 |
results = []
|
| 114 |
tasks = [lookup_address(keyword.strip()) for keyword in keywords if
|
|
|
|
| 147 |
|
| 148 |
return results
|
| 149 |
|
|
|
|
| 150 |
def is_valid_for_lookup(keyword):
|
| 151 |
return not any(keyword in districts.keys() or keyword in subdistrict
|
| 152 |
for districts in area_data.values()
|
| 153 |
for subdistrict in districts.values())
|
| 154 |
|
|
|
|
| 155 |
async def gradio_function(text, reformat, perform_lookup):
|
| 156 |
extracted_keywords = process_text_only(text, reformat)
|
| 157 |
keywords_list = extracted_keywords.splitlines()
|
|
|
|
| 162 |
|
| 163 |
return extracted_keywords, address_results
|
| 164 |
|
|
|
|
| 165 |
def gradio_interface(text, reformat, perform_lookup):
|
| 166 |
return asyncio.run(gradio_function(text, reformat, perform_lookup))
|
| 167 |
|
|
|
|
| 168 |
interface = gr.Interface(
|
| 169 |
fn=gradio_interface,
|
| 170 |
inputs=[
|