donb-hf commited on
Commit
4b29566
1 Parent(s): 89ace7e

improve examples and error handling

Browse files
Files changed (3) hide show
  1. .gitignore +2 -0
  2. app.py +43 -12
  3. requirements.txt +1 -1
.gitignore ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ .venv/
2
+ .python-version
app.py CHANGED
@@ -16,38 +16,69 @@ subprocess.run(
16
  hf_token = os.getenv("HF_TOKEN")
17
  login(token=hf_token, add_to_git_credential=True)
18
 
 
 
 
19
  # Function to get the model summary
20
  @spaces.GPU
21
  def get_model_summary(model_name):
22
- device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
23
- model = AutoModelForCausalLM.from_pretrained(model_name, trust_remote_code=True).to(device)
24
- return str(model)
 
 
 
 
 
 
 
 
 
 
25
 
26
  # Create the Gradio Blocks interface
27
  with gr.Blocks() as demo:
28
  with gr.Row():
29
  with gr.Column():
30
- textbox = gr.Textbox(label="Model Name")
31
- examples = gr.Examples(
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32
  examples=[
33
  ["google/gemma-7b"],
34
  ["microsoft/Phi-3-mini-4k-instruct"],
35
  ["meta-llama/Meta-Llama-3-8B"],
36
  ["mistralai/Mistral-7B-Instruct-v0.3"],
37
- ["vikhyatk/moondream2"],
38
- ["microsoft/Phi-3-vision-128k-instruct"],
39
- ["openbmb/MiniCPM-Llama3-V-2_5"],
40
- ["google/paligemma-3b-mix-224"],
41
- ["HuggingFaceM4/idefics2-8b-chatty"],
42
  ["mistralai/Codestral-22B-v0.1"]
43
  ],
44
  inputs=textbox
45
  )
46
  submit_button = gr.Button("Submit")
47
  with gr.Column():
48
- output = gr.Textbox(label="Output", lines=20)
 
 
 
 
 
49
 
50
- submit_button.click(fn=get_model_summary, inputs=textbox, outputs=output)
51
 
52
  # Launch the interface
53
  demo.launch()
 
16
  hf_token = os.getenv("HF_TOKEN")
17
  login(token=hf_token, add_to_git_credential=True)
18
 
19
+ # Cache for storing loaded models and their summaries
20
+ model_cache = {}
21
+
22
  # Function to get the model summary
23
  @spaces.GPU
24
  def get_model_summary(model_name):
25
+ if model_name in model_cache:
26
+ return model_cache[model_name], ""
27
+
28
+ try:
29
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
30
+
31
+ model = AutoModelForCausalLM.from_pretrained(model_name, trust_remote_code=True).to(device)
32
+
33
+ model_summary = str(model)
34
+ model_cache[model_name] = model_summary
35
+ return model_summary, ""
36
+ except Exception as e:
37
+ return "", str(e)
38
 
39
  # Create the Gradio Blocks interface
40
  with gr.Blocks() as demo:
41
  with gr.Row():
42
  with gr.Column():
43
+ textbox = gr.Textbox(label="Model Name", placeholder="Enter the model name here OR select example below...", lines=1)
44
+ gr.Markdown("### Vision Models")
45
+ vision_examples = gr.Examples(
46
+ examples=[
47
+ ["liuhaotian/llava-v1.6-mistral-7b"],
48
+ ["microsoft/llava-med-v1.5-mistral-7b"],
49
+ ["llava-hf/llava-v1.6-mistral-7b-hf"],
50
+ ["xtuner/llava-phi-3-mini-hf"],
51
+ ["xtuner/llava-llama-3-8b-v1_1-transformers"],
52
+ ["vikhyatk/moondream2"],
53
+ ["openbmb/MiniCPM-Llama3-V-2_5"],
54
+ ["microsoft/Phi-3-vision-128k-instruct"],
55
+ ["google/paligemma-3b-mix-224"],
56
+ ["HuggingFaceM4/idefics2-8b-chatty"]
57
+ ],
58
+ inputs=textbox
59
+ )
60
+
61
+ gr.Markdown("### Other Models")
62
+ other_examples = gr.Examples(
63
  examples=[
64
  ["google/gemma-7b"],
65
  ["microsoft/Phi-3-mini-4k-instruct"],
66
  ["meta-llama/Meta-Llama-3-8B"],
67
  ["mistralai/Mistral-7B-Instruct-v0.3"],
 
 
 
 
 
68
  ["mistralai/Codestral-22B-v0.1"]
69
  ],
70
  inputs=textbox
71
  )
72
  submit_button = gr.Button("Submit")
73
  with gr.Column():
74
+ output = gr.Textbox(label="Model Architecture", lines=20, placeholder="Model architecture will appear here...", show_copy_button=True)
75
+ error_output = gr.Textbox(label="Error", lines=10, placeholder="Exceptions will appear here...", show_copy_button=True)
76
+
77
+ def handle_click(model_name):
78
+ model_summary, error_message = get_model_summary(model_name)
79
+ return model_summary, error_message
80
 
81
+ submit_button.click(fn=handle_click, inputs=textbox, outputs=[output, error_output])
82
 
83
  # Launch the interface
84
  demo.launch()
requirements.txt CHANGED
@@ -1,4 +1,4 @@
1
- transformers
2
  spaces
3
  torchvision
4
  einops
 
1
+ git+https://github.com/huggingface/transformers.git
2
  spaces
3
  torchvision
4
  einops