Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from huggingface_hub import list_models
|
| 3 |
+
from sentence_transformers import SentenceTransformer, util
|
| 4 |
+
import numpy as np
|
| 5 |
+
|
| 6 |
+
# Load sentence transformer model for similarity calculation
|
| 7 |
+
semantic_model = SentenceTransformer('all-MiniLM-L6-v2')
|
| 8 |
+
|
| 9 |
+
# Function to fetch models from Hugging Face based on dynamic task filter
|
| 10 |
+
def fetch_models_from_hf(task_filter, limit=10):
|
| 11 |
+
models = list_models(filter=task_filter, limit=limit)
|
| 12 |
+
model_data = [
|
| 13 |
+
{
|
| 14 |
+
"model_id": model.modelId,
|
| 15 |
+
"tags": model.tags,
|
| 16 |
+
"downloads": model.downloads,
|
| 17 |
+
"likes": model.likes,
|
| 18 |
+
"last_modified": model.lastModified
|
| 19 |
+
}
|
| 20 |
+
for model in models
|
| 21 |
+
]
|
| 22 |
+
return model_data
|
| 23 |
+
|
| 24 |
+
# Function to normalize a list of values to a 0-1 range
|
| 25 |
+
def normalize(values):
|
| 26 |
+
min_val, max_val = min(values), max(values)
|
| 27 |
+
return [(v - min_val) / (max_val - min_val) if max_val > min_val else 0 for v in values]
|
| 28 |
+
|
| 29 |
+
# Function to get weighted recommendations based on task filter and additional metrics
|
| 30 |
+
def get_weighted_recommendations_from_hf(task_filter, weights=None):
|
| 31 |
+
if weights is None:
|
| 32 |
+
weights = {"similarity": 0.7, "downloads": 0.2, "likes": 0.1}
|
| 33 |
+
|
| 34 |
+
model_data = fetch_models_from_hf(task_filter)
|
| 35 |
+
|
| 36 |
+
if len(model_data) == 0:
|
| 37 |
+
return "No models found for the specified task filter."
|
| 38 |
+
|
| 39 |
+
model_ids = [model["model_id"] for model in model_data]
|
| 40 |
+
model_tags = [' '.join(model["tags"]) for model in model_data]
|
| 41 |
+
|
| 42 |
+
# Use a fixed user query based on task filter
|
| 43 |
+
user_query = f"best model for {task_filter}"
|
| 44 |
+
|
| 45 |
+
model_embeddings = semantic_model.encode(model_tags)
|
| 46 |
+
user_embedding = semantic_model.encode(user_query)
|
| 47 |
+
|
| 48 |
+
similarities = util.pytorch_cos_sim(user_embedding, model_embeddings)[0].numpy()
|
| 49 |
+
|
| 50 |
+
downloads = normalize([model["downloads"] for model in model_data])
|
| 51 |
+
likes = normalize([model["likes"] for model in model_data])
|
| 52 |
+
|
| 53 |
+
final_scores = []
|
| 54 |
+
for i in range(len(model_data)):
|
| 55 |
+
score = (
|
| 56 |
+
weights["similarity"] * similarities[i] +
|
| 57 |
+
weights["downloads"] * downloads[i] +
|
| 58 |
+
weights["likes"] * likes[i]
|
| 59 |
+
)
|
| 60 |
+
final_scores.append((model_ids[i], score, similarities[i], downloads[i], likes[i]))
|
| 61 |
+
|
| 62 |
+
ranked_recommendations = sorted(final_scores, key=lambda x: x[1], reverse=True)
|
| 63 |
+
|
| 64 |
+
result = []
|
| 65 |
+
for rank, (model_id, final_score, sim, downloads, likes) in enumerate(ranked_recommendations, 1):
|
| 66 |
+
result.append(f"Rank {rank}: Model ID: {model_id}")
|
| 67 |
+
|
| 68 |
+
return '\n'.join(result)
|
| 69 |
+
|
| 70 |
+
# Gradio chatbot interface
|
| 71 |
+
def respond(task_filter, history=None, weights=None):
|
| 72 |
+
# Provide model recommendations based on the task filter
|
| 73 |
+
return get_weighted_recommendations_from_hf(task_filter, weights)
|
| 74 |
+
|
| 75 |
+
# Gradio Interface
|
| 76 |
+
demo = gr.Interface(
|
| 77 |
+
fn=respond,
|
| 78 |
+
inputs=[
|
| 79 |
+
gr.Textbox(label="Task Filter", placeholder="Enter the task, e.g., text-classification, atari, question-answering"),
|
| 80 |
+
gr.Textbox(value="You are using the Hugging Face model recommender system.", label="System message")
|
| 81 |
+
],
|
| 82 |
+
outputs=gr.Textbox(label="Model Recommendations"),
|
| 83 |
+
title="Hugging Face Model Recommender",
|
| 84 |
+
description="This chatbot recommends models from Hugging Face based on the task or tag you're interested in. It combines various attributes of a model on hub like downloads, likes, etc. to suggest models with ranks from 1-10. In general term basically it intelligently combines the search filter for recommendation"
|
| 85 |
+
)
|
| 86 |
+
|
| 87 |
+
if __name__ == "__main__":
|
| 88 |
+
demo.launch(share=True)
|