Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
rajkumarrrk
commited on
Commit
•
b585e42
1
Parent(s):
a09c6ce
Update app.py
Browse files
app.py
CHANGED
@@ -1,7 +1,50 @@
|
|
1 |
import gradio as gr
|
|
|
|
|
|
|
2 |
|
3 |
-
def greet(name):
|
4 |
-
return "Hello " + name + "!!"
|
5 |
|
6 |
-
|
7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
from transformers import AutoModelForSequenceClassification, AutoTokenizer
|
3 |
+
from jinja2 import Template
|
4 |
+
import torch
|
5 |
|
|
|
|
|
6 |
|
7 |
+
# load the judge
|
8 |
+
device = "cuda:0"
|
9 |
+
model_name = "collinear-ai/collinear-reliability-judge-v1-deberta-ext"
|
10 |
+
model = AutoModelForSequenceClassification.from_pretrained(model_name).to(device)
|
11 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
12 |
+
|
13 |
+
|
14 |
+
# tempalte
|
15 |
+
template = Template(
|
16 |
+
"""
|
17 |
+
# Document:
|
18 |
+
{{ text }}
|
19 |
+
|
20 |
+
# Conversation:
|
21 |
+
{{ conversation }}
|
22 |
+
"""
|
23 |
+
)
|
24 |
+
|
25 |
+
|
26 |
+
def judge_reliability(document: str, conversation: str):
|
27 |
+
with torch.no_grad():
|
28 |
+
text = template.render(text=document, conversation=conversation)
|
29 |
+
encoded = tokenizer([text], padding=True)
|
30 |
+
input_ids = torch.tensor(encoded.input_ids).to(device)
|
31 |
+
attention_mask = torch.tensor(encoded.attention_mask).to(device)
|
32 |
+
outputs = model.forward(input_ids=input_ids, attention_mask=attention_mask)
|
33 |
+
outputs = torch.softmax(outputs.logits, axis=1)
|
34 |
+
results = f"Reliability Score: {outputs[0][1]}"
|
35 |
+
return results
|
36 |
+
|
37 |
+
demo = gr.Interface(
|
38 |
+
fn=judge_reliability,
|
39 |
+
inputs=[
|
40 |
+
gr.Textbox(label="Document", lines=5, value="CV was born in Iowa"),
|
41 |
+
gr.Textbox(label="Conversation", lines=5, value='[{"role": "user", "content": "Where are you born?"}, {"role": "assistant", "content": "I am born in Iowa"}]')
|
42 |
+
],
|
43 |
+
outputs=gr.Textbox(label="Results"),
|
44 |
+
title="Collinear Reliability Judge",
|
45 |
+
description="Enter a document and conversation (json formatted) to judge reliability. Note: this judges if the last assistant turn is faithful according to the given document ",
|
46 |
+
theme="default"
|
47 |
+
)
|
48 |
+
|
49 |
+
if __name__ == "__main__":
|
50 |
+
demo.launch()
|