Spaces:
Sleeping
Sleeping
Upload app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
import torch.nn as nn
|
| 3 |
+
from transformers import AutoTokenizer, AutoModel
|
| 4 |
+
|
| 5 |
+
import streamlit as st
|
| 6 |
+
|
| 7 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 8 |
+
|
| 9 |
+
# β
Load tokenizer from current directory
|
| 10 |
+
tokenizer = AutoTokenizer.from_pretrained("./")
|
| 11 |
+
|
| 12 |
+
# β
Define model
|
| 13 |
+
class ScoringModel(nn.Module):
|
| 14 |
+
def __init__(self, base_model_name="microsoft/deberta-v3-small", dropout_rate=0.242):
|
| 15 |
+
super().__init__()
|
| 16 |
+
self.base = AutoModel.from_pretrained(base_model_name)
|
| 17 |
+
self.base.gradient_checkpointing_enable()
|
| 18 |
+
self.dropout1 = nn.Dropout(dropout_rate)
|
| 19 |
+
self.dropout2 = nn.Dropout(dropout_rate)
|
| 20 |
+
self.dropout3 = nn.Dropout(dropout_rate)
|
| 21 |
+
self.classifier = nn.Linear(self.base.config.hidden_size, 1)
|
| 22 |
+
|
| 23 |
+
def forward(self, input_ids, attention_mask):
|
| 24 |
+
hidden = self.base(input_ids=input_ids, attention_mask=attention_mask).last_hidden_state[:, 0]
|
| 25 |
+
logits = (self.classifier(self.dropout1(hidden)) +
|
| 26 |
+
self.classifier(self.dropout2(hidden)) +
|
| 27 |
+
self.classifier(self.dropout3(hidden))) / 3
|
| 28 |
+
return logits
|
| 29 |
+
|
| 30 |
+
# β
Load your fine-tuned weights
|
| 31 |
+
model = ScoringModel()
|
| 32 |
+
model.load_state_dict(torch.load("scoring_model.pt", map_location=device))
|
| 33 |
+
model.to(device)
|
| 34 |
+
model.eval()
|
| 35 |
+
|
| 36 |
+
# β
Streamlit UI
|
| 37 |
+
st.title("LLM Response Scoring App π")
|
| 38 |
+
prompt = st.text_area("Enter a prompt:")
|
| 39 |
+
response_a = st.text_area("Response A:")
|
| 40 |
+
response_b = st.text_area("Response B:")
|
| 41 |
+
|
| 42 |
+
if st.button("Predict Better Response"):
|
| 43 |
+
text_a = f"Prompt: {prompt} [SEP] {response_a}"
|
| 44 |
+
text_b = f"Prompt: {prompt} [SEP] {response_b}"
|
| 45 |
+
|
| 46 |
+
inputs_a = tokenizer(text_a, return_tensors="pt", padding="max_length", truncation=True, max_length=186)
|
| 47 |
+
inputs_b = tokenizer(text_b, return_tensors="pt", padding="max_length", truncation=True, max_length=186)
|
| 48 |
+
|
| 49 |
+
inputs_a = {k: v.to(device) for k, v in inputs_a.items() if k in ["input_ids", "attention_mask"]}
|
| 50 |
+
inputs_b = {k: v.to(device) for k, v in inputs_b.items() if k in ["input_ids", "attention_mask"]}
|
| 51 |
+
|
| 52 |
+
with torch.no_grad():
|
| 53 |
+
score_a = model(**inputs_a).squeeze()
|
| 54 |
+
score_b = model(**inputs_b).squeeze()
|
| 55 |
+
|
| 56 |
+
prob_a = torch.sigmoid(score_a).item()
|
| 57 |
+
prob_b = torch.sigmoid(score_b).item()
|
| 58 |
+
|
| 59 |
+
if prob_b > prob_a:
|
| 60 |
+
st.success("β
Model predicts: Response B is better")
|
| 61 |
+
else:
|
| 62 |
+
st.success("β
Model predicts: Response A is better")
|
| 63 |
+
|
| 64 |
+
st.write(f"π΅ Probability A: {prob_a:.4f}")
|
| 65 |
+
st.write(f"π Probability B: {prob_b:.4f}")
|
| 66 |
+
|