File size: 10,960 Bytes
ccf0d16
 
 
9682c5f
ccf0d16
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9682c5f
ccf0d16
 
7fe5217
 
 
 
 
 
 
 
565bae4
7fe5217
ccf0d16
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7fe5217
 
 
 
 
 
 
 
 
 
 
 
 
 
ccf0d16
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9682c5f
ccf0d16
 
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
#!/usr/bin/env python3

import os
import gradio as gr
from datasets import load_dataset
from dataclasses import dataclass
from typing import Any

# HF token should be set as environment variable HF_TOKEN

@dataclass
class DatasetViewer:
    dataset: Any = None
    current_index: int = 0
    total_items: int = 0
    show_answer: bool = False
    
    def load_dataset(self, dataset_name: str = "yourbench/reachy_mini_info_benchmark", 
                    subset: str = "prepared_lighteval"):
        try:
            self.dataset = load_dataset(dataset_name, subset)
            
            # Try to get the first available split
            if isinstance(self.dataset, dict):
                first_split = next(iter(self.dataset.keys()))
                self.dataset = self.dataset[first_split]
            
            self.total_items = len(self.dataset)
            self.current_index = 0
            self.show_answer = False
            return True
        except Exception as e:
            print(f"Error loading dataset: {e}")
            # Create mock data for demonstration
            self.create_mock_data()
            return False
    
    def create_mock_data(self):
        """Create mock data that simulates yourbench dataset structure"""
        mock_data = [
            {
                "question": "What is the capital of France?",
                "ground_truth_answer": "Paris",
                "document_summary": "France is a country in Western Europe.",
                "question_category": "geography",
                "estimated_difficulty": 2,
                "kind": "single_shot",
                "additional_instructions": "Ask generalizable questions."
            },
            {
                "question": "Explain the concept of object-oriented programming.",
                "ground_truth_answer": "Object-oriented programming (OOP) is a programming paradigm based on the concept of 'objects', which can contain data and code: data in the form of fields, and code in the form of procedures. Key principles include encapsulation, inheritance, and polymorphism.",
                "document_summary": "Programming concepts",
                "question_category": "computer_science",
                "estimated_difficulty": 4,
                "kind": "single_shot",
                "additional_instructions": "Provide detailed explanations."
            },
        ]
        
        # Create a mock dataset-like structure
        class MockDataset:
            def __init__(self, data):
                self.data = data
            
            def __len__(self):
                return len(self.data)
            
            def __getitem__(self, idx):
                return self.data[idx]
        
        self.dataset = MockDataset(mock_data)
        self.total_items = len(mock_data)
        self.current_index = 0
        self.show_answer = False

viewer = DatasetViewer()
viewer.load_dataset()

def get_current_item():
    if viewer.dataset is None or viewer.total_items == 0:
        return {"question": "No data available", "answer": "", "context": "", "task": "", "difficulty": ""}
    
    item = viewer.dataset[viewer.current_index]
    
    # Map actual dataset fields to expected interface fields
    mapped_item = {
        "question": item.get("question", "No question available"),
        "answer": item.get("ground_truth_answer", "No answer available"),
        "context": item.get("document_summary", "No context available"),
        "task": item.get("question_category", "Unknown category"),
        "difficulty": str(item.get("estimated_difficulty", "Unknown")),
        "additional_instructions": item.get("additional_instructions", ""),
        "kind": item.get("kind", ""),
        "citations": item.get("citations", []),
        "document_id": item.get("document_id", ""),
    }
    
    return mapped_item

def get_card_content():
    item = get_current_item()
    
    if viewer.show_answer:
        # Show answer side
        content = f"""
        <div style="text-align: center; padding: 40px; min-height: 400px; display: flex; flex-direction: column; justify-content: center;">
            <h1 style="color: #2d5aa0; font-size: 2.5em; margin-bottom: 30px;">Answer</h1>
            <div style="font-size: 1.4em; line-height: 1.6; color: #333; max-width: 800px; margin: 0 auto;">
                {item['answer']}
            </div>
        </div>
        """
        button_text = "Show Question"
    else:
        # Show question side
        content = f"""
        <div style="text-align: center; padding: 40px; min-height: 400px; display: flex; flex-direction: column; justify-content: center;">
            <h1 style="color: #2d5aa0; font-size: 2.5em; margin-bottom: 30px;">Question</h1>
            <div style="font-size: 1.4em; line-height: 1.6; color: #333; max-width: 800px; margin: 0 auto;">
                {item['question']}
            </div>
        </div>
        """
        button_text = "Show Answer"
    
    return content, button_text

def get_metadata():
    item = get_current_item()
    
    context = item.get('context', 'No context')
    task = item.get('task', 'Unknown task')
    difficulty = item.get('difficulty', 'Unknown')
    kind = item.get('kind', 'Unknown')
    additional_instructions = item.get('additional_instructions', '')
    
    metadata = f"Category: {task} | Difficulty: {difficulty}/5 | Type: {kind}"
    if additional_instructions:
        metadata += f" | Instructions: {additional_instructions}"
    
    # Truncate context if too long
    if len(context) > 150:
        context = context[:150] + "..."
    
    return metadata, context

