Spaces:
Sleeping
Sleeping
Upload folder using huggingface_hub
Browse files- inference.py +47 -82
inference.py
CHANGED
|
@@ -3,18 +3,16 @@ import requests
|
|
| 3 |
import json
|
| 4 |
from openai import OpenAI
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
openai_client =
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
else:
|
| 17 |
-
print(f"[INFO] API_KEY not set. Using fallback heuristic agent.", flush=True)
|
| 18 |
|
| 19 |
SYSTEM_PROMPT = """You are an expert code reviewer AI agent. Your task is to analyze code snippets for bugs and issues.
|
| 20 |
|
|
@@ -33,53 +31,15 @@ Guidelines:
|
|
| 33 |
Respond only with valid JSON action."""
|
| 34 |
|
| 35 |
|
| 36 |
-
def get_fallback_action(observation, step_in_task):
|
| 37 |
-
"""
|
| 38 |
-
Fallback heuristic agent when HF_TOKEN is not available.
|
| 39 |
-
Uses simple pattern matching to find common bugs.
|
| 40 |
-
"""
|
| 41 |
-
task_name = observation.get("task_name", "easy")
|
| 42 |
-
issues_found = set(observation.get("issues_found_so_far", []))
|
| 43 |
-
max_issues = {"easy": 3, "medium": 4, "hard": 5}.get(task_name, 3)
|
| 44 |
-
|
| 45 |
-
# Heuristic: try lines in a pattern based on task difficulty
|
| 46 |
-
if task_name == "easy":
|
| 47 |
-
candidate_lines = [2, 5, 7] # Known positions for easy task
|
| 48 |
-
elif task_name == "medium":
|
| 49 |
-
candidate_lines = [1, 3, 4, 5] # Known positions for medium task
|
| 50 |
-
else: # hard
|
| 51 |
-
candidate_lines = [3, 4, 6, 7, 8] # Known positions for hard task
|
| 52 |
-
|
| 53 |
-
# Return the next unfound issue
|
| 54 |
-
for line in candidate_lines:
|
| 55 |
-
if line not in issues_found:
|
| 56 |
-
return {
|
| 57 |
-
"action_type": "FLAG_BUG",
|
| 58 |
-
"line_number": line,
|
| 59 |
-
"issue_type": "bug",
|
| 60 |
-
"comment": f"Potential issue detected at line {line}"
|
| 61 |
-
}
|
| 62 |
-
|
| 63 |
-
# If all known issues found, return no action
|
| 64 |
-
return {
|
| 65 |
-
"action_type": "FLAG_BUG",
|
| 66 |
-
"line_number": None,
|
| 67 |
-
"issue_type": "none",
|
| 68 |
-
"comment": "No action"
|
| 69 |
-
}
|
| 70 |
-
|
| 71 |
-
|
| 72 |
def get_action_from_model(observation):
|
| 73 |
-
"""
|
| 74 |
-
|
| 75 |
-
return get_fallback_action(observation, 0)
|
| 76 |
-
|
| 77 |
code = observation["code_snippet"]
|
| 78 |
task = observation["task_name"]
|
| 79 |
step = observation["step_number"]
|
| 80 |
max_steps = observation["max_steps"]
|
| 81 |
found = observation["issues_found_so_far"]
|
| 82 |
-
|
| 83 |
user_prompt = f"""Task: {task}
|
| 84 |
Current step: {step}/{max_steps}
|
| 85 |
Issues found so far: {found}
|
|
@@ -93,33 +53,33 @@ What action do you take next? Respond with JSON action."""
|
|
| 93 |
{"role": "system", "content": SYSTEM_PROMPT},
|
| 94 |
{"role": "user", "content": user_prompt}
|
| 95 |
]
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
return
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
|
| 124 |
|
| 125 |
def safe_json_request(method, url, **kwargs):
|
|
@@ -174,8 +134,13 @@ def run_suite():
|
|
| 174 |
while not done and steps < 50: # Safety limit
|
| 175 |
action = get_action_from_model(res)
|
| 176 |
if not validate_action(action):
|
| 177 |
-
print(f"[WARN] Invalid action from model, using
|
| 178 |
-
action =
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 179 |
|
| 180 |
try:
|
| 181 |
response = safe_json_request("POST", f"{API_BASE_URL}/step", json=action)
|
|
|
|
| 3 |
import json
|
| 4 |
from openai import OpenAI
|
| 5 |
|
| 6 |
+
# ✅ STRICT: use ONLY injected environment variables (no defaults)
|
| 7 |
+
API_BASE_URL = os.environ["API_BASE_URL"].rstrip("/")
|
| 8 |
+
MODEL_NAME = os.environ["MODEL_NAME"]
|
| 9 |
+
API_KEY = os.environ["API_KEY"]
|
| 10 |
+
|
| 11 |
+
# ✅ Initialize OpenAI client with THEIR proxy
|
| 12 |
+
openai_client = OpenAI(
|
| 13 |
+
api_key=API_KEY,
|
| 14 |
+
base_url=API_BASE_URL
|
| 15 |
+
)
|
|
|
|
|
|
|
| 16 |
|
| 17 |
SYSTEM_PROMPT = """You are an expert code reviewer AI agent. Your task is to analyze code snippets for bugs and issues.
|
| 18 |
|
|
|
|
| 31 |
Respond only with valid JSON action."""
|
| 32 |
|
| 33 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
def get_action_from_model(observation):
|
| 35 |
+
"""Always use LLM via proxy (no fallback)."""
|
| 36 |
+
|
|
|
|
|
|
|
| 37 |
code = observation["code_snippet"]
|
| 38 |
task = observation["task_name"]
|
| 39 |
step = observation["step_number"]
|
| 40 |
max_steps = observation["max_steps"]
|
| 41 |
found = observation["issues_found_so_far"]
|
| 42 |
+
|
| 43 |
user_prompt = f"""Task: {task}
|
| 44 |
Current step: {step}/{max_steps}
|
| 45 |
Issues found so far: {found}
|
|
|
|
| 53 |
{"role": "system", "content": SYSTEM_PROMPT},
|
| 54 |
{"role": "user", "content": user_prompt}
|
| 55 |
]
|
| 56 |
+
|
| 57 |
+
response = openai_client.chat.completions.create(
|
| 58 |
+
model=MODEL_NAME,
|
| 59 |
+
messages=messages,
|
| 60 |
+
max_tokens=200,
|
| 61 |
+
temperature=0.1
|
| 62 |
+
)
|
| 63 |
+
|
| 64 |
+
generated_text = response.choices[0].message.content or ""
|
| 65 |
+
|
| 66 |
+
# Extract JSON safely
|
| 67 |
+
start = generated_text.find("{")
|
| 68 |
+
end = generated_text.rfind("}") + 1
|
| 69 |
+
|
| 70 |
+
if start != -1 and end > start:
|
| 71 |
+
try:
|
| 72 |
+
return json.loads(generated_text[start:end])
|
| 73 |
+
except:
|
| 74 |
+
pass
|
| 75 |
+
|
| 76 |
+
# Minimal safe fallback (still after API call)
|
| 77 |
+
return {
|
| 78 |
+
"action_type": "FLAG_BUG",
|
| 79 |
+
"line_number": None,
|
| 80 |
+
"issue_type": "none",
|
| 81 |
+
"comment": "Parsing error"
|
| 82 |
+
}
|
| 83 |
|
| 84 |
|
| 85 |
def safe_json_request(method, url, **kwargs):
|
|
|
|
| 134 |
while not done and steps < 50: # Safety limit
|
| 135 |
action = get_action_from_model(res)
|
| 136 |
if not validate_action(action):
|
| 137 |
+
print(f"[WARN] Invalid action from model, using safe default: {action}", flush=True)
|
| 138 |
+
action = {
|
| 139 |
+
"action_type": "FLAG_BUG",
|
| 140 |
+
"line_number": None,
|
| 141 |
+
"issue_type": "none",
|
| 142 |
+
"comment": "Invalid action"
|
| 143 |
+
}
|
| 144 |
|
| 145 |
try:
|
| 146 |
response = safe_json_request("POST", f"{API_BASE_URL}/step", json=action)
|