Spaces:
Sleeping
Sleeping
Upload folder using huggingface_hub
Browse files- inference.py +113 -8
inference.py
CHANGED
|
@@ -48,8 +48,7 @@ import textwrap
|
|
| 48 |
from io import BytesIO
|
| 49 |
from pathlib import Path
|
| 50 |
from typing import Dict, List, Optional
|
| 51 |
-
|
| 52 |
-
import numpy as np
|
| 53 |
|
| 54 |
try:
|
| 55 |
from ..models import EdupilotAction, EdupilotObservation
|
|
@@ -101,13 +100,14 @@ ACTION_PATTERN = re.compile(r"[A-Za-z_]+\s*\(.*\)", re.DOTALL)
|
|
| 101 |
SYSTEM_PROMPT = textwrap.dedent("""
|
| 102 |
You are an intelligent agent and a structured data generator.
|
| 103 |
|
| 104 |
-
Your task is to produce a valid JSON object that strictly adheres to the provided schema.
|
| 105 |
|
| 106 |
Instructions:
|
| 107 |
- If the task is to create or transform data β output ONLY valid JSON.
|
| 108 |
|
| 109 |
Rules:
|
| 110 |
- Output ONLY valid JSON.
|
|
|
|
| 111 |
- Do not include explanations, comments, or extra text.
|
| 112 |
- Follow the schema exactly (keys, nesting, and structure).
|
| 113 |
- Preserve all provided values unless instructed otherwise.
|
|
@@ -115,13 +115,107 @@ SYSTEM_PROMPT = textwrap.dedent("""
|
|
| 115 |
- Never mix JSON and actions.
|
| 116 |
- No explanations or extra text.
|
| 117 |
- Follow the schema strictly when generating JSON.
|
| 118 |
-
- Keep the values in 'extra-details' in the final JSON labels etc. as they are in the details section of given schema
|
| 119 |
- Labels should be string and should not contain any link.
|
| 120 |
- Convert deadline in hh:mm AM/PM format.
|
| 121 |
- If unsure:
|
| 122 |
- For JSON tasks β best-effort valid JSON
|
| 123 |
""").strip()
|
| 124 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 125 |
|
| 126 |
class PromptContextModel(BaseModel):
|
| 127 |
task: str = Field(default="I want to send an assignment reminder email to the students. All the details should be in the final notification JSON.", description="")
|
|
@@ -195,22 +289,26 @@ def build_user_prompt(step: int, observation, prompt_context: PromptContextModel
|
|
| 195 |
deadline: {prompt_context.deadline}
|
| 196 |
associated lecture link: {prompt_context.associated_lecture_link}
|
| 197 |
additional task: {prompt_context.additional_task}
|
| 198 |
-
Keep the values in 'extra-details' in the final JSON labels etc. as they are in the details section of given schema
|
| 199 |
Labels should be string and should not contain any link.
|
| 200 |
Convert deadline in hh:mm AM/PM format.
|
| 201 |
-
|
|
|
|
| 202 |
""").strip()
|
| 203 |
|
| 204 |
prompt = textwrap.dedent(f"""
|
| 205 |
Step: {step}
|
| 206 |
Goal: {goal}
|
| 207 |
-
schema: {
|
|
|
|
| 208 |
Previous steps:
|
| 209 |
{build_history_lines(history)}
|
| 210 |
Last action error: {error_note}
|
| 211 |
Current context: {context_text}
|
| 212 |
-
|
|
|
|
| 213 |
""").strip()
|
|
|
|
| 214 |
return prompt
|
| 215 |
|
| 216 |
|
|
@@ -218,6 +316,13 @@ def parse_model_action(response_text: str) -> str:
|
|
| 218 |
if not response_text:
|
| 219 |
return FALLBACK_ACTION
|
| 220 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 221 |
lines = response_text.splitlines()
|
| 222 |
for raw_line in lines:
|
| 223 |
line = raw_line.strip()
|
|
|
|
| 48 |
from io import BytesIO
|
| 49 |
from pathlib import Path
|
| 50 |
from typing import Dict, List, Optional
|
| 51 |
+
from pydantic import BaseModel, Field
|
|
|
|
| 52 |
|
| 53 |
try:
|
| 54 |
from ..models import EdupilotAction, EdupilotObservation
|
|
|
|
| 100 |
SYSTEM_PROMPT = textwrap.dedent("""
|
| 101 |
You are an intelligent agent and a structured data generator.
|
| 102 |
|
| 103 |
+
Your task is to produce a valid JSON object that strictly adheres to the provided notification schema and notification instance.
|
| 104 |
|
| 105 |
Instructions:
|
| 106 |
- If the task is to create or transform data β output ONLY valid JSON.
|
| 107 |
|
| 108 |
Rules:
|
| 109 |
- Output ONLY valid JSON.
|
| 110 |
+
- This valid JSON has to in format that can be parsed programmatically.
|
| 111 |
- Do not include explanations, comments, or extra text.
|
| 112 |
- Follow the schema exactly (keys, nesting, and structure).
|
| 113 |
- Preserve all provided values unless instructed otherwise.
|
|
|
|
| 115 |
- Never mix JSON and actions.
|
| 116 |
- No explanations or extra text.
|
| 117 |
- Follow the schema strictly when generating JSON.
|
| 118 |
+
- Keep the values in 'extra-details' in the final JSON labels etc. as they are in the details section of given notification schema and notification instance.
|
| 119 |
- Labels should be string and should not contain any link.
|
| 120 |
- Convert deadline in hh:mm AM/PM format.
|
| 121 |
- If unsure:
|
| 122 |
- For JSON tasks β best-effort valid JSON
|
| 123 |
""").strip()
|
| 124 |
|
| 125 |
+
notification_schema = {
|
| 126 |
+
"$schema": "http://json-schema.org/draft-07/schema#",
|
| 127 |
+
"title": "Notification",
|
| 128 |
+
"type": "object",
|
| 129 |
+
"required": ["notification"],
|
| 130 |
+
"properties": {
|
| 131 |
+
"notification": {
|
| 132 |
+
"type": "object",
|
| 133 |
+
"required": ["brand_name", "greetings", "message", "details"],
|
| 134 |
+
"properties": {
|
| 135 |
+
"brand_name": { "type": "string" },
|
| 136 |
+
"message": { "type": "string" },
|
| 137 |
+
"greetings": {
|
| 138 |
+
"type": "object",
|
| 139 |
+
"required": ["prefix", "username"],
|
| 140 |
+
"properties": {
|
| 141 |
+
"prefix": { "type": "string" },
|
| 142 |
+
"username": { "type": "string" }
|
| 143 |
+
}
|
| 144 |
+
},
|
| 145 |
+
"details": {
|
| 146 |
+
"type": "array",
|
| 147 |
+
"minItems": 1,
|
| 148 |
+
"items": {
|
| 149 |
+
"type": "object",
|
| 150 |
+
"required": ["category", "type", "label", "value"],
|
| 151 |
+
"properties": {
|
| 152 |
+
"category": {
|
| 153 |
+
"type": "string",
|
| 154 |
+
"enum": ["main-details", "extra-details"]
|
| 155 |
+
},
|
| 156 |
+
"type": {
|
| 157 |
+
"type": "string",
|
| 158 |
+
"enum": [
|
| 159 |
+
"assignment_title",
|
| 160 |
+
"deadline",
|
| 161 |
+
"lms_link",
|
| 162 |
+
"associated_lecture_link",
|
| 163 |
+
"youtube_lecture_link"
|
| 164 |
+
]
|
| 165 |
+
},
|
| 166 |
+
"label": { "type": "string" },
|
| 167 |
+
"value": { "type": "string" }
|
| 168 |
+
}
|
| 169 |
+
}
|
| 170 |
+
}
|
| 171 |
+
}
|
| 172 |
+
}
|
| 173 |
+
}
|
| 174 |
+
}
|
| 175 |
+
|
| 176 |
+
notification_instance = {
|
| 177 |
+
"notification": {
|
| 178 |
+
"brand_name": "Scaler School",
|
| 179 |
+
"greetings": {
|
| 180 |
+
"prefix": "Dear",
|
| 181 |
+
"username": "Mithilesh Nakade"
|
| 182 |
+
},
|
| 183 |
+
"message": "A new assignment has been released for you.",
|
| 184 |
+
"details": [
|
| 185 |
+
{
|
| 186 |
+
"category": "main-details",
|
| 187 |
+
"type": "assignment_title",
|
| 188 |
+
"label": "π Assignment Title:",
|
| 189 |
+
"value": "Faculty Session - Meta and Scaler OpenEnv Hackathon"
|
| 190 |
+
},
|
| 191 |
+
{
|
| 192 |
+
"category": "main-details",
|
| 193 |
+
"type": "deadline",
|
| 194 |
+
"label": "π Deadline:",
|
| 195 |
+
"value": "Apr 9, 2026 at 11:59 PM"
|
| 196 |
+
},
|
| 197 |
+
{
|
| 198 |
+
"category": "extra-details",
|
| 199 |
+
"type": "lms_link",
|
| 200 |
+
"label": "π LMS Link:",
|
| 201 |
+
"value": "https://www.scaler.com/school-of-technology/meta-pytorch-hackathon/"
|
| 202 |
+
},
|
| 203 |
+
{
|
| 204 |
+
"category": "extra-details",
|
| 205 |
+
"type": "associated_lecture_link",
|
| 206 |
+
"label": "associated lecture:",
|
| 207 |
+
"value": "https://www.scaler.com/school-of-technology/meta-pytorch-hackathon/dashboard?utm_source=midfunnel&utm_medium=email&utm_campaign=registration_acknowledgement"
|
| 208 |
+
},
|
| 209 |
+
{
|
| 210 |
+
"category": "extra-details",
|
| 211 |
+
"type": "youtube_lecture_link",
|
| 212 |
+
"label": "Youtube link:",
|
| 213 |
+
"value": "https://www.youtube.com/watch?v=kkCNMz0Ptd8&t=1703s"
|
| 214 |
+
}
|
| 215 |
+
]
|
| 216 |
+
}
|
| 217 |
+
}
|
| 218 |
+
|
| 219 |
|
| 220 |
class PromptContextModel(BaseModel):
|
| 221 |
task: str = Field(default="I want to send an assignment reminder email to the students. All the details should be in the final notification JSON.", description="")
|
|
|
|
| 289 |
deadline: {prompt_context.deadline}
|
| 290 |
associated lecture link: {prompt_context.associated_lecture_link}
|
| 291 |
additional task: {prompt_context.additional_task}
|
| 292 |
+
Keep the values in 'extra-details' in the final JSON labels etc. as they are in the details section of given notification schema and notification instance.
|
| 293 |
Labels should be string and should not contain any link.
|
| 294 |
Convert deadline in hh:mm AM/PM format.
|
| 295 |
+
Convert the given instance in to valid JSON format that can be parsed programmatically.
|
| 296 |
+
Reply with exactly one EduPilot action json which adhers by given notification schema and notification instance.
|
| 297 |
""").strip()
|
| 298 |
|
| 299 |
prompt = textwrap.dedent(f"""
|
| 300 |
Step: {step}
|
| 301 |
Goal: {goal}
|
| 302 |
+
Notification schema: {notification_schema}
|
| 303 |
+
Example instance: {notification_instance}
|
| 304 |
Previous steps:
|
| 305 |
{build_history_lines(history)}
|
| 306 |
Last action error: {error_note}
|
| 307 |
Current context: {context_text}
|
| 308 |
+
Convert the given instance in to valid JSON format that can be parsed programmatically.
|
| 309 |
+
Reply with exactly one EduPilot action json which adhers by given notification schema and notification instance.
|
| 310 |
""").strip()
|
| 311 |
+
|
| 312 |
return prompt
|
| 313 |
|
| 314 |
|
|
|
|
| 316 |
if not response_text:
|
| 317 |
return FALLBACK_ACTION
|
| 318 |
|
| 319 |
+
try:
|
| 320 |
+
response_text_json = json.loads(response_text)
|
| 321 |
+
if response_text_json:
|
| 322 |
+
return response_text_json
|
| 323 |
+
except json.JSONDecodeError:
|
| 324 |
+
pass
|
| 325 |
+
|
| 326 |
lines = response_text.splitlines()
|
| 327 |
for raw_line in lines:
|
| 328 |
line = raw_line.strip()
|