def toggle_answer():
    viewer.show_answer = not viewer.show_answer
    content, button_text = get_card_content()
    return content, button_text

def next_item():
    if viewer.total_items > 0:
        viewer.current_index = (viewer.current_index + 1) % viewer.total_items
        viewer.show_answer = False
    
    content, button_text = get_card_content()
    metadata, context = get_metadata()
    progress = f"{viewer.current_index + 1} of {viewer.total_items}"
    
    return content, button_text, metadata, context, progress

def previous_item():
    if viewer.total_items > 0:
        viewer.current_index = (viewer.current_index - 1) % viewer.total_items
        viewer.show_answer = False
    
    content, button_text = get_card_content()
    metadata, context = get_metadata()
    progress = f"{viewer.current_index + 1} of {viewer.total_items}"
    
    return content, button_text, metadata, context, progress

def jump_to_item(item_number):
    if viewer.total_items > 0 and 1 <= item_number <= viewer.total_items:
        viewer.current_index = item_number - 1
        viewer.show_answer = False
    
    content, button_text = get_card_content()
    metadata, context = get_metadata()
    progress = f"{viewer.current_index + 1} of {viewer.total_items}"
    
    return content, button_text, metadata, context, progress

# Initialize
initial_content, initial_button_text = get_card_content()
initial_metadata, initial_context = get_metadata()
initial_progress = f"{viewer.current_index + 1} of {viewer.total_items}"

# Custom CSS for flashcard styling
css = """
.flashcard {
    border: 2px solid #ddd;
    border-radius: 15px;
    box-shadow: 0 4px 8px rgba(0,0,0,0.1);
    background: white;
    margin: 20px 0;
    min-height: 500px;
}

.controls {
    text-align: center;
    padding: 20px;
}

.metadata {
    background: #f8f9fa;
    padding: 15px;
    border-radius: 8px;
    margin: 10px 0;
    font-size: 0.9em;
    color: #666;
}

.context {
    background: #fff3cd;
    padding: 15px;
    border-radius: 8px;
    margin: 10px 0;
    font-size: 0.9em;
    color: #856404;
    border: 1px solid #ffeaa7;
}

.progress {
    text-align: center;
    font-size: 1.2em;
    font-weight: bold;
    color: #2d5aa0;
    margin: 20px 0;
}
"""

# Create Gradio interface
with gr.Blocks(css=css, title="Reachy Trivia") as demo:
    gr.Markdown("# πŸ€– Reachy Trivia")
    gr.Markdown("""
    **Test your knowledge about Reachy Mini and Reachy 2!**
    
    Reachy Mini is the $299 open-source desktop robot designed for AI developers, educators, and hobbyists. 
    With its expressive antennas, screen-based eyes, and full Python programmability, it's the perfect companion 
    for exploring human-robot interaction and AI development.
    
    *Questions generated by Yourbench - HuggingFace's Dynamic Evaluation Generation Platform*
    """)
    
    # Main flashcard area
    with gr.Row():
        with gr.Column(scale=1):
            flashcard = gr.HTML(initial_content, elem_classes=["flashcard"])
            
            # Controls
            with gr.Row(elem_classes=["controls"]):
                prev_btn = gr.Button("Previous", size="lg", variant="secondary")
                toggle_btn = gr.Button(initial_button_text, size="lg", variant="primary")
                next_btn = gr.Button("Next", size="lg", variant="secondary")
            
            # Progress
            progress_display = gr.HTML(f'<div class="progress">{initial_progress}</div>')
    
    # Metadata and navigation sidebar
    with gr.Row():
        with gr.Column(scale=2):
            metadata_display = gr.HTML(f'<div class="metadata">{initial_metadata}</div>')
            context_display = gr.HTML(f'<div class="context">Context: {initial_context}</div>')
        
        with gr.Column(scale=1):
            gr.Markdown("### Jump to Item")
            jump_input = gr.Number(
                label="Item Number",
                value=1,
                minimum=1,
                maximum=viewer.total_items,
                precision=0
            )
            jump_btn = gr.Button("Jump", variant="secondary")
            
            gr.Markdown("---")
            gr.Markdown("### About Reachy Mini")
            gr.Markdown("""
            **Key Features:**
            - 🎯 $299 open-source desktop robot
            - πŸ“± Expressive screen-based eyes
            - πŸ”§ Full Python programmability  
            - 🀝 Human-robot interaction focus
            - 🧠 Hugging Face AI integration
            - πŸ“¦ DIY assembly kit
            """)
            
            gr.Markdown("*Questions powered by [YouBench](https://huggingface.co/yourbench)*")
    
    # Event handlers
    toggle_btn.click(
        toggle_answer,
        outputs=[flashcard, toggle_btn]
    )
    
    next_btn.click(
        next_item,
        outputs=[flashcard, toggle_btn, metadata_display, context_display, progress_display]
    )
    
    prev_btn.click(
        previous_item,
        outputs=[flashcard, toggle_btn, metadata_display, context_display, progress_display]
    )
    
    jump_btn.click(
        jump_to_item,
        inputs=[jump_input],
        outputs=[flashcard, toggle_btn, metadata_display, context_display, progress_display]
    )

if __name__ == "__main__":
    demo.launch()