Spaces:
Sleeping
Sleeping
File size: 1,651 Bytes
b81fc38 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 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 |
import requests
import gradio as gr
from countries_code import a
def binary_search(list,item):
low=0
high = len(list)-1
while low <= high:
mid = (low + high)//2
guess = list[mid]
if guess == item:
return mid
elif guess > item:
high = mid - 1
else:
low = mid + 1
return None
list1 = [a[j]['code'] for j in range(len(a))]
list2 = [a[i]['name'] for i in range(len(a))]
def name_gender(name,country):
try:
if name!='' and country!='':
country = list1[binary_search(list2,country)]
url = f"https://api.genderize.io/?name={name.title()}&country_id={country}"
response = requests.get(url)
if response.status_code==200:
return response.json()
else:
return f"Serverdan so'rov kelmadi: {response.status_code}"
except Exception as e:
return f"Xatolik: {e}"
demo = gr.Interface(
fn=name_gender,
description=
"""
<h1 align="center">Name to Gender</h1>
O'z ismingizni kiriting va qaysi millat fuqarosi ekanligini taxmin qilib ko'ring<br>
Creator: <a href="https://t.me/shohabbosdev">Shoh Abbos</a>
""",
inputs=[
gr.Textbox(placeholder="Ismingizni kiriting...", label="Ism", autofocus=True),
gr.Dropdown(choices=list2, value="Uzbekistan", allow_custom_value=True)
],
submit_btn = gr.Button("Aniqlash", variant="primary"),
clear_btn = gr.Button("Tozalash", variant="secondary"),
outputs = gr.JSON(label="Natija oynasi")
)
if __name__ == "__main__":
demo.launch(share=False) |