ruffy369's picture
Update app.py
4cc7261 verified
raw
history blame
3.26 kB
import gradio as gr
from huggingface_hub import list_models
from sentence_transformers import SentenceTransformer, util
import numpy as np
# Load sentence transformer model for similarity calculation
semantic_model = SentenceTransformer('all-MiniLM-L6-v2')
# Function to fetch models from Hugging Face based on dynamic task filter
def fetch_models_from_hf(task_filter, limit=10):
models = list_models(filter=task_filter, limit=limit)
model_data = [
{
"model_id": model.modelId,
"tags": model.tags,
"downloads": model.downloads,
"likes": model.likes,
"last_modified": model.lastModified # You could use this for recency
}
for model in models
]
return model_data
# Normalize values for a 0-1 range
def normalize(values):
min_val, max_val = min(values), max(values)
return [(v - min_val) / (max_val - min_val) if max_val > min_val else 0 for v in values]
# Get weighted recommendations based on user query and additional metrics
def get_weighted_recommendations_from_hf(user_query, task_filter, weights=None):
if weights is None:
weights = {"similarity": 0.7, "downloads": 0.2, "likes": 0.1} # Adjustable
model_data = fetch_models_from_hf(task_filter)
model_ids = [model["model_id"] for model in model_data]
model_tags = [' '.join(model["tags"]) for model in model_data]
model_embeddings = semantic_model.encode(model_tags)
user_embedding = semantic_model.encode(user_query)
similarities = util.pytorch_cos_sim(user_embedding, model_embeddings)[0].numpy()
downloads = normalize([model["downloads"] for model in model_data])
likes = normalize([model["likes"] for model in model_data])
final_scores = []
for i in range(len(model_data)):
score = (
weights["similarity"] * similarities[i] +
weights["downloads"] * downloads[i] +
weights["likes"] * likes[i]
)
final_scores.append((model_ids[i], score, similarities[i], downloads[i], likes[i]))
ranked_recommendations = sorted(final_scores, key=lambda x: x[1], reverse=True)
result = []
for rank, (model_id, final_score, sim, downloads, likes) in enumerate(ranked_recommendations, 1):
result.append(f"Rank {rank}: Model ID: {model_id}, Final Score: {final_score:.4f}, "
f"Similarity: {sim:.4f}, Downloads: {downloads:.4f}, Likes: {likes:.4f}")
return '\n'.join(result)
# Define a Gradio interface function
def chatbot_interface(user_query, task_filter):
return get_weighted_recommendations_from_hf(user_query, task_filter)
# Gradio Interface
interface = gr.Interface(
fn=chatbot_interface,
inputs=[
gr.inputs.Textbox(label="Enter your query", placeholder="What kind of model or tag are you looking for?"),
gr.inputs.Textbox(label="Task Filter (e.g., text-classification, summarization, atari)", placeholder="Enter the task"),
],
outputs="text",
title="Hugging Face Model Recommendation Chatbot",
description="This chatbot recommends models from Hugging Face based on your query."
)
# Launch the Gradio interface
if __name__ == "__main__":
interface.launch()