Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# app.py
|
| 2 |
+
|
| 3 |
+
from fastapi import FastAPI, HTTPException
|
| 4 |
+
from pydantic import BaseModel
|
| 5 |
+
from transformers import AutoModelForSequenceClassification, AutoTokenizer
|
| 6 |
+
from datasets import load_dataset
|
| 7 |
+
from sklearn.metrics import accuracy_score
|
| 8 |
+
import uvicorn
|
| 9 |
+
|
| 10 |
+
# Initialize FastAPI
|
| 11 |
+
app = FastAPI()
|
| 12 |
+
|
| 13 |
+
# Define a request model
|
| 14 |
+
class ModelRequest(BaseModel):
|
| 15 |
+
model_id: str
|
| 16 |
+
tokenizer_id: str
|
| 17 |
+
|
| 18 |
+
# In-memory leaderboard to store results
|
| 19 |
+
leaderboard = []
|
| 20 |
+
|
| 21 |
+
def load_model_and_tokenizer(model_id: str, tokenizer_id: str):
|
| 22 |
+
"""Load the model and tokenizer from Hugging Face Hub."""
|
| 23 |
+
try:
|
| 24 |
+
model = AutoModelForSequenceClassification.from_pretrained(model_id)
|
| 25 |
+
tokenizer = AutoTokenizer.from_pretrained(tokenizer_id)
|
| 26 |
+
return model, tokenizer
|
| 27 |
+
except Exception as e:
|
| 28 |
+
raise HTTPException(status_code=400, detail=str(e))
|
| 29 |
+
|
| 30 |
+
def evaluate_model(model, tokenizer):
|
| 31 |
+
"""Evaluate the model using a benchmark dataset."""
|
| 32 |
+
# Load a benchmark dataset (replace with your actual dataset)
|
| 33 |
+
dataset = load_dataset("glue", "mrpc") # Example: GLUE MRPC dataset
|
| 34 |
+
# Tokenize the inputs
|
| 35 |
+
inputs = tokenizer(dataset['test']['sentence1'], dataset['test']['sentence2'], padding=True, truncation=True, return_tensors="pt")
|
| 36 |
+
|
| 37 |
+
# Get predictions from the model
|
| 38 |
+
with torch.no_grad():
|
| 39 |
+
outputs = model(**inputs)
|
| 40 |
+
predictions = outputs.logits.argmax(dim=-1).numpy()
|
| 41 |
+
|
| 42 |
+
# Calculate accuracy
|
| 43 |
+
accuracy = accuracy_score(dataset['test']['label'], predictions)
|
| 44 |
+
return accuracy
|
| 45 |
+
|
| 46 |
+
def update_leaderboard(model_id: str, score: float):
|
| 47 |
+
"""Update the leaderboard with new results."""
|
| 48 |
+
leaderboard.append({"model_id": model_id, "score": score})
|
| 49 |
+
leaderboard.sort(key=lambda x: x['score'], reverse=True) # Sort by score descending
|
| 50 |
+
|
| 51 |
+
@app.post("/submit")
|
| 52 |
+
async def submit_model(request: ModelRequest):
|
| 53 |
+
"""Endpoint to submit a model for evaluation."""
|
| 54 |
+
model_id = request.model_id
|
| 55 |
+
tokenizer_id = request.tokenizer_id
|
| 56 |
+
|
| 57 |
+
model, tokenizer = load_model_and_tokenizer(model_id, tokenizer_id)
|
| 58 |
+
|
| 59 |
+
score = evaluate_model(model, tokenizer)
|
| 60 |
+
|
| 61 |
+
update_leaderboard(model_id, score)
|
| 62 |
+
|
| 63 |
+
return {"message": "Model evaluated successfully", "score": score}
|
| 64 |
+
|
| 65 |
+
@app.get("/leaderboard")
|
| 66 |
+
async def get_leaderboard():
|
| 67 |
+
"""Endpoint to retrieve the current leaderboard."""
|
| 68 |
+
return leaderboard
|
| 69 |
+
|
| 70 |
+
if __name__ == "__main__":
|
| 71 |
+
uvicorn.run(app, host="0.0.0.0", port=8000)
|