binary-mod / app.py
Omnibus's picture
Update app.py
44dcca2 verified
import gradio as gr
import numpy
import torch
import requests
import io
from huggingface_hub import HfApi, ModelFilter, list_models, list_repo_files, hf_hub_download
api = HfApi()
models=[
""
]
def list_files(model_name):
files = api.list_repo_files(repo_id=model_name,repo_type="model")
return gr.update(choices=[m for m in files],interactive=True)
def load_bin(model_name,file_name):
r = requests.get(f'https://huggingface.co/{model_name}/resolve/main/{file_name}')
#print(r.content)
print("#######")
print(dir(r))
#torch.set_printoptions(profile="full")
#result=torch.frombuffer(r.content,dtype=torch.get_default_dtype(),offset=1)
result=torch.frombuffer(r.content,dtype=torch.get_default_dtype(),offset=27)
print(dir(result))
return result
def load_models(model_in):
loaded_model=[]
model_details=[]
if "/" in models:
similar_models = api.list_models(search=model_in.split("/")[1],limit=100,cardData=True)
else:
similar_models = api.list_models(search=model_in,limit=100,cardData=True)
for model in similar_models:
try:
model_load=gr.load(f'models/{model.id}')
print(model_load)
#out_test=model_load("hello?")
loaded_model.append(model_load)
except Exception as e:
loaded_model.append({"MODEL":model.id,"ERROR":e})
try:
model_details.append(model)
except Exception as ee:
model_details.append({"MODEL":model.id,"ERROR":ee})
return loaded_model, model_details
with gr.Blocks() as app:
with gr.Row():
model_name=gr.Textbox(label="Model", value=models[0], placeholder=models[0])
load_btn=gr.Button("Load")
with gr.Row():
file_name=gr.Dropdown(label="Files", choices=[])
bin_btn=gr.Button("Binary")
binary=gr.JSON()
with gr.Row():
models_out=gr.JSON(label="Gradio Details")
details=gr.JSON(label="Hub Details")
app.load(list_files,model_name,[file_name])
bin_btn.click(load_bin,[model_name,file_name],binary)
#app.load(load_models,model_name,[models_out,details])
load_btn.click(load_models,model_name,[models_out,details])
app.launch()