Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,38 +1,57 @@
|
|
| 1 |
-
import
|
| 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 |
if __name__ == "__main__":
|
| 37 |
-
|
| 38 |
-
interface.launch()
|
|
|
|
| 1 |
+
from transformers import HfApi, pipeline
|
| 2 |
+
|
| 3 |
+
# Function to merge models at equal weights
|
| 4 |
+
def merge_models(models):
|
| 5 |
+
model_weights = [1.0 / len(models) for _ in range(len(models))]
|
| 6 |
+
merged_model = pipeline("text-generation", model=models, model_weights=model_weights)
|
| 7 |
+
return merged_model
|
| 8 |
+
|
| 9 |
+
# Retrieve code-generative models with config.json
|
| 10 |
+
def get_code_generative_models():
|
| 11 |
+
api = HfApi()
|
| 12 |
+
models_list = api.list_models()
|
| 13 |
+
|
| 14 |
+
code_generative_models = []
|
| 15 |
+
|
| 16 |
+
for model in models_list:
|
| 17 |
+
model_id = model.modelId
|
| 18 |
+
model_info = api.model_info(model_id)
|
| 19 |
+
|
| 20 |
+
if "config.json" in model_info.keys():
|
| 21 |
+
code_generative_models.append(model_id)
|
| 22 |
+
|
| 23 |
+
return code_generative_models
|
| 24 |
+
|
| 25 |
+
# Main function to merge models and deploy the merged model
|
| 26 |
+
def main():
|
| 27 |
+
code_generative_models = get_code_generative_models()
|
| 28 |
+
|
| 29 |
+
if len(code_generative_models) < 2:
|
| 30 |
+
print("At least two code-generative models with config.json files are required for merging.")
|
| 31 |
+
return
|
| 32 |
+
|
| 33 |
+
models = [model for model in code_generative_models[:2]] # Select the first two models for merging
|
| 34 |
+
merged_model = merge_models(models)
|
| 35 |
+
|
| 36 |
+
# Embed the merged model into a chat app for testing
|
| 37 |
+
chat_app = pipeline("text-generation", model=merged_model)
|
| 38 |
+
|
| 39 |
+
# Provide options for the user to download the code/config or deploy the merged model
|
| 40 |
+
print("Chat App Ready for Testing!")
|
| 41 |
+
print("Options:")
|
| 42 |
+
print("1. Download Code/Config")
|
| 43 |
+
print("2. Deploy as a Unique Space (Requires Write-Permission API Key)")
|
| 44 |
+
|
| 45 |
+
user_choice = input("Enter your choice (1 or 2): ")
|
| 46 |
+
|
| 47 |
+
if user_choice == "1":
|
| 48 |
+
# Download code/config
|
| 49 |
+
merged_model.save_pretrained("merged_model")
|
| 50 |
+
|
| 51 |
+
elif user_choice == "2":
|
| 52 |
+
# Deploy as a Unique Space with write-permission API Key
|
| 53 |
+
api_key = input("Enter your write-permission API Key: ")
|
| 54 |
+
# Code to deploy the merged model using the provided API key
|
| 55 |
|
| 56 |
if __name__ == "__main__":
|
| 57 |
+
main()
|
|
|