Update app.py
Browse files
app.py
CHANGED
@@ -15,35 +15,35 @@ def fetch_models_from_hf(task_filter, limit=10):
|
|
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 |
-
#
|
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 |
-
#
|
30 |
def get_weighted_recommendations_from_hf(user_query, 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 |
model_ids = [model["model_id"] for model in model_data]
|
37 |
model_tags = [' '.join(model["tags"]) for model in model_data]
|
38 |
-
|
39 |
model_embeddings = semantic_model.encode(model_tags)
|
40 |
user_embedding = semantic_model.encode(user_query)
|
41 |
-
|
42 |
similarities = util.pytorch_cos_sim(user_embedding, model_embeddings)[0].numpy()
|
43 |
-
|
44 |
downloads = normalize([model["downloads"] for model in model_data])
|
45 |
likes = normalize([model["likes"] for model in model_data])
|
46 |
-
|
47 |
final_scores = []
|
48 |
for i in range(len(model_data)):
|
49 |
score = (
|
@@ -52,9 +52,9 @@ def get_weighted_recommendations_from_hf(user_query, task_filter, weights=None):
|
|
52 |
weights["likes"] * likes[i]
|
53 |
)
|
54 |
final_scores.append((model_ids[i], score, similarities[i], downloads[i], likes[i]))
|
55 |
-
|
56 |
ranked_recommendations = sorted(final_scores, key=lambda x: x[1], reverse=True)
|
57 |
-
|
58 |
result = []
|
59 |
for rank, (model_id, final_score, sim, downloads, likes) in enumerate(ranked_recommendations, 1):
|
60 |
result.append(f"Rank {rank}: Model ID: {model_id}, Final Score: {final_score:.4f}, "
|
@@ -62,22 +62,23 @@ def get_weighted_recommendations_from_hf(user_query, task_filter, weights=None):
|
|
62 |
|
63 |
return '\n'.join(result)
|
64 |
|
65 |
-
#
|
66 |
-
def
|
67 |
-
|
|
|
68 |
|
69 |
# Gradio Interface
|
70 |
-
|
71 |
-
fn=
|
72 |
inputs=[
|
73 |
gr.Textbox(label="Enter your query", placeholder="What kind of model are you looking for?"),
|
74 |
-
gr.Textbox(label="Task Filter
|
|
|
75 |
],
|
76 |
-
outputs=gr.Textbox(),
|
77 |
-
title="Hugging Face Model
|
78 |
-
description="This chatbot recommends models from Hugging Face based on your query."
|
79 |
)
|
80 |
|
81 |
-
# Launch the Gradio interface
|
82 |
if __name__ == "__main__":
|
83 |
-
|
|
|
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 user query and additional metrics
|
30 |
def get_weighted_recommendations_from_hf(user_query, 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 |
model_ids = [model["model_id"] for model in model_data]
|
37 |
model_tags = [' '.join(model["tags"]) for model in model_data]
|
38 |
+
|
39 |
model_embeddings = semantic_model.encode(model_tags)
|
40 |
user_embedding = semantic_model.encode(user_query)
|
41 |
+
|
42 |
similarities = util.pytorch_cos_sim(user_embedding, model_embeddings)[0].numpy()
|
43 |
+
|
44 |
downloads = normalize([model["downloads"] for model in model_data])
|
45 |
likes = normalize([model["likes"] for model in model_data])
|
46 |
+
|
47 |
final_scores = []
|
48 |
for i in range(len(model_data)):
|
49 |
score = (
|
|
|
52 |
weights["likes"] * likes[i]
|
53 |
)
|
54 |
final_scores.append((model_ids[i], score, similarities[i], downloads[i], likes[i]))
|
55 |
+
|
56 |
ranked_recommendations = sorted(final_scores, key=lambda x: x[1], reverse=True)
|
57 |
+
|
58 |
result = []
|
59 |
for rank, (model_id, final_score, sim, downloads, likes) in enumerate(ranked_recommendations, 1):
|
60 |
result.append(f"Rank {rank}: Model ID: {model_id}, Final Score: {final_score:.4f}, "
|
|
|
62 |
|
63 |
return '\n'.join(result)
|
64 |
|
65 |
+
# Gradio chatbot interface
|
66 |
+
def respond(user_query, task_filter, history, weights=None):
|
67 |
+
# Provide model recommendations based on the user's query and task filter
|
68 |
+
return get_weighted_recommendations_from_hf(user_query, task_filter, weights)
|
69 |
|
70 |
# Gradio Interface
|
71 |
+
demo = gr.Interface(
|
72 |
+
fn=respond,
|
73 |
inputs=[
|
74 |
gr.Textbox(label="Enter your query", placeholder="What kind of model are you looking for?"),
|
75 |
+
gr.Textbox(label="Task Filter", placeholder="Enter the task, e.g., text-classification"),
|
76 |
+
gr.Textbox(value="You are using the Hugging Face model recommender system.", label="System message")
|
77 |
],
|
78 |
+
outputs=gr.Textbox(label="Model Recommendations"),
|
79 |
+
title="Hugging Face Model Recommender",
|
80 |
+
description="This chatbot recommends models from Hugging Face based on your query and task."
|
81 |
)
|
82 |
|
|
|
83 |
if __name__ == "__main__":
|
84 |
+
demo.launch(share=True)
|