File size: 1,791 Bytes
5fe2042
 
 
 
 
87f281a
 
 
5fe2042
 
 
 
 
87f281a
 
5fe2042
bbf81ab
6008bf2
bbf81ab
5fe2042
 
 
87f281a
5fe2042
 
 
 
 
 
 
 
 
87f281a
 
 
 
 
 
 
 
 
5fe2042
87f281a
 
5fe2042
87f281a
6008bf2
 
 
 
87f281a
 
 
 
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import falcon
from engine import evaluate_solution
from threading import Lock
from typing import Dict
import logging


class RootResource:
    def __init__(self):
        self.main_lock = Lock()
        self.task_locks: Dict[str, Lock] = {}
        self.max_tasks = 5

    def on_get(self, request, response):
        response.text = 'Human Eval for Solidity Server v1.2410.0'

    def on_post(self, request, response):
        payload = request.media

        if 'task_id' not in payload or 'solution' not in payload:
            response.status = falcon.HTTP_400
            response.media = {
                'error': 'task_id or solution are missing',
            }
            return

        task_id = payload['task_id']
        solution = payload['solution']

        try:
            with self.main_lock:
                if len(self.task_locks) >= self.max_tasks:
                    # Remove the oldest task lock if we've reached the limit
                    oldest_task = next(iter(self.task_locks))
                    del self.task_locks[oldest_task]

                if task_id not in self.task_locks:
                    self.task_locks[task_id] = Lock()

            with self.task_locks[task_id]:
                passed = evaluate_solution(task_id, solution)

            response.media = {'passed': passed}
        except FileNotFoundError as e:
            logging.error('Task not found: {}'.format(str(e)))
            response.status = falcon.HTTP_404
            response.media = {'error': 'Task not found'}
        except Exception as e:
            logging.error('Error processing request: {}'.format(str(e)))
            response.status = falcon.HTTP_500
            response.media = {'error': 'Internal server error'}