Spaces:
Sleeping
Sleeping
File size: 1,705 Bytes
bf7d9e2 | 1 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 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | # Gradio
import gradio as gr
# ML
from transformers import AutoTokenizer, AutoModelForSequenceClassification
import torch
from typing import Tuple, Dict
model_path = "./"
tokenizer = AutoTokenizer.from_pretrained(model_path)
model = AutoModelForSequenceClassification.from_pretrained(model_path)
model.eval()
reverse_label_mapping = {0: 'gpt-4.1-nano', 1: 'gpt-4.1', 2: 'o4-mini'}
def route_query(query: str) -> Tuple[str, float, Dict[str, str]]:
"""
Route query endpoint
"""
if not query.strip():
return "Please enter a query", 0.0, {}
inputs = tokenizer(
query,
padding='max_length',
truncation=True,
max_length=128,
return_tensors='pt'
)
with torch.no_grad():
outputs = model(**inputs)
predictions = torch.nn.functional.softmax(outputs.logits, dim=-1)
predicted_class = torch.argmax(predictions, dim=-1)
confidence = torch.max(predictions, dim=-1)[0]
probs = predictions.cpu().numpy()[0]
probabilities = {reverse_label_mapping[i]: f"{prob:.3f}" for i, prob in enumerate(probs)}
recommended_model = reverse_label_mapping[predicted_class.item()]
return recommended_model, f"{confidence.item():.3f}", probabilities
iface = gr.Interface(
fn=route_query,
inputs=gr.Textbox(lines=2, placeholder="Enter your query here..."),
outputs=[
gr.Textbox(label="Recommended Model"),
gr.Textbox(label="Confidence"),
gr.JSON(label="All Probabilities")
],
title="GPT Router Model",
description="Enter a query to get routing recommendation to the appropriate GPT model"
)
if __name__ == "__main__":
iface.launch() |