allenpark commited on
Commit
f9a609b
·
1 Parent(s): b94625f

fix: updated and functional code

Browse files
Files changed (1) hide show
  1. app.py +175 -0
app.py CHANGED
@@ -0,0 +1,175 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import re
3
+ import io
4
+ import json
5
+ from typing import List, Tuple, Union
6
+ from pathlib import Path
7
+ import gradio as gr
8
+ from leptonai import Client
9
+
10
+ LEPTON_API_TOKEN = os.environ.get("LEPTON_API_TOKEN", None)
11
+
12
+ client = Client("https://yb15a7dy-glider.tin.lepton.run", "glider", LEPTON_API_TOKEN)
13
+
14
+ PROMPT = """Analyze the following pass criteria carefully and score the text based on the rubric defined below.
15
+
16
+ To perform this evaluation, you must:
17
+
18
+ 1. Understand the text tags, pass criteria and rubric thoroughly.
19
+ 2. Review the finer details of the text and the rubric.
20
+ 3. Compare the tags to be evaluated to the score descriptions in the rubric.
21
+ 4. Pay close attention to small details that might impact the final score and form accurate associations between tags and pass criteria.
22
+ 5. Write a detailed reasoning justifying your evaluation in a bullet point format.
23
+ 6. The reasoning must summarize the overall strengths and weaknesses of the output while quoting exact phrases from the output wherever required.
24
+ 7. Output a list of words or phrases that you believe are the most important in determining the score.
25
+ 8. Assign a final score based on the scoring rubric.
26
+
27
+ Data to evaluate:
28
+ {user_input}
29
+
30
+ Pass Criteria:
31
+ {pass_criteria}
32
+
33
+ Rubric:
34
+ {rubric}
35
+
36
+ Your output must in the following format:
37
+ <reasoning>
38
+ [Detailed reasoning justifying your evaluation in a bullet point format according to the specifics defined above]
39
+ </reasoning>
40
+ <highlight>
41
+ [List of words or phrases that you believe are the most important in determining the score]
42
+ </highlight>
43
+ <score>
44
+ [The final integer score assigned based on the scoring rubric]
45
+ </score>
46
+ """
47
+
48
+ EXAMPLES = [
49
+ {
50
+ "emoji": "🌁",
51
+ "model_output": "The sky is green.",
52
+ "user_input": "What color is the sky?",
53
+ "gold_answer": "",
54
+ "retrieved_context": "The sky is blue.",
55
+ "pass_criteria": "Is the MODEL OUTPUT grounded in the CONTEXT?",
56
+ "rubric": "0. The pass criteria is not satisfied and not accurately followed\n1. The pass criteria is satisfied and accurately followed",
57
+ }
58
+
59
+ ]
60
+
61
+ HEADER = """
62
+ <div style="width: 100%; display: flex; flex-direction: column; gap: 24px; padding-top: 24px; position: relative">
63
+ <img src="https://postimage.me/image/ICONGLIDER.U5e6TO" width="175" style="position: absolute; top: 0; right: 48px">
64
+ <div style="display: flex; justify-content: space-between; z-index: 1;">
65
+ <a href="https://www.patronus.ai">
66
+ <img src="https://postimage.me/images/2024/07/31/FullLogo_ColorDefault.png" width="250">
67
+ </a>
68
+ <div style="display: flex; gap: 12px;">
69
+ <a href="https://huggingface.co/PatronusAI/glider">
70
+ <img src="https://img.shields.io/badge/%F0%9F%A4%97%20Model_Card-Huggingface-orange" height="20">
71
+ </a>
72
+ <a href="https://github.com/patronus-ai/glider">
73
+ <img src="https://img.shields.io/badge/GitHub-Glider-indigo" height="20">
74
+ </a>
75
+ <a href="https://arxiv.org/abs/2412.14140">
76
+ <img src="https://img.shields.io/badge/arXiv-2412.14140-b31b1b.svg" height="20">
77
+ </a>
78
+ </div>
79
+ </div>
80
+ <div>
81
+ <h1>GLIDER: Grading LLM Interactions and Decisions using Explainable Ranking</h1>
82
+ <h2>Patronus GLIDER Demo</h2>
83
+ </div>
84
+ </div>
85
+
86
+ **GLIDER** is a powerful 3B evaluator LLM that can score any text input and associated context on arbitrary user defined criteria.
87
+
88
+ **Getting Started**: First, provide a model output (text generated by your model) and user input (text used to prompt your model) and optionally a gold answer (label or gold answer to the prompt) and retrieved context (context used for text generated by your model). Next, provide a pass criteria (description of a passing evaluation). Finally, provide an optional rubric (scoring scales with explanations) and then click submit. The GLIDER Output panel will provide a score and reasoning which is a human readable explanation of the score.
89
+
90
+ """
91
+
92
+ EXAMPLES_HEADER = """
93
+ # Try it Yourself!
94
+ """
95
+
96
+ css = """
97
+ .example-button {
98
+ width: fit-content;
99
+ font-size: 1rem;
100
+ font-weight: 400 !important;
101
+ padding: .5rem 1rem;
102
+ text-align: start;
103
+ }
104
+ .fixed-height-button {
105
+ height: fit-content;
106
+ word-break: break-all;
107
+ font-size: .85rem;
108
+ }
109
+ """
110
+
111
+ def format_string(retrieved_context, user_input, model_output, gold_answer):
112
+ parts = []
113
+ if retrieved_context:
114
+ parts.append(f"<CONTEXT>\n{retrieved_context}\n</CONTEXT>")
115
+ if user_input:
116
+ parts.append(f"<USER INPUT>\n{user_input}\n</USER INPUT>")
117
+ if model_output:
118
+ parts.append(f"<MODEL OUTPUT>\n{model_output}\n</MODEL OUTPUT>")
119
+ if gold_answer:
120
+ parts.append(f"<GOLD ANSWER>\n{gold_answer}\n</GOLD ANSWER>")
121
+ return "\n".join(parts)
122
+
123
+ def model_call(model_output, user_input, gold_answer, retrieved_context, pass_criteria, rubric):
124
+ if model_output == "" or user_input == "" or pass_criteria == "":
125
+ return "", "", ""
126
+ combined_user_input = format_string(retrieved_context, user_input, model_output, gold_answer)
127
+ NEW_PROMPT_FORMAT = PROMPT.format(user_input=combined_user_input, pass_criteria=pass_criteria, rubric=rubric)
128
+ print("NEW_PROMPT_FORMAT", NEW_PROMPT_FORMAT)
129
+ response = client.api.v1.chat.completions(
130
+ model="glider",
131
+ messages=[{"role": "user", "content": NEW_PROMPT_FORMAT}],
132
+ temperature=0,
133
+ top_p=0.999,
134
+ max_tokens=2048,
135
+ stream=False,
136
+ )
137
+ score, reasoning, highlight_spans = extract_spans(response["choices"][0]["message"]["content"])
138
+ return score, reasoning, highlight_spans
139
+
140
+ def select_template(template):
141
+ return template["model_output"], template["user_input"], template["gold_answer"], template["retrieved_context"], template["pass_criteria"], template["rubric"]
142
+
143
+ with gr.Blocks(css=css, theme=gr.themes.Default(spacing_size="sm", font=[gr.themes.GoogleFont("Plus Jakarta Sans"), "Arial", "sans-serif"], primary_hue="indigo", secondary_hue="purple")) as demo:
144
+ gr.Markdown(HEADER)
145
+ with gr.Row(equal_height=True):
146
+ with gr.Column(scale=1):
147
+ gr.Markdown("**Your Inputs**")
148
+ model_output = gr.Textbox(label="MODEL OUTPUT (required)")
149
+ user_input = gr.Textbox(label="USER INPUT (required)")
150
+ gold_answer = gr.Textbox(label="GOLD ANSWER")
151
+ retrieved_context = gr.Textbox(label="RETRIEVED CONTEXT")
152
+ pass_criteria = gr.Textbox(label="Pass Criteria (required)")
153
+ rubric = gr.Textbox(label="Rubric")
154
+ with gr.Row():
155
+ clear_btn = gr.ClearButton([model_output, user_input, gold_answer, retrieved_context, pass_criteria, rubric])
156
+ submit_button = gr.Button("Submit", variant="primary")
157
+ with gr.Column(scale=1):
158
+ gr.Markdown("**GLIDER Output**")
159
+ score = gr.Textbox(label="Score")
160
+ reasoning = gr.Textbox(label="Reasoning")
161
+ highlights = gr.Textbox(label="Highlights")
162
+ gr.Markdown("&nbsp;")
163
+ gr.Markdown(EXAMPLES_HEADER)
164
+ with gr.Row():
165
+ with gr.Column():
166
+ for _, example in enumerate(EXAMPLES):
167
+ template_btn = gr.Button(f"{example['emoji']} {example['model_output']}", elem_classes="example-button")
168
+ template_btn.click(
169
+ fn=select_template,
170
+ inputs=[gr.State(example)],
171
+ outputs=[model_output, user_input, gold_answer, retrieved_context, pass_criteria, rubric]
172
+ )
173
+
174
+ submit_button.click(fn=model_call, inputs=[model_output, user_input, gold_answer, retrieved_context, pass_criteria, rubric], outputs=[score, reasoning, highlights])
175
+ demo.launch()