-
Notifications
You must be signed in to change notification settings - Fork 83
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: nik <[email protected]>
- Loading branch information
Showing
8 changed files
with
428 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
from .base import Environment, StaticEnvironment | ||
from .console import ConsoleEnvironment | ||
from .web import WebStaticEnvironment | ||
from .web import WebStaticEnvironment | ||
from .code import SimpleCodeValidationEnvironment |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import sys | ||
import io | ||
from contextlib import redirect_stdout, redirect_stderr | ||
from typing import Dict, Optional, List | ||
from .base import StaticEnvironment, EnvironmentFeedback | ||
from adala.skills import SkillSet | ||
from adala.utils.internal_data import InternalDataFrame | ||
|
||
|
||
class redirect_stdin: | ||
"""Context manager for redirecting stdin to a given StringIO object.""" | ||
def __init__(self, new_target): | ||
self.new_target = new_target | ||
self._original_stdin = sys.stdin | ||
|
||
def __enter__(self): | ||
sys.stdin = self.new_target | ||
|
||
def __exit__(self, exc_type, exc_value, traceback): | ||
sys.stdin = self._original_stdin | ||
|
||
|
||
class SimpleCodeValidationEnvironment(StaticEnvironment): | ||
|
||
code_template: str = None | ||
code_fields: Dict[str, str] = None | ||
|
||
def execute_code(self, code: str, input_string) -> Dict: | ||
out = {'success': False} | ||
stdout = io.StringIO() | ||
stderr = io.StringIO() | ||
stdin = io.StringIO(input_string) | ||
|
||
try: | ||
with redirect_stdin(stdin), redirect_stdout(stdout), redirect_stderr(stderr): | ||
exec(code, {"__builtins__": __builtins__}) | ||
out['success'] = True | ||
except Exception as e: | ||
stderr.write(str(e)) | ||
|
||
out['stdout'] = stdout.getvalue() | ||
out['stderr'] = stderr.getvalue() | ||
return out | ||
|
||
def get_feedback( | ||
self, | ||
skills: SkillSet, | ||
predictions: InternalDataFrame, | ||
num_feedbacks: Optional[int] = None, | ||
) -> EnvironmentFeedback: | ||
|
||
if num_feedbacks: | ||
predictions = predictions.sample(n=num_feedbacks) | ||
|
||
code_match, code_feedback = {}, {} | ||
for code_field, code_input in self.code_fields.items(): | ||
match, feedback = [], [] | ||
for data_row in predictions.to_dict(orient='records'): | ||
res = self.execute_code(data_row[code_field], data_row[code_input]) | ||
if res['success']: | ||
match.append(True) | ||
feedback.append(f'Code is valid. Output:\n{res["stdout"]}') | ||
else: | ||
match.append(False) | ||
feedback.append(f'Code is invalid:\n{res["stderr"]}') | ||
|
||
code_match[code_field] = match | ||
code_feedback[code_field] = feedback | ||
|
||
return EnvironmentFeedback( | ||
match=InternalDataFrame(code_match, index=predictions.index), | ||
feedback=InternalDataFrame(code_feedback, index=predictions.index) | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.