File size: 7,756 Bytes
bc37111
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Submit Tab Component

Model evaluation submission with HuggingFace authentication.
"""

import logging
import re
from typing import Optional, Tuple
import gradio as gr

from ..api import EvaluationApiClient

logger = logging.getLogger(__name__)


class SubmitTab:
    """
    Submit evaluation tab component.
    
    Provides:
    - HuggingFace OAuth login
    - Model submission form
    - Email notification setup
    """
    
    def __init__(self):
        self.api_client = EvaluationApiClient()
        
        # UI components (will be set during build)
        self.model_input: Optional[gr.Textbox] = None
        self.email_input: Optional[gr.Textbox] = None
        self.submit_btn: Optional[gr.Button] = None
        self.login_button: Optional[gr.LoginButton] = None
        self.result_output: Optional[gr.HTML] = None
    
    def _validate_model_name(self, model_name: str) -> Optional[str]:
        """Validate model name format."""
        if not model_name or not model_name.strip():
            return "Model name cannot be empty!"
        
        model_name = model_name.strip()
        
        if len(model_name) < 3:
            return "Model name too short!"
        
        if len(model_name) > 256:
            return "Model name too long (maximum 256 characters)!"
        
        if '/' not in model_name:
            return "Invalid format! Must include organization (e.g., organization/model-name)"
        
        if not re.match(r'^[a-zA-Z0-9._-]+/[a-zA-Z0-9._-]+$', model_name):
            return "Invalid format! Use format: organization/model-name"
        
        return None
    
    def _validate_email(self, email: str) -> Optional[str]:
        """Validate email format."""
        if not email or not email.strip():
            return "Email address cannot be empty!"
        
        email = email.strip()
        
        if len(email) > 254:
            return "Email address too long!"
        
        email_pattern = r'^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$'
        if not re.match(email_pattern, email):
            return "Invalid email address format!"
        
        return None
    
    def _handle_submit(self, model_name: str, email: str, profile) -> str:
        """Handle evaluation submission."""
        # Authentication check
        if profile is None:
            return "<p style='color: red; font-weight: bold;'>⚠️ Authentication required. Please log in with your Hugging Face account.</p>"
        
        # Check for local dev mock auth
        if isinstance(profile, str) and profile == "Sign in with Hugging Face":
            return "<p style='color: orange; font-weight: bold;'>⚠️ HF authentication required.</p>"
        
        # Validate model name
        model_error = self._validate_model_name(model_name)
        if model_error:
            return f"<p style='color: red; font-weight: bold;'>❌ {model_error}</p>"
        
        # Validate email
        email_error = self._validate_email(email)
        if email_error:
            return f"<p style='color: red; font-weight: bold;'>❌ {email_error}</p>"
        
        # Submit to API
        model_name = model_name.strip()
        email = email.strip()
        
        try:
            success = self.api_client.submit_evaluation(model_name, email)
            
            if success:
                return f"""
                <div style='padding: 16px; background: #d4edda; border-radius: 8px; border: 1px solid #c3e6cb; color: #155724;'>
                    <h3 style='color: #155724; margin: 0 0 12px 0;'>✅ Evaluation Request Submitted!</h3>
                    <p style='color: #155724; margin: 4px 0;'><strong style='color: #155724;'>Model:</strong> {model_name}</p>
                    <p style='color: #155724; margin: 4px 0;'><strong style='color: #155724;'>Email:</strong> {email}</p>
                    <hr style='margin: 12px 0; border-color: #c3e6cb;'>
                    <p style='color: #155724; margin: 4px 0;'><strong style='color: #155724;'>Next Steps:</strong></p>
                    <ul style='color: #155724; margin: 8px 0; padding-left: 20px;'>
                        <li style='color: #155724;'>Your request will be reviewed by our system</li>
                        <li style='color: #155724;'>You will receive email notifications about the status</li>
                        <li style='color: #155724;'>Results will appear on the leaderboard when complete</li>
                    </ul>
                    <p style='color: #155724; margin-top: 12px; font-style: italic;'>Thank you for contributing to the Mizan Leaderboard!</p>
                </div>
                """
            else:
                return """
                <div style='padding: 16px; background: #f8d7da; border-radius: 8px; border: 1px solid #f5c6cb;'>
                    <h3 style='color: #721c24; margin: 0 0 8px 0;'>❌ Submission Failed</h3>
                    <p>Unable to connect to the evaluation service. Please try again later.</p>
                </div>
                """
        except Exception as e:
            logger.error(f"Error submitting evaluation: {e}")
            return f"""
            <div style='padding: 16px; background: #f8d7da; border-radius: 8px; border: 1px solid #f5c6cb;'>
                <h3 style='color: #721c24; margin: 0 0 8px 0;'>❌ Error</h3>
                <p>An unexpected error occurred. Please try again later.</p>
            </div>
            """
    
    def build(self) -> None:
        """Build the submit tab UI."""
        gr.Markdown("### Submit Model for Evaluation")
        gr.Markdown("""
        Submit your Turkish embedding model for evaluation on the Mizan benchmark.
        **Authentication with Hugging Face is required to submit evaluations.**
        """)
        
        # OAuth login button
        self.login_button = gr.LoginButton(value="Sign in with Hugging Face")
        
        self.model_input = gr.Textbox(
            label="Model Name",
            placeholder="sentence-transformers/your-model",
            info="HuggingFace model identifier (e.g., sentence-transformers/your-model-name)"
        )
        
        self.email_input = gr.Textbox(
            label="Email Address",
            placeholder="your.email@example.com",
            info="Email for notifications about evaluation status and results"
        )
        
        self.submit_btn = gr.Button(
            "Submit", 
            variant="primary",
            size="lg"
        )
        
        # Result output
        self.result_output = gr.HTML(label="Status")
        
        # Wire up submit button
        self.submit_btn.click(
            fn=self._handle_submit,
            inputs=[self.model_input, self.email_input, self.login_button],
            outputs=[self.result_output]
        )
        
        # Information about the evaluation process
        gr.Markdown("""
        ### Evaluation Process:
        1. **Sign In**: First, sign in with your Hugging Face account using the button above
        2. **Submit Request**: Fill out the form with your model details and email
        3. **Admin Review**: Your request will be reviewed by administrators
        4. **Evaluation**: If approved, your model will be evaluated on Mizan benchmark
        5. **Results**: You'll receive email notifications and results will appear on the leaderboard
        
        ### Important Notes:
        - **Authentication Required**: You must be logged in with Hugging Face to submit evaluations
        - You'll receive email updates about your request status
        - Make sure your model is publicly available on HuggingFace
        - Valid email address is required for receiving results
        """)