unclemusclez commited on
Commit
6b057bb
·
verified ·
1 Parent(s): 3d7c439

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +72 -60
app.py CHANGED
@@ -18,20 +18,15 @@ from textwrap import dedent
18
 
19
  HF_TOKEN = os.environ.get("HF_TOKEN")
20
  OLLAMA_USERNAME = os.environ.get("OLLAMA_USERNAME").lower()
21
- ollama_pubkey = open("/home/ollamafy/.ollama/id_ed25519.pub", "r")
22
- # ollama_pubkey_read = print(ollama_pubkey.read())
23
 
24
 
25
-
26
-
27
- def process_model(model_id, q_method, latest, maintainer, oauth_token: gr.OAuthToken | None):
28
- #def process_model(model_id, q_method, latest):
29
  if oauth_token.token is None:
30
  raise ValueError("You must be logged in to use GGUF-my-repo")
31
  model_name = model_id.split('/')[-1]
32
- model_maintainer = model_id.split('/')[-2]
33
- ollama_model_name = model_maintainer.lower() + '_' + model_name.lower()
34
-
35
 
36
  try:
37
  api = HfApi(token=oauth_token.token)
@@ -57,51 +52,65 @@ def process_model(model_id, q_method, latest, maintainer, oauth_token: gr.OAuthT
57
  print(f"Current working directory: {os.getcwd()}")
58
  print(f"Model directory contents: {os.listdir(model_name)}")
59
 
60
- model_file = model_name + '_modelfile'
61
- # model_path = f"{HOME}/.cache/huggingface/hub/{model_id}"
62
-
63
- model_modelfile = open(model_file, "w")
64
- model_modelfile_path = f"FROM {HOME}/.cache/huggingface/hub/{model_id}"
65
- model_modelfile.write(model_modelfile_path)
66
- model_modelfile.close()
67
- print(model_modelfile_path)
68
-
69
- if q_method == "FP16":
70
- ollama_conversion = f"ollama create -f {model_file} {OLLAMA_USERNAME}/{ollama_model_name}:{q_method.lower()}"
71
- else:
72
- ollama_conversion = f"ollama create -q {q_method} -f {model_file} {OLLAMA_USERNAME}/{ollama_model_name}:{q_method.lower()}"
73
-
74
- ollama_conversion_result = subprocess.run(ollama_conversion, shell=True, capture_output=True)
75
- print(ollama_conversion_result)
76
- if ollama_conversion_result.returncode != 0:
77
- raise Exception(f"Error converting to Ollama: {ollama_conversion_result.stderr}")
78
- print("Model converted to Ollama successfully!")
79
- if maintainer == True:
80
- ollama_push = f"ollama push {OLLAMA_USERNAME}/{model_name}:{q_method.lower()}"
81
- else:
82
- ollama_push = f"ollama push {OLLAMA_USERNAME}/{ollama_model_name}:{q_method.lower()}"
83
- ollama_push_result = subprocess.run(ollama_push, shell=True, capture_output=True)
84
- print(ollama_push_result)
85
- if ollama_push_result.returncode != 0:
86
- raise Exception(f"Error converting to Ollama: {ollama_push_result.stderr}")
87
- print("Model pushed to Ollama library successfully!")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
88
 
89
- if latest == True:
90
- ollama_copy = f"ollama cp {OLLAMA_USERNAME}/{model_id.lower()}:{q_method.lower()} {OLLAMA_USERNAME}/{model_id.lower()}:latest"
91
- ollama_copy_result = subprocess.run(ollama_copy, shell=True, capture_output=True)
92
- print(ollama_copy_result)
93
- if ollama_copy_result.returncode != 0:
94
- raise Exception(f"Error converting to Ollama: {ollama_push_result.stderr}")
95
- print("Model pushed to Ollama library successfully!")
96
- if maintainer == True:
97
- llama_push_latest = f"ollama push {OLLAMA_USERNAME}/{model_name}:latest"
98
- else:
99
- ollama_push_latest = f"ollama push {OLLAMA_USERNAME}/{ollama_model_name}:latest"
100
- ollama_push_latest_result = subprocess.run(ollama_push_latest, shell=True, capture_output=True)
101
- print(ollama_push_latest_result)
102
- if ollama_push_latest_result.returncode != 0:
103
- raise Exception(f"Error converting to Ollama: {ollama_push_result.stderr}")
104
- print("Model pushed to Ollama library successfully!")
105
 
106
 
107
  except Exception as e:
@@ -116,10 +125,10 @@ css="""/* Custom CSS to allow scrolling */
116
  """
117
  # Create Gradio interface
118
  with gr.Blocks(css=css) as demo:
 
119
  gr.Markdown("You must be logged in to use Ollamafy.")
120
  gr.Markdown(ollama_pubkey.read().rstrip())
121
  ollama_pubkey.close()
122
- gr.LoginButton(min_width=250)
123
 
124
  model_id = HuggingfaceHubSearch(
125
  label="Hub Model ID",
@@ -127,29 +136,32 @@ with gr.Blocks(css=css) as demo:
127
  search_type="model",
128
  )
129
 
130
- q_method = gr.Dropdown(
131
- ["FP16", "Q3_K_S", "Q3_K_M", "Q3_K_L", "Q4_0", "Q4_1", "Q4_K_S", "Q4_K_M", "Q5_0", "Q5_1", "Q5_K_S", "Q5_K_M", "Q6_K", "Q8_0"],
132
- label="Quantization Method",
133
- info="Ollama Quantization Types",
 
134
  value="FP16",
135
  filterable=False,
136
- visible=True
137
  )
138
  latest = gr.Checkbox(
139
  value=False,
140
  label="Latest",
141
  info="Copy Model to Ollama Library with the :latest tag"
142
  )
 
143
  maintainer = gr.Checkbox(
144
  value=False,
145
  label="Maintainer",
146
  info="This is your original repository on both Hugging Face and Ollama. (DO NOT USE!!!)"
147
  )
 
148
  iface = gr.Interface(
149
  fn=process_model,
150
  inputs=[
151
  model_id,
152
- q_method,
153
  latest,
154
  maintainer
155
  ],
 
18
 
19
  HF_TOKEN = os.environ.get("HF_TOKEN")
20
  OLLAMA_USERNAME = os.environ.get("OLLAMA_USERNAME").lower()
21
+ ollama_pubkey = open("/home/user/.ollama/id_ed25519.pub", "r")
22
+ ollama_q_methods = ["FP16","Q3_K_S", "Q3_K_M", "Q3_K_L", "Q4_0", "Q4_1", "Q4_K_S", "Q4_K_M", "Q5_0", "Q5_1", "Q5_K_S", "Q5_K_M", "Q6_K", "Q8_0"]
23
 
24
 
25
+ def process_model(model_id, q_method, use_imatrix, imatrix_q_method, private_repo, train_data_file, split_model, split_max_tensors, split_max_size, ollamafy, latest, maintainer, oauth_token: gr.OAuthToken | None):
 
 
 
26
  if oauth_token.token is None:
27
  raise ValueError("You must be logged in to use GGUF-my-repo")
28
  model_name = model_id.split('/')[-1]
29
+ fp16 = f"{model_name}.fp16.gguf"
 
 
30
 
31
  try:
32
  api = HfApi(token=oauth_token.token)
 
52
  print(f"Current working directory: {os.getcwd()}")
53
  print(f"Model directory contents: {os.listdir(model_name)}")
54
 
55
+ conversion_script = "convert_hf_to_gguf.py"
56
+ fp16_conversion = f"python llama.cpp/{conversion_script} {model_name} --outtype f16 --outfile {fp16}"
57
+ result = subprocess.run(fp16_conversion, shell=True, capture_output=True)
58
+ print(result)
59
+ if result.returncode != 0:
60
+ raise Exception(f"Error converting to fp16: {result.stderr}")
61
+ print("Model converted to fp16 successfully!")
62
+ print(f"Converted model path: {fp16}")
63
+
64
+ ### Ollamafy ###
65
+ if ollama_model:
66
+ model_maintainer = model_id.split('/')[-2]
67
+ ollama_model_name = model_maintainer.lower() + '_' + model_name.lower()
68
+ ollama_modelfile_name = model_name + '_modelfile'
69
+ # model_path = f"{HOME}/.cache/huggingface/hub/{model_id}"
70
+
71
+ ollama_modelfile = open(ollama_modelfile_name, "w")
72
+ # ollama_modelfile_path = quantized_gguf_path
73
+ ollama_modelfile.write(quantized_gguf_path)
74
+ ollama_modelfile.close()
75
+ print(quantized_gguf_path)
76
+
77
+ for ollama_q_method in ollama_q_methods:
78
+ if ollama_q_method == "FP16":
79
+ ollama_conversion = f"ollama create -q {ollama_q_method} -f {model_file} {OLLAMA_USERNAME}/{ollama_model_name}:{ollama_q_method.lower()}"
80
+ else:
81
+ ollama_conversion = f"ollama create -f {model_file} {OLLAMA_USERNAME}/{ollama_model_name}:{ollama_q_method.lower()}"
82
+ ollama_conversion_result = subprocess.run(ollama_conversion, shell=True, capture_output=True)
83
+ print(ollama_conversion_result)
84
+ if ollama_conversion_result.returncode != 0:
85
+ raise Exception(f"Error converting to Ollama: {ollama_conversion_result.stderr}")
86
+ print("Model converted to Ollama successfully!")
87
+
88
+ if maintainer:
89
+ ollama_push = f"ollama push {OLLAMA_USERNAME}/{model_name}:{q_method.lower()}"
90
+ else:
91
+ ollama_push = f"ollama push {OLLAMA_USERNAME}/{ollama_model_name}:{q_method.lower()}"
92
+ ollama_push_result = subprocess.run(ollama_push, shell=True, capture_output=True)
93
+ print(ollama_push_result)
94
+ if ollama_push_result.returncode != 0:
95
+ raise Exception(f"Error converting to Ollama: {ollama_push_result.stderr}")
96
+ print("Model pushed to Ollama library successfully!")
97
 
98
+ if latest == True:
99
+ ollama_copy = f"ollama cp {OLLAMA_USERNAME}/{model_id.lower()}:{q_method.lower()} {OLLAMA_USERNAME}/{model_id.lower()}:latest"
100
+ ollama_copy_result = subprocess.run(ollama_copy, shell=True, capture_output=True)
101
+ print(ollama_copy_result)
102
+ if ollama_copy_result.returncode != 0:
103
+ raise Exception(f"Error converting to Ollama: {ollama_push_result.stderr}")
104
+ print("Model pushed to Ollama library successfully!")
105
+ if maintainer == True:
106
+ llama_push_latest = f"ollama push {OLLAMA_USERNAME}/{model_name}:latest"
107
+ else:
108
+ ollama_push_latest = f"ollama push {OLLAMA_USERNAME}/{ollama_model_name}:latest"
109
+ ollama_push_latest_result = subprocess.run(ollama_push_latest, shell=True, capture_output=True)
110
+ print(ollama_push_latest_result)
111
+ if ollama_push_latest_result.returncode != 0:
112
+ raise Exception(f"Error converting to Ollama: {ollama_push_result.stderr}")
113
+ print("Model pushed to Ollama library successfully!")
114
 
115
 
116
  except Exception as e:
 
125
  """
126
  # Create Gradio interface
127
  with gr.Blocks(css=css) as demo:
128
+ gr.LoginButton(min_width=250)
129
  gr.Markdown("You must be logged in to use Ollamafy.")
130
  gr.Markdown(ollama_pubkey.read().rstrip())
131
  ollama_pubkey.close()
 
132
 
133
  model_id = HuggingfaceHubSearch(
134
  label="Hub Model ID",
 
136
  search_type="model",
137
  )
138
 
139
+ ollama_q_method
140
+ latest = gr.Dropdown(
141
+ ollama_q_methods,
142
+ label="Ollama Lastest Quantization Method",
143
+ info="Chose which quantization will be labled with the latest tag in the Ollama Library",
144
  value="FP16",
145
  filterable=False,
146
+ visible=False
147
  )
148
  latest = gr.Checkbox(
149
  value=False,
150
  label="Latest",
151
  info="Copy Model to Ollama Library with the :latest tag"
152
  )
153
+
154
  maintainer = gr.Checkbox(
155
  value=False,
156
  label="Maintainer",
157
  info="This is your original repository on both Hugging Face and Ollama. (DO NOT USE!!!)"
158
  )
159
+
160
  iface = gr.Interface(
161
  fn=process_model,
162
  inputs=[
163
  model_id,
164
+ ollama_q_method,
165
  latest,
166
  maintainer
167
  ],