Spaces:
Runtime error
Runtime error
Zach Wentz commited on
Commit ·
fb4e6b7
1
Parent(s): 67a233b
🤖 Deploy chat_env environment - 2025-10-21 22:49:50
Browse files- Dockerfile +26 -2
- src/core/__pycache__/__init__.cpython-311.pyc +0 -0
- src/core/__pycache__/__init__.cpython-313.pyc +0 -0
- src/core/__pycache__/http_env_client.cpython-311.pyc +0 -0
- src/core/__pycache__/http_env_client.cpython-313.pyc +0 -0
- src/core/__pycache__/types.cpython-311.pyc +0 -0
- src/core/__pycache__/types.cpython-313.pyc +0 -0
- src/core/containers/__pycache__/__init__.cpython-311.pyc +0 -0
- src/core/containers/__pycache__/__init__.cpython-313.pyc +0 -0
- src/core/containers/runtime/__pycache__/__init__.cpython-311.pyc +0 -0
- src/core/containers/runtime/__pycache__/__init__.cpython-313.pyc +0 -0
- src/core/containers/runtime/__pycache__/providers.cpython-311.pyc +0 -0
- src/core/containers/runtime/__pycache__/providers.cpython-313.pyc +0 -0
- src/core/env_server/__pycache__/__init__.cpython-311.pyc +0 -0
- src/core/env_server/__pycache__/__init__.cpython-313.pyc +0 -0
- src/core/env_server/__pycache__/base_transforms.cpython-311.pyc +0 -0
- src/core/env_server/__pycache__/base_transforms.cpython-313.pyc +0 -0
- src/core/env_server/__pycache__/http_server.cpython-311.pyc +0 -0
- src/core/env_server/__pycache__/http_server.cpython-313.pyc +0 -0
- src/core/env_server/__pycache__/interfaces.cpython-311.pyc +0 -0
- src/core/env_server/__pycache__/interfaces.cpython-313.pyc +0 -0
- src/core/env_server/__pycache__/types.cpython-311.pyc +0 -0
- src/core/env_server/__pycache__/types.cpython-313.pyc +0 -0
- src/core/env_server/__pycache__/web_interface.cpython-311.pyc +0 -0
- src/core/env_server/__pycache__/web_interface.cpython-313.pyc +0 -0
- src/envs/chat_env/server/app.py +9 -1
- src/envs/chat_env/server/chat_environment.py +38 -10
Dockerfile
CHANGED
|
@@ -4,8 +4,31 @@
|
|
| 4 |
# This source code is licensed under the BSD-style license found in the
|
| 5 |
# LICENSE file in the root directory of this source tree.
|
| 6 |
|
| 7 |
-
#
|
| 8 |
-
FROM
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
# Install additional dependencies for ChatEnvironment
|
| 10 |
RUN pip install --no-cache-dir torch transformers
|
| 11 |
|
|
@@ -17,6 +40,7 @@ ENV TRANSFORMERS_CACHE=/.cache
|
|
| 17 |
# Pre-download the GPT-2 model to avoid permission issues during runtime
|
| 18 |
RUN python -c "from transformers import GPT2Tokenizer; GPT2Tokenizer.from_pretrained('gpt2')"
|
| 19 |
|
|
|
|
| 20 |
# Copy only what's needed for this environment
|
| 21 |
COPY src/core/ /app/src/core/
|
| 22 |
COPY src/envs/chat_env/ /app/src/envs/chat_env/
|
|
|
|
| 4 |
# This source code is licensed under the BSD-style license found in the
|
| 5 |
# LICENSE file in the root directory of this source tree.
|
| 6 |
|
| 7 |
+
# Multi-stage build: First stage builds the base image
|
| 8 |
+
FROM python:3.11-slim as base-builder
|
| 9 |
+
|
| 10 |
+
# Install system dependencies
|
| 11 |
+
RUN apt-get update && apt-get install -y --no-install-recommends \
|
| 12 |
+
curl \
|
| 13 |
+
&& rm -rf /var/lib/apt/lists/*
|
| 14 |
+
|
| 15 |
+
# Install Python dependencies that all environments need
|
| 16 |
+
RUN pip install --no-cache-dir \
|
| 17 |
+
fastapi>=0.104.0 \
|
| 18 |
+
"uvicorn[standard]>=0.24.0" \
|
| 19 |
+
requests>=2.25.0 \
|
| 20 |
+
wsproto>=1.0.0
|
| 21 |
+
|
| 22 |
+
# Set working directory
|
| 23 |
+
WORKDIR /app
|
| 24 |
+
|
| 25 |
+
# Default environment variables
|
| 26 |
+
ENV PYTHONPATH=/app/src
|
| 27 |
+
ENV PYTHONUNBUFFERED=1
|
| 28 |
+
|
| 29 |
+
# Second stage: Use the built base image and add environment-specific dependencies
|
| 30 |
+
FROM base-builder
|
| 31 |
+
|
| 32 |
# Install additional dependencies for ChatEnvironment
|
| 33 |
RUN pip install --no-cache-dir torch transformers
|
| 34 |
|
|
|
|
| 40 |
# Pre-download the GPT-2 model to avoid permission issues during runtime
|
| 41 |
RUN python -c "from transformers import GPT2Tokenizer; GPT2Tokenizer.from_pretrained('gpt2')"
|
| 42 |
|
| 43 |
+
|
| 44 |
# Copy only what's needed for this environment
|
| 45 |
COPY src/core/ /app/src/core/
|
| 46 |
COPY src/envs/chat_env/ /app/src/envs/chat_env/
|
src/core/__pycache__/__init__.cpython-311.pyc
ADDED
|
Binary file (400 Bytes). View file
|
|
|
src/core/__pycache__/__init__.cpython-313.pyc
ADDED
|
Binary file (383 Bytes). View file
|
|
|
src/core/__pycache__/http_env_client.cpython-311.pyc
ADDED
|
Binary file (7.68 kB). View file
|
|
|
src/core/__pycache__/http_env_client.cpython-313.pyc
ADDED
|
Binary file (6.93 kB). View file
|
|
|
src/core/__pycache__/types.cpython-311.pyc
ADDED
|
Binary file (1.09 kB). View file
|
|
|
src/core/__pycache__/types.cpython-313.pyc
ADDED
|
Binary file (993 Bytes). View file
|
|
|
src/core/containers/__pycache__/__init__.cpython-311.pyc
ADDED
|
Binary file (206 Bytes). View file
|
|
|
src/core/containers/__pycache__/__init__.cpython-313.pyc
ADDED
|
Binary file (224 Bytes). View file
|
|
|
src/core/containers/runtime/__pycache__/__init__.cpython-311.pyc
ADDED
|
Binary file (389 Bytes). View file
|
|
|
src/core/containers/runtime/__pycache__/__init__.cpython-313.pyc
ADDED
|
Binary file (375 Bytes). View file
|
|
|
src/core/containers/runtime/__pycache__/providers.cpython-311.pyc
ADDED
|
Binary file (10.9 kB). View file
|
|
|
src/core/containers/runtime/__pycache__/providers.cpython-313.pyc
ADDED
|
Binary file (9.64 kB). View file
|
|
|
src/core/env_server/__pycache__/__init__.cpython-311.pyc
ADDED
|
Binary file (898 Bytes). View file
|
|
|
src/core/env_server/__pycache__/__init__.cpython-313.pyc
ADDED
|
Binary file (788 Bytes). View file
|
|
|
src/core/env_server/__pycache__/base_transforms.cpython-311.pyc
ADDED
|
Binary file (1.67 kB). View file
|
|
|
src/core/env_server/__pycache__/base_transforms.cpython-313.pyc
ADDED
|
Binary file (1.57 kB). View file
|
|
|
src/core/env_server/__pycache__/http_server.cpython-311.pyc
ADDED
|
Binary file (9.2 kB). View file
|
|
|
src/core/env_server/__pycache__/http_server.cpython-313.pyc
ADDED
|
Binary file (8.33 kB). View file
|
|
|
src/core/env_server/__pycache__/interfaces.cpython-311.pyc
ADDED
|
Binary file (5.22 kB). View file
|
|
|
src/core/env_server/__pycache__/interfaces.cpython-313.pyc
ADDED
|
Binary file (4.68 kB). View file
|
|
|
src/core/env_server/__pycache__/types.cpython-311.pyc
ADDED
|
Binary file (2.39 kB). View file
|
|
|
src/core/env_server/__pycache__/types.cpython-313.pyc
ADDED
|
Binary file (2.66 kB). View file
|
|
|
src/core/env_server/__pycache__/web_interface.cpython-311.pyc
ADDED
|
Binary file (29.9 kB). View file
|
|
|
src/core/env_server/__pycache__/web_interface.cpython-313.pyc
ADDED
|
Binary file (59.3 kB). View file
|
|
|
src/envs/chat_env/server/app.py
CHANGED
|
@@ -36,12 +36,20 @@ from .chat_environment import ChatEnvironment
|
|
| 36 |
# Initialize tokenizer based on environment variable
|
| 37 |
def get_tokenizer():
|
| 38 |
"""Get tokenizer from environment or use a mock for testing."""
|
| 39 |
-
tokenizer_name = os.environ.get("TOKENIZER_NAME", "
|
| 40 |
|
| 41 |
try:
|
| 42 |
from transformers import AutoTokenizer
|
| 43 |
|
| 44 |
tokenizer = AutoTokenizer.from_pretrained(tokenizer_name)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
print(f"Loaded tokenizer: {tokenizer_name}")
|
| 46 |
return tokenizer
|
| 47 |
except ImportError:
|
|
|
|
| 36 |
# Initialize tokenizer based on environment variable
|
| 37 |
def get_tokenizer():
|
| 38 |
"""Get tokenizer from environment or use a mock for testing."""
|
| 39 |
+
tokenizer_name = os.environ.get("TOKENIZER_NAME", "microsoft/DialoGPT-medium")
|
| 40 |
|
| 41 |
try:
|
| 42 |
from transformers import AutoTokenizer
|
| 43 |
|
| 44 |
tokenizer = AutoTokenizer.from_pretrained(tokenizer_name)
|
| 45 |
+
|
| 46 |
+
# Set a chat template if the tokenizer doesn't have one
|
| 47 |
+
if tokenizer.chat_template is None:
|
| 48 |
+
# Use a simple chat template that works with most tokenizers
|
| 49 |
+
chat_template = "{% for message in messages %}{% if message['role'] == 'user' %}User: {{ message['content'] }}{% elif message['role'] == 'assistant' %}Assistant: {{ message['content'] }}{% elif message['role'] == 'system' %}System: {{ message['content'] }}{% endif %}{% if not loop.last %}\n{% endif %}{% endfor %}"
|
| 50 |
+
tokenizer.chat_template = chat_template
|
| 51 |
+
print(f"Set custom chat template for {tokenizer_name}")
|
| 52 |
+
|
| 53 |
print(f"Loaded tokenizer: {tokenizer_name}")
|
| 54 |
return tokenizer
|
| 55 |
except ImportError:
|
src/envs/chat_env/server/chat_environment.py
CHANGED
|
@@ -54,9 +54,18 @@ class ChatEnvironment(Environment):
|
|
| 54 |
system_message: Message = {"role": system_role, "content": system_prompt}
|
| 55 |
self._state.history_messages.append(system_message)
|
| 56 |
# Tokenize the system message
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
self._state.history_tokens.append(system_tokens)
|
| 61 |
|
| 62 |
def reset(self) -> ChatObservation:
|
|
@@ -74,9 +83,18 @@ class ChatEnvironment(Environment):
|
|
| 74 |
}
|
| 75 |
self._state.history_messages = [system_message]
|
| 76 |
# Tokenize the system message
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
self._state.history_tokens = [system_tokens]
|
| 81 |
|
| 82 |
return self._create_observation()
|
|
@@ -164,9 +182,19 @@ class ChatEnvironment(Environment):
|
|
| 164 |
if message["content"] is None:
|
| 165 |
raise ValueError("Message content cannot be None")
|
| 166 |
|
| 167 |
-
#
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 171 |
|
| 172 |
return ChatAction(tokens=tokens)
|
|
|
|
| 54 |
system_message: Message = {"role": system_role, "content": system_prompt}
|
| 55 |
self._state.history_messages.append(system_message)
|
| 56 |
# Tokenize the system message
|
| 57 |
+
try:
|
| 58 |
+
system_tokens = self.tokenizer.apply_chat_template(
|
| 59 |
+
conversation=[system_message], tokenize=True, return_tensors="pt" # type: ignore
|
| 60 |
+
)
|
| 61 |
+
except (ValueError, AttributeError) as e:
|
| 62 |
+
# Fallback: tokenize the content directly if chat template fails
|
| 63 |
+
if "chat_template" in str(e).lower():
|
| 64 |
+
role_prefix = f"{system_role}: " if system_role else ""
|
| 65 |
+
text_to_tokenize = f"{role_prefix}{system_prompt}"
|
| 66 |
+
system_tokens = self.tokenizer.encode(text_to_tokenize, return_tensors="pt")
|
| 67 |
+
else:
|
| 68 |
+
raise e
|
| 69 |
self._state.history_tokens.append(system_tokens)
|
| 70 |
|
| 71 |
def reset(self) -> ChatObservation:
|
|
|
|
| 83 |
}
|
| 84 |
self._state.history_messages = [system_message]
|
| 85 |
# Tokenize the system message
|
| 86 |
+
try:
|
| 87 |
+
system_tokens = self.tokenizer.apply_chat_template(
|
| 88 |
+
conversation=[system_message], tokenize=True, return_tensors="pt" # type: ignore
|
| 89 |
+
)
|
| 90 |
+
except (ValueError, AttributeError) as e:
|
| 91 |
+
# Fallback: tokenize the content directly if chat template fails
|
| 92 |
+
if "chat_template" in str(e).lower():
|
| 93 |
+
role_prefix = f"{self.system_role}: " if self.system_role else ""
|
| 94 |
+
text_to_tokenize = f"{role_prefix}{self.system_prompt}"
|
| 95 |
+
system_tokens = self.tokenizer.encode(text_to_tokenize, return_tensors="pt")
|
| 96 |
+
else:
|
| 97 |
+
raise e
|
| 98 |
self._state.history_tokens = [system_tokens]
|
| 99 |
|
| 100 |
return self._create_observation()
|
|
|
|
| 182 |
if message["content"] is None:
|
| 183 |
raise ValueError("Message content cannot be None")
|
| 184 |
|
| 185 |
+
# Try to use chat template, fallback to direct tokenization if not available
|
| 186 |
+
try:
|
| 187 |
+
tokens = self.tokenizer.apply_chat_template(
|
| 188 |
+
conversation=[message], tokenize=True, return_tensors="pt" # type: ignore
|
| 189 |
+
)
|
| 190 |
+
except (ValueError, AttributeError) as e:
|
| 191 |
+
# Fallback: tokenize the content directly if chat template fails
|
| 192 |
+
if "chat_template" in str(e).lower():
|
| 193 |
+
# Simple fallback: just tokenize the content with role prefix
|
| 194 |
+
role_prefix = f"{message['role']}: " if message['role'] else ""
|
| 195 |
+
text_to_tokenize = f"{role_prefix}{message['content']}"
|
| 196 |
+
tokens = self.tokenizer.encode(text_to_tokenize, return_tensors="pt")
|
| 197 |
+
else:
|
| 198 |
+
raise e
|
| 199 |
|
| 200 |
return ChatAction(tokens=tokens)
|