Spaces:
Sleeping
Sleeping
Commit
·
ba45294
1
Parent(s):
1f82605
update
Browse files
app.py
CHANGED
|
@@ -69,6 +69,9 @@ EXAMPLES = [
|
|
| 69 |
|
| 70 |
# Verification function
|
| 71 |
def verify_answer(question, ground_truth, model_answer, temperature, top_p, max_tokens):
|
|
|
|
|
|
|
|
|
|
| 72 |
# Format the prompt with user inputs
|
| 73 |
prompt = LV_PROMPT.format(
|
| 74 |
question=question,
|
|
@@ -110,71 +113,191 @@ def load_example(example_index):
|
|
| 110 |
example["tokens"]
|
| 111 |
)
|
| 112 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 113 |
# Create the Gradio interface
|
| 114 |
with gr.Blocks(
|
| 115 |
-
theme=gr.themes.
|
| 116 |
-
primary_hue="
|
|
|
|
| 117 |
font=[gr.themes.GoogleFont("Inter"), "ui-sans-serif", "system-ui", "sans-serif"]
|
| 118 |
),
|
| 119 |
-
css=
|
| 120 |
-
.container { max-width: 1000px; margin: auto; }
|
| 121 |
-
.example-btn { min-width: 140px; }
|
| 122 |
-
.title { text-align: center; margin-bottom: 1rem; }
|
| 123 |
-
.result-box { min-height: 100px; }
|
| 124 |
-
"""
|
| 125 |
) as demo:
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
|
|
|
| 142 |
|
| 143 |
-
# Main
|
| 144 |
with gr.Row(equal_height=True):
|
| 145 |
# Left column - Inputs
|
| 146 |
-
with gr.Column():
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 162 |
|
| 163 |
-
# Right column - Result
|
| 164 |
-
with gr.Column():
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
|
| 173 |
# Examples section
|
| 174 |
-
gr.Markdown("###
|
| 175 |
-
with gr.
|
| 176 |
for i, ex in enumerate(EXAMPLES):
|
| 177 |
-
btn = gr.Button(ex["name"],
|
| 178 |
btn.click(
|
| 179 |
fn=lambda idx=i: load_example(idx),
|
| 180 |
outputs=[question, ground_truth, model_answer, temperature, top_p, max_tokens]
|
|
@@ -183,12 +306,11 @@ with gr.Blocks(
|
|
| 183 |
btn.click(
|
| 184 |
fn=verify_answer,
|
| 185 |
inputs=[question, ground_truth, model_answer, temperature, top_p, max_tokens],
|
| 186 |
-
outputs=result
|
| 187 |
-
queue=False
|
| 188 |
)
|
| 189 |
|
| 190 |
-
# Advanced Settings
|
| 191 |
-
with gr.Accordion("
|
| 192 |
with gr.Row():
|
| 193 |
temp_slider = gr.Slider(0, 1, value=0.3, step=0.1, label="Temperature")
|
| 194 |
top_p_slider = gr.Slider(0.1, 1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)")
|
|
@@ -199,22 +321,10 @@ with gr.Blocks(
|
|
| 199 |
top_p_slider.change(lambda x: x, inputs=[top_p_slider], outputs=[top_p])
|
| 200 |
max_tokens_slider.change(lambda x: x, inputs=[max_tokens_slider], outputs=[max_tokens])
|
| 201 |
|
| 202 |
-
#
|
| 203 |
-
with gr.Accordion("
|
| 204 |
gr.Markdown(
|
| 205 |
"""
|
| 206 |
-
### What This Tool Does
|
| 207 |
-
|
| 208 |
-
This verification tool uses the TinyV-1.5B model to determine if answers are semantically equivalent,
|
| 209 |
-
even when they have different:
|
| 210 |
-
|
| 211 |
-
- **Formatting** (LaTeX vs. plain text, spacing, etc.)
|
| 212 |
-
- **Ordering** (e.g., listing items in different orders)
|
| 213 |
-
- **Phrasing** (paraphrases with the same meaning)
|
| 214 |
-
- **Variable labeling** (with or without variable names)
|
| 215 |
-
|
| 216 |
-
### API Usage
|
| 217 |
-
|
| 218 |
```python
|
| 219 |
from gradio_client import Client
|
| 220 |
|
|
@@ -225,13 +335,21 @@ with gr.Blocks(
|
|
| 225 |
model_answer="Paris is the capital of France.",
|
| 226 |
temperature=0.3,
|
| 227 |
top_p=0.95,
|
| 228 |
-
max_tokens=
|
| 229 |
api_name="/verify_answer"
|
| 230 |
)
|
| 231 |
print(result)
|
| 232 |
```
|
| 233 |
"""
|
| 234 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 235 |
|
| 236 |
# Connect the interface to the verification function
|
| 237 |
verify_btn.click(
|
|
|
|
| 69 |
|
| 70 |
# Verification function
|
| 71 |
def verify_answer(question, ground_truth, model_answer, temperature, top_p, max_tokens):
|
| 72 |
+
if not question or not ground_truth or not model_answer:
|
| 73 |
+
return "Please fill in all fields: Question, Ground Truth Answer, and Model Answer."
|
| 74 |
+
|
| 75 |
# Format the prompt with user inputs
|
| 76 |
prompt = LV_PROMPT.format(
|
| 77 |
question=question,
|
|
|
|
| 113 |
example["tokens"]
|
| 114 |
)
|
| 115 |
|
| 116 |
+
# Custom CSS for better styling
|
| 117 |
+
custom_css = """
|
| 118 |
+
.gradio-container {
|
| 119 |
+
max-width: 1080px !important;
|
| 120 |
+
margin: auto !important;
|
| 121 |
+
}
|
| 122 |
+
|
| 123 |
+
.main-container {
|
| 124 |
+
background-color: #f9f9f9;
|
| 125 |
+
border-radius: 10px;
|
| 126 |
+
padding: 15px;
|
| 127 |
+
box-shadow: 0 0 5px rgba(0,0,0,0.1);
|
| 128 |
+
}
|
| 129 |
+
|
| 130 |
+
.header {
|
| 131 |
+
background: linear-gradient(90deg, #4776E6 0%, #8E54E9 100%);
|
| 132 |
+
color: white;
|
| 133 |
+
border-radius: 8px;
|
| 134 |
+
padding: 20px 30px;
|
| 135 |
+
margin-bottom: 20px;
|
| 136 |
+
text-align: center;
|
| 137 |
+
}
|
| 138 |
+
|
| 139 |
+
.header h1 {
|
| 140 |
+
font-size: 32px !important;
|
| 141 |
+
font-weight: 700 !important;
|
| 142 |
+
margin-bottom: 5px !important;
|
| 143 |
+
}
|
| 144 |
+
|
| 145 |
+
.header p {
|
| 146 |
+
font-size: 16px !important;
|
| 147 |
+
opacity: 0.9;
|
| 148 |
+
}
|
| 149 |
+
|
| 150 |
+
.example-btn {
|
| 151 |
+
min-width: 140px !important;
|
| 152 |
+
margin: 5px !important;
|
| 153 |
+
background-color: #f0f0f0 !important;
|
| 154 |
+
border: 1px solid #ddd !important;
|
| 155 |
+
transition: all 0.3s ease !important;
|
| 156 |
+
}
|
| 157 |
+
|
| 158 |
+
.example-btn:hover {
|
| 159 |
+
background-color: #e0e0e0 !important;
|
| 160 |
+
transform: translateY(-2px) !important;
|
| 161 |
+
box-shadow: 0 4px 6px rgba(0,0,0,0.1) !important;
|
| 162 |
+
}
|
| 163 |
+
|
| 164 |
+
.verify-btn {
|
| 165 |
+
background: linear-gradient(90deg, #4776E6 0%, #8E54E9 100%) !important;
|
| 166 |
+
color: white !important;
|
| 167 |
+
padding: 12px 20px !important;
|
| 168 |
+
font-size: 16px !important;
|
| 169 |
+
font-weight: 600 !important;
|
| 170 |
+
border-radius: 8px !important;
|
| 171 |
+
transition: all 0.3s ease !important;
|
| 172 |
+
border: none !important;
|
| 173 |
+
width: 100% !important;
|
| 174 |
+
margin-top: 10px !important;
|
| 175 |
+
}
|
| 176 |
+
|
| 177 |
+
.verify-btn:hover {
|
| 178 |
+
transform: translateY(-2px) !important;
|
| 179 |
+
box-shadow: 0 4px 10px rgba(0,0,0,0.2) !important;
|
| 180 |
+
}
|
| 181 |
+
|
| 182 |
+
.panel {
|
| 183 |
+
background-color: white;
|
| 184 |
+
border-radius: 8px;
|
| 185 |
+
padding: 15px;
|
| 186 |
+
box-shadow: 0 2px 4px rgba(0,0,0,0.05);
|
| 187 |
+
margin-bottom: 15px;
|
| 188 |
+
}
|
| 189 |
+
|
| 190 |
+
.result-panel {
|
| 191 |
+
background-color: #f8f9fa;
|
| 192 |
+
border-left: 4px solid #4776E6;
|
| 193 |
+
padding: 15px;
|
| 194 |
+
border-radius: 8px;
|
| 195 |
+
min-height: 120px;
|
| 196 |
+
}
|
| 197 |
+
|
| 198 |
+
.label {
|
| 199 |
+
font-weight: 600;
|
| 200 |
+
margin-bottom: 5px;
|
| 201 |
+
color: #444;
|
| 202 |
+
}
|
| 203 |
+
|
| 204 |
+
.examples-container {
|
| 205 |
+
display: flex;
|
| 206 |
+
flex-wrap: wrap;
|
| 207 |
+
justify-content: center;
|
| 208 |
+
margin-top: 10px;
|
| 209 |
+
margin-bottom: 20px;
|
| 210 |
+
}
|
| 211 |
+
|
| 212 |
+
.footer {
|
| 213 |
+
text-align: center;
|
| 214 |
+
font-size: 12px;
|
| 215 |
+
color: #888;
|
| 216 |
+
margin-top: 20px;
|
| 217 |
+
}
|
| 218 |
+
|
| 219 |
+
/* Make the accordion look better */
|
| 220 |
+
.accordions-container .label {
|
| 221 |
+
font-size: 14px !important;
|
| 222 |
+
font-weight: 600 !important;
|
| 223 |
+
}
|
| 224 |
+
"""
|
| 225 |
+
|
| 226 |
# Create the Gradio interface
|
| 227 |
with gr.Blocks(
|
| 228 |
+
theme=gr.themes.Monochrome(
|
| 229 |
+
primary_hue="indigo",
|
| 230 |
+
secondary_hue="blue",
|
| 231 |
font=[gr.themes.GoogleFont("Inter"), "ui-sans-serif", "system-ui", "sans-serif"]
|
| 232 |
),
|
| 233 |
+
css=custom_css
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 234 |
) as demo:
|
| 235 |
+
|
| 236 |
+
# Define these variables first so they can be used by the example loading function
|
| 237 |
+
temperature = gr.State(value=0.3)
|
| 238 |
+
top_p = gr.State(value=0.95)
|
| 239 |
+
max_tokens = gr.State(value=2)
|
| 240 |
+
|
| 241 |
+
# Main container
|
| 242 |
+
with gr.Box(elem_classes="main-container"):
|
| 243 |
+
# Header
|
| 244 |
+
with gr.Box(elem_classes="header"):
|
| 245 |
+
gr.Markdown(
|
| 246 |
+
"""
|
| 247 |
+
# TinyV - Answer Verification Tool
|
| 248 |
+
|
| 249 |
+
Verify if model-generated answers are correct compared to ground truth.
|
| 250 |
+
"""
|
| 251 |
+
)
|
| 252 |
|
| 253 |
+
# Main content area
|
| 254 |
with gr.Row(equal_height=True):
|
| 255 |
# Left column - Inputs
|
| 256 |
+
with gr.Column(scale=3):
|
| 257 |
+
with gr.Box(elem_classes="panel"):
|
| 258 |
+
gr.Markdown("### Input", elem_classes="label")
|
| 259 |
+
|
| 260 |
+
question = gr.Textbox(
|
| 261 |
+
lines=3,
|
| 262 |
+
label="Question",
|
| 263 |
+
placeholder="Enter the problem or question here...",
|
| 264 |
+
elem_classes="input-field"
|
| 265 |
+
)
|
| 266 |
+
|
| 267 |
+
with gr.Row():
|
| 268 |
+
with gr.Column(scale=1):
|
| 269 |
+
ground_truth = gr.Textbox(
|
| 270 |
+
lines=3,
|
| 271 |
+
label="Ground Truth Answer",
|
| 272 |
+
placeholder="Enter the correct answer here...",
|
| 273 |
+
elem_classes="input-field"
|
| 274 |
+
)
|
| 275 |
+
|
| 276 |
+
with gr.Column(scale=1):
|
| 277 |
+
model_answer = gr.Textbox(
|
| 278 |
+
lines=3,
|
| 279 |
+
label="Model Answer",
|
| 280 |
+
placeholder="Enter the answer to verify here...",
|
| 281 |
+
elem_classes="input-field"
|
| 282 |
+
)
|
| 283 |
+
|
| 284 |
+
verify_btn = gr.Button("Verify Answer", elem_classes="verify-btn")
|
| 285 |
|
| 286 |
+
# Right column - Result
|
| 287 |
+
with gr.Column(scale=2):
|
| 288 |
+
with gr.Box(elem_classes="panel"):
|
| 289 |
+
gr.Markdown("### Verification Result", elem_classes="label")
|
| 290 |
+
result = gr.Textbox(
|
| 291 |
+
placeholder="The verification result will appear here...",
|
| 292 |
+
lines=10,
|
| 293 |
+
elem_classes="result-panel"
|
| 294 |
+
)
|
| 295 |
|
| 296 |
# Examples section
|
| 297 |
+
gr.Markdown("### Examples", elem_classes="label")
|
| 298 |
+
with gr.Box(elem_classes="examples-container"):
|
| 299 |
for i, ex in enumerate(EXAMPLES):
|
| 300 |
+
btn = gr.Button(ex["name"], elem_classes="example-btn")
|
| 301 |
btn.click(
|
| 302 |
fn=lambda idx=i: load_example(idx),
|
| 303 |
outputs=[question, ground_truth, model_answer, temperature, top_p, max_tokens]
|
|
|
|
| 306 |
btn.click(
|
| 307 |
fn=verify_answer,
|
| 308 |
inputs=[question, ground_truth, model_answer, temperature, top_p, max_tokens],
|
| 309 |
+
outputs=result
|
|
|
|
| 310 |
)
|
| 311 |
|
| 312 |
+
# Advanced Settings in accordion
|
| 313 |
+
with gr.Accordion("Advanced Settings", open=False, elem_classes="accordions-container"):
|
| 314 |
with gr.Row():
|
| 315 |
temp_slider = gr.Slider(0, 1, value=0.3, step=0.1, label="Temperature")
|
| 316 |
top_p_slider = gr.Slider(0.1, 1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)")
|
|
|
|
| 321 |
top_p_slider.change(lambda x: x, inputs=[top_p_slider], outputs=[top_p])
|
| 322 |
max_tokens_slider.change(lambda x: x, inputs=[max_tokens_slider], outputs=[max_tokens])
|
| 323 |
|
| 324 |
+
# API usage in accordion
|
| 325 |
+
with gr.Accordion("API Usage", open=False, elem_classes="accordions-container"):
|
| 326 |
gr.Markdown(
|
| 327 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 328 |
```python
|
| 329 |
from gradio_client import Client
|
| 330 |
|
|
|
|
| 335 |
model_answer="Paris is the capital of France.",
|
| 336 |
temperature=0.3,
|
| 337 |
top_p=0.95,
|
| 338 |
+
max_tokens=1,
|
| 339 |
api_name="/verify_answer"
|
| 340 |
)
|
| 341 |
print(result)
|
| 342 |
```
|
| 343 |
"""
|
| 344 |
)
|
| 345 |
+
|
| 346 |
+
# Footer
|
| 347 |
+
gr.Markdown(
|
| 348 |
+
"""
|
| 349 |
+
Powered by TinyV-1.5B model. This tool verifies semantic equivalence between answers.
|
| 350 |
+
""",
|
| 351 |
+
elem_classes="footer"
|
| 352 |
+
)
|
| 353 |
|
| 354 |
# Connect the interface to the verification function
|
| 355 |
verify_btn.click(
|