File size: 3,029 Bytes
7ecd52b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import io
import sys
import logging
import multiprocessing
import os
import pickle
import threading
import time
from collections import Counter, defaultdict
from concurrent.futures import ProcessPoolExecutor, as_completed, wait, FIRST_COMPLETED
from datetime import datetime
from typing import Any, Dict, List, Tuple
from warnings import warn
from contextlib import redirect_stdout, redirect_stderr

import numpy as np
from huggingface_hub import HfApi
from bigcodebench.data.utils import CACHE_DIR
from bigcodebench.eval import PASS, compatible_eval_result, estimate_pass_at_k, untrusted_check
from bigcodebench.gen.util import trusted_check
from apscheduler.schedulers.background import BackgroundScheduler

REPO_ID = "bigcode/bigcodebench-interaction"
HF_TOKEN = os.environ.get("HF_TOKEN", None)
API = HfApi(token=HF_TOKEN)
Result = Tuple[str, List[bool]]


def run_code(code: str) -> str:
    # Create string buffers to capture output
    stdout_buffer = io.StringIO()
    stderr_buffer = io.StringIO()
    
    # Create a dictionary for local variables
    local_dict = {}
    
    # Capture both stdout and stderr
    with redirect_stdout(stdout_buffer), redirect_stderr(stderr_buffer):
        try:
            # Execute the code
            exec(code, globals(), local_dict)
            
            # Get the output
            output = stdout_buffer.getvalue()
            errors = stderr_buffer.getvalue()
            
            # If there's a return value in the last expression, capture it
            last_line = code.strip().split('\n')[-1]
            if not (last_line.startswith('print') or last_line.strip() == ''):
                try:
                    result = eval(last_line, globals(), local_dict)
                    if result is not None:
                        output += f"\n>>> {result}"
                except:
                    pass
            
            # Combine stdout and stderr
            result = output
            if errors:
                result += "\n--- Errors ---\n" + errors
                
        except Exception as e:
            # Capture any execution errors
            result = f"Error: {str(e)}"
    
    return result if result.strip() else "Code executed successfully (no output)"

# Create the Gradio interface with better styling
interface = gr.Interface(
    fn=run_code,
    inputs=[
        gr.Code(label="Python Code", language="python"),
    ],
    outputs=[
        gr.Textbox(label="Output")
    ],
)
interface.queue(default_concurrency_limit=None)


def restart_space():
    logging.info(f"Restarting space with repo ID: {REPO_ID}")
    try:
        # Now restart the space
        API.restart_space(repo_id=REPO_ID, token=HF_TOKEN)
        logging.info("Space restarted successfully.")
    except Exception as e:
        logging.error(f"Failed to restart space: {e}")


scheduler = BackgroundScheduler()
scheduler.add_job(restart_space, "interval", hours=5)  # Restart every 5hs
scheduler.start()
interface.launch(show_error=True)