HawkClaws commited on
Commit
92d7f1e
1 Parent(s): 39dad64

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +60 -0
app.py ADDED
@@ -0,0 +1,60 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import torch
3
+ from transformers import AutoModelForCausalLM
4
+
5
+ def get_model_structure(model_id):
6
+ model = AutoModelForCausalLM.from_pretrained(
7
+ model_id,
8
+ torch_dtype=torch.bfloat16,
9
+ device_map="cpu",
10
+ )
11
+ structure = {k: v.shape for k, v in model.state_dict().items()}
12
+ return structure
13
+
14
+ def compare_structures(struct1, struct2):
15
+ keys1 = set(struct1.keys())
16
+ keys2 = set(struct2.keys())
17
+ all_keys = keys1.union(keys2)
18
+
19
+ diff = []
20
+ for key in all_keys:
21
+ shape1 = struct1.get(key)
22
+ shape2 = struct2.get(key)
23
+ if shape1 != shape2:
24
+ diff.append((key, shape1, shape2))
25
+ return diff
26
+
27
+ def display_diff(diff):
28
+ left_lines = []
29
+ right_lines = []
30
+
31
+ for key, shape1, shape2 in diff:
32
+ left_lines.append(f"{key}: {shape1}")
33
+ right_lines.append(f"{key}: {shape2}")
34
+
35
+ left_html = "<br>".join(left_lines)
36
+ right_html = "<br>".join(right_lines)
37
+
38
+ return left_html, right_html
39
+
40
+ st.title("Model Structure Comparison Tool")
41
+ model_id1 = st.text_input("Enter the first HuggingFace Model ID")
42
+ model_id2 = st.text_input("Enter the second HuggingFace Model ID")
43
+
44
+ if model_id1 and model_id2:
45
+ struct1 = get_model_structure(model_id1)
46
+ struct2 = get_model_structure(model_id2)
47
+
48
+ diff = compare_structures(struct1, struct2)
49
+ left_html, right_html = display_diff(diff)
50
+
51
+ st.write("### Comparison Result")
52
+ col1, col2 = st.columns(2)
53
+
54
+ with col1:
55
+ st.write("### Model 1")
56
+ st.markdown(left_html, unsafe_allow_html=True)
57
+
58
+ with col2:
59
+ st.write("### Model 2")
60
+ st.markdown(right_html, unsafe_allow_html=True)