Spaces:
Running
Running
Upload folder using huggingface_hub
Browse files- inference.py +24 -4
- server/app.py +38 -0
- server/rust_coder_environment.py +8 -0
inference.py
CHANGED
|
@@ -2,12 +2,21 @@ import os
|
|
| 2 |
import re
|
| 3 |
import json
|
| 4 |
import asyncio
|
|
|
|
| 5 |
from typing import List, Optional
|
| 6 |
from openai import OpenAI
|
| 7 |
from dotenv import load_dotenv
|
| 8 |
|
| 9 |
load_dotenv()
|
| 10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
# --- Competition Configuration ---
|
| 12 |
API_BASE_URL = os.getenv("API_BASE_URL") or "https://router.huggingface.co/v1"
|
| 13 |
MODEL_NAME = os.getenv("MODEL_NAME") or "Qwen/Qwen2.5-72B-Instruct"
|
|
@@ -41,6 +50,13 @@ def log_end(success: bool, steps: int, score: float, rewards: List[float]):
|
|
| 41 |
async def get_model_code(prompt: str, client: OpenAI) -> str:
|
| 42 |
"""Call the LLM to get a Rust solution."""
|
| 43 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
completion = client.chat.completions.create(
|
| 45 |
model=MODEL_NAME,
|
| 46 |
messages=[
|
|
@@ -50,6 +66,7 @@ async def get_model_code(prompt: str, client: OpenAI) -> str:
|
|
| 50 |
temperature=0.1,
|
| 51 |
)
|
| 52 |
text = (completion.choices[0].message.content or "").strip()
|
|
|
|
| 53 |
|
| 54 |
# Extract code from markdown
|
| 55 |
if "```rust" in text:
|
|
@@ -58,16 +75,18 @@ async def get_model_code(prompt: str, client: OpenAI) -> str:
|
|
| 58 |
text = text.split("```")[1].split("```")[0]
|
| 59 |
text = text.strip()
|
| 60 |
if not text:
|
|
|
|
| 61 |
return "// Error: empty response (no code returned)."
|
|
|
|
| 62 |
return text
|
| 63 |
except Exception as e:
|
| 64 |
-
|
| 65 |
return f"// Error: {e}"
|
| 66 |
|
| 67 |
# --- Main Evaluation Loop ---
|
| 68 |
async def main():
|
| 69 |
if not HF_TOKEN:
|
| 70 |
-
|
| 71 |
return
|
| 72 |
|
| 73 |
client = OpenAI(base_url=API_BASE_URL, api_key=HF_TOKEN)
|
|
@@ -100,6 +119,7 @@ async def main():
|
|
| 100 |
code_solution = await get_model_code(prompt, client)
|
| 101 |
|
| 102 |
# 2. Environment step
|
|
|
|
| 103 |
result = await env.step(RustCoderAction(code=code_solution))
|
| 104 |
obs = result.observation
|
| 105 |
reward = result.reward or 0.0
|
|
@@ -117,14 +137,14 @@ async def main():
|
|
| 117 |
success = score >= SUCCESS_SCORE_THRESHOLD
|
| 118 |
|
| 119 |
except Exception as e:
|
| 120 |
-
|
| 121 |
log_step(step=steps_taken + 1, action="error", reward=0.0, done=True, error=str(e))
|
| 122 |
|
| 123 |
finally:
|
| 124 |
try:
|
| 125 |
await env.close()
|
| 126 |
except Exception as e:
|
| 127 |
-
|
| 128 |
log_end(success=success, steps=steps_taken, score=score, rewards=rewards)
|
| 129 |
|
| 130 |
if __name__ == "__main__":
|
|
|
|
| 2 |
import re
|
| 3 |
import json
|
| 4 |
import asyncio
|
| 5 |
+
import logging
|
| 6 |
from typing import List, Optional
|
| 7 |
from openai import OpenAI
|
| 8 |
from dotenv import load_dotenv
|
| 9 |
|
| 10 |
load_dotenv()
|
| 11 |
|
| 12 |
+
# --- Logging (inference.py) ---
|
| 13 |
+
_LOG_LEVEL = (os.getenv("LOG_LEVEL") or "INFO").upper()
|
| 14 |
+
logging.basicConfig(
|
| 15 |
+
level=getattr(logging, _LOG_LEVEL, logging.INFO),
|
| 16 |
+
format="%(asctime)s %(levelname)s %(name)s - %(message)s",
|
| 17 |
+
)
|
| 18 |
+
logger = logging.getLogger("rust_coder.inference")
|
| 19 |
+
|
| 20 |
# --- Competition Configuration ---
|
| 21 |
API_BASE_URL = os.getenv("API_BASE_URL") or "https://router.huggingface.co/v1"
|
| 22 |
MODEL_NAME = os.getenv("MODEL_NAME") or "Qwen/Qwen2.5-72B-Instruct"
|
|
|
|
| 50 |
async def get_model_code(prompt: str, client: OpenAI) -> str:
|
| 51 |
"""Call the LLM to get a Rust solution."""
|
| 52 |
try:
|
| 53 |
+
logger.info(
|
| 54 |
+
"LLM call start model=%s base_url=%s prompt_chars=%d token_present=%s",
|
| 55 |
+
MODEL_NAME,
|
| 56 |
+
API_BASE_URL,
|
| 57 |
+
len(prompt or ""),
|
| 58 |
+
bool(HF_TOKEN),
|
| 59 |
+
)
|
| 60 |
completion = client.chat.completions.create(
|
| 61 |
model=MODEL_NAME,
|
| 62 |
messages=[
|
|
|
|
| 66 |
temperature=0.1,
|
| 67 |
)
|
| 68 |
text = (completion.choices[0].message.content or "").strip()
|
| 69 |
+
logger.debug("LLM raw response chars=%d", len(text))
|
| 70 |
|
| 71 |
# Extract code from markdown
|
| 72 |
if "```rust" in text:
|
|
|
|
| 75 |
text = text.split("```")[1].split("```")[0]
|
| 76 |
text = text.strip()
|
| 77 |
if not text:
|
| 78 |
+
logger.warning("LLM returned empty code after cleanup.")
|
| 79 |
return "// Error: empty response (no code returned)."
|
| 80 |
+
logger.info("LLM call end: returned_code_chars=%d", len(text))
|
| 81 |
return text
|
| 82 |
except Exception as e:
|
| 83 |
+
logger.exception("LLM Request failed.")
|
| 84 |
return f"// Error: {e}"
|
| 85 |
|
| 86 |
# --- Main Evaluation Loop ---
|
| 87 |
async def main():
|
| 88 |
if not HF_TOKEN:
|
| 89 |
+
logger.error("HF_TOKEN/API_KEY not found in environment.")
|
| 90 |
return
|
| 91 |
|
| 92 |
client = OpenAI(base_url=API_BASE_URL, api_key=HF_TOKEN)
|
|
|
|
| 119 |
code_solution = await get_model_code(prompt, client)
|
| 120 |
|
| 121 |
# 2. Environment step
|
| 122 |
+
logger.debug("Submitting to env.step code_chars=%d", len(code_solution or ""))
|
| 123 |
result = await env.step(RustCoderAction(code=code_solution))
|
| 124 |
obs = result.observation
|
| 125 |
reward = result.reward or 0.0
|
|
|
|
| 137 |
success = score >= SUCCESS_SCORE_THRESHOLD
|
| 138 |
|
| 139 |
except Exception as e:
|
| 140 |
+
logger.exception("Runtime error.")
|
| 141 |
log_step(step=steps_taken + 1, action="error", reward=0.0, done=True, error=str(e))
|
| 142 |
|
| 143 |
finally:
|
| 144 |
try:
|
| 145 |
await env.close()
|
| 146 |
except Exception as e:
|
| 147 |
+
logger.exception("env.close() error.")
|
| 148 |
log_end(success=success, steps=steps_taken, score=score, rewards=rewards)
|
| 149 |
|
| 150 |
if __name__ == "__main__":
|
server/app.py
CHANGED
|
@@ -10,6 +10,7 @@ Endpoints:
|
|
| 10 |
"""
|
| 11 |
|
| 12 |
import os
|
|
|
|
| 13 |
import gradio as gr
|
| 14 |
from openai import OpenAI
|
| 15 |
from dotenv import load_dotenv
|
|
@@ -20,6 +21,14 @@ from server.rust_coder_environment import RustCoderEnvironment
|
|
| 20 |
|
| 21 |
load_dotenv()
|
| 22 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
# --- Core OpenEnv Server Setup ---
|
| 24 |
# Use a distinct name for the OpenEnv FastAPI instance
|
| 25 |
openenv_app = create_app(
|
|
@@ -43,6 +52,13 @@ HF_TOKEN = os.getenv("HF_TOKEN") or os.getenv("API_KEY")
|
|
| 43 |
def get_llm_solution(problem_desc: str):
|
| 44 |
"""Call LLM to get a Rust solution"""
|
| 45 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
client_llm = OpenAI(base_url=API_BASE_URL, api_key=HF_TOKEN)
|
| 47 |
completion = client_llm.chat.completions.create(
|
| 48 |
model=MODEL_NAME,
|
|
@@ -53,6 +69,7 @@ def get_llm_solution(problem_desc: str):
|
|
| 53 |
temperature=0.2,
|
| 54 |
)
|
| 55 |
text = (completion.choices[0].message.content or "").strip()
|
|
|
|
| 56 |
# Clean markdown code blocks
|
| 57 |
if "```rust" in text:
|
| 58 |
text = text.split("```rust")[1].split("```")[0]
|
|
@@ -60,9 +77,12 @@ def get_llm_solution(problem_desc: str):
|
|
| 60 |
text = text.split("```")[1].split("```")[0]
|
| 61 |
text = text.strip()
|
| 62 |
if not text:
|
|
|
|
| 63 |
return "// LLM Error: empty response (no code returned)."
|
|
|
|
| 64 |
return text
|
| 65 |
except Exception as e:
|
|
|
|
| 66 |
return f"// LLM Error: {e}"
|
| 67 |
|
| 68 |
def evaluate_single(problem_id, code=None):
|
|
@@ -70,19 +90,36 @@ def evaluate_single(problem_id, code=None):
|
|
| 70 |
try:
|
| 71 |
idx = int(problem_id.split(":")[0]) - 1
|
| 72 |
problem = RustCoderEnvironment().problems[idx]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
|
| 74 |
# 1. Get code from LLM if not provided
|
| 75 |
solution_code = code if code else get_llm_solution(problem["description"])
|
| 76 |
|
| 77 |
# 2. Guard: If LLM failed, do not evaluate
|
| 78 |
if not solution_code.strip() or solution_code.startswith("// LLM Error"):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
return solution_code, {"error": "LLM failed to generate a solution. Check your HF_TOKEN."}
|
| 80 |
|
| 81 |
# 3. Evaluate properly
|
| 82 |
env = RustCoderEnvironment()
|
| 83 |
# Reset to the specifically requested index
|
| 84 |
state = env.reset(start_index=idx)
|
|
|
|
| 85 |
state = env.step(RustCoderAction(code=solution_code))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
|
| 87 |
metrics = {
|
| 88 |
"Total Reward": f"{state.reward:.2f}",
|
|
@@ -91,6 +128,7 @@ def evaluate_single(problem_id, code=None):
|
|
| 91 |
}
|
| 92 |
return solution_code, metrics
|
| 93 |
except Exception as e:
|
|
|
|
| 94 |
return f"// Error: {e}", {"error": f"Evaluation system error: {e}"}
|
| 95 |
|
| 96 |
def run_benchmark(progress=gr.Progress()):
|
|
|
|
| 10 |
"""
|
| 11 |
|
| 12 |
import os
|
| 13 |
+
import logging
|
| 14 |
import gradio as gr
|
| 15 |
from openai import OpenAI
|
| 16 |
from dotenv import load_dotenv
|
|
|
|
| 21 |
|
| 22 |
load_dotenv()
|
| 23 |
|
| 24 |
+
# --- Logging (server/app.py) ---
|
| 25 |
+
_LOG_LEVEL = (os.getenv("LOG_LEVEL") or "INFO").upper()
|
| 26 |
+
logging.basicConfig(
|
| 27 |
+
level=getattr(logging, _LOG_LEVEL, logging.INFO),
|
| 28 |
+
format="%(asctime)s %(levelname)s %(name)s - %(message)s",
|
| 29 |
+
)
|
| 30 |
+
logger = logging.getLogger("rust_coder.server")
|
| 31 |
+
|
| 32 |
# --- Core OpenEnv Server Setup ---
|
| 33 |
# Use a distinct name for the OpenEnv FastAPI instance
|
| 34 |
openenv_app = create_app(
|
|
|
|
| 52 |
def get_llm_solution(problem_desc: str):
|
| 53 |
"""Call LLM to get a Rust solution"""
|
| 54 |
try:
|
| 55 |
+
logger.info(
|
| 56 |
+
"LLM call start model=%s base_url=%s prompt_chars=%d token_present=%s",
|
| 57 |
+
MODEL_NAME,
|
| 58 |
+
API_BASE_URL,
|
| 59 |
+
len(problem_desc or ""),
|
| 60 |
+
bool(HF_TOKEN),
|
| 61 |
+
)
|
| 62 |
client_llm = OpenAI(base_url=API_BASE_URL, api_key=HF_TOKEN)
|
| 63 |
completion = client_llm.chat.completions.create(
|
| 64 |
model=MODEL_NAME,
|
|
|
|
| 69 |
temperature=0.2,
|
| 70 |
)
|
| 71 |
text = (completion.choices[0].message.content or "").strip()
|
| 72 |
+
logger.debug("LLM raw response chars=%d", len(text))
|
| 73 |
# Clean markdown code blocks
|
| 74 |
if "```rust" in text:
|
| 75 |
text = text.split("```rust")[1].split("```")[0]
|
|
|
|
| 77 |
text = text.split("```")[1].split("```")[0]
|
| 78 |
text = text.strip()
|
| 79 |
if not text:
|
| 80 |
+
logger.warning("LLM returned empty code after cleanup.")
|
| 81 |
return "// LLM Error: empty response (no code returned)."
|
| 82 |
+
logger.info("LLM call end: returned_code_chars=%d", len(text))
|
| 83 |
return text
|
| 84 |
except Exception as e:
|
| 85 |
+
logger.exception("LLM call failed.")
|
| 86 |
return f"// LLM Error: {e}"
|
| 87 |
|
| 88 |
def evaluate_single(problem_id, code=None):
|
|
|
|
| 90 |
try:
|
| 91 |
idx = int(problem_id.split(":")[0]) - 1
|
| 92 |
problem = RustCoderEnvironment().problems[idx]
|
| 93 |
+
logger.info(
|
| 94 |
+
"evaluate_single start problem_id=%s idx=%d code_provided=%s",
|
| 95 |
+
problem_id,
|
| 96 |
+
idx,
|
| 97 |
+
code is not None,
|
| 98 |
+
)
|
| 99 |
|
| 100 |
# 1. Get code from LLM if not provided
|
| 101 |
solution_code = code if code else get_llm_solution(problem["description"])
|
| 102 |
|
| 103 |
# 2. Guard: If LLM failed, do not evaluate
|
| 104 |
if not solution_code.strip() or solution_code.startswith("// LLM Error"):
|
| 105 |
+
logger.warning(
|
| 106 |
+
"evaluate_single abort: empty_or_error_code=%s chars=%d",
|
| 107 |
+
solution_code.startswith("// LLM Error"),
|
| 108 |
+
len(solution_code or ""),
|
| 109 |
+
)
|
| 110 |
return solution_code, {"error": "LLM failed to generate a solution. Check your HF_TOKEN."}
|
| 111 |
|
| 112 |
# 3. Evaluate properly
|
| 113 |
env = RustCoderEnvironment()
|
| 114 |
# Reset to the specifically requested index
|
| 115 |
state = env.reset(start_index=idx)
|
| 116 |
+
logger.debug("evaluate_single step() submitting chars=%d", len(solution_code))
|
| 117 |
state = env.step(RustCoderAction(code=solution_code))
|
| 118 |
+
logger.info(
|
| 119 |
+
"evaluate_single end reward=%.4f compilation_success=%s",
|
| 120 |
+
float(state.reward or 0.0),
|
| 121 |
+
bool(state.compilation_success),
|
| 122 |
+
)
|
| 123 |
|
| 124 |
metrics = {
|
| 125 |
"Total Reward": f"{state.reward:.2f}",
|
|
|
|
| 128 |
}
|
| 129 |
return solution_code, metrics
|
| 130 |
except Exception as e:
|
| 131 |
+
logger.exception("evaluate_single crashed.")
|
| 132 |
return f"// Error: {e}", {"error": f"Evaluation system error: {e}"}
|
| 133 |
|
| 134 |
def run_benchmark(progress=gr.Progress()):
|
server/rust_coder_environment.py
CHANGED
|
@@ -12,6 +12,7 @@ import re
|
|
| 12 |
import subprocess
|
| 13 |
import tempfile
|
| 14 |
import time
|
|
|
|
| 15 |
from typing import Dict, List, Optional, Tuple
|
| 16 |
|
| 17 |
from openenv.core.env_server.interfaces import Environment
|
|
@@ -63,6 +64,7 @@ class RustCoderEnvironment(Environment):
|
|
| 63 |
|
| 64 |
def __init__(self) -> None:
|
| 65 |
"""Initialize environment and load problems from JSON."""
|
|
|
|
| 66 |
self.problems: List[Dict] = self._load_problems()
|
| 67 |
self.current_problem_idx: int = 0
|
| 68 |
self.step_count: int = 0
|
|
@@ -119,6 +121,12 @@ class RustCoderEnvironment(Environment):
|
|
| 119 |
|
| 120 |
if not code.strip():
|
| 121 |
# Invalid/empty submission: do not advance the problem index.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 122 |
done = False
|
| 123 |
return RustCoderObservation(
|
| 124 |
problem_description=problem["description"],
|
|
|
|
| 12 |
import subprocess
|
| 13 |
import tempfile
|
| 14 |
import time
|
| 15 |
+
import logging
|
| 16 |
from typing import Dict, List, Optional, Tuple
|
| 17 |
|
| 18 |
from openenv.core.env_server.interfaces import Environment
|
|
|
|
| 64 |
|
| 65 |
def __init__(self) -> None:
|
| 66 |
"""Initialize environment and load problems from JSON."""
|
| 67 |
+
self._logger = logging.getLogger("rust_coder.env")
|
| 68 |
self.problems: List[Dict] = self._load_problems()
|
| 69 |
self.current_problem_idx: int = 0
|
| 70 |
self.step_count: int = 0
|
|
|
|
| 121 |
|
| 122 |
if not code.strip():
|
| 123 |
# Invalid/empty submission: do not advance the problem index.
|
| 124 |
+
self._logger.warning(
|
| 125 |
+
"Empty code submitted step_count=%d problem_id=%s title=%s",
|
| 126 |
+
self.step_count,
|
| 127 |
+
problem.get("id"),
|
| 128 |
+
problem.get("title"),
|
| 129 |
+
)
|
| 130 |
done = False
|
| 131 |
return RustCoderObservation(
|
| 132 |
problem_description=problem["description"],
|