Spaces:
Runtime error
Runtime error
GitLab CI
commited on
Commit
·
0f6f535
1
Parent(s):
9254534
Update game build from GitLab CI
Browse files- server/ActionProcessor.py +38 -30
- server/static/godot/index.pck +0 -0
server/ActionProcessor.py
CHANGED
|
@@ -1,6 +1,6 @@
|
|
| 1 |
from threading import Thread
|
| 2 |
from multiprocessing import Queue
|
| 3 |
-
from typing import Dict, Any
|
| 4 |
import json
|
| 5 |
import re
|
| 6 |
import logging
|
|
@@ -27,56 +27,64 @@ class ActionProcessor(Thread):
|
|
| 27 |
super().__init__()
|
| 28 |
self.text_queue = text_queue
|
| 29 |
self.action_queue = action_queue
|
|
|
|
| 30 |
self.mistral_client = Mistral(api_key=mistral_api_key)
|
| 31 |
self.daemon = True # Thread will exit when main program exits
|
| 32 |
|
| 33 |
-
def
|
| 34 |
"""Get sentiment analysis for input text."""
|
| 35 |
messages = [
|
| 36 |
{
|
| 37 |
"role": "user",
|
| 38 |
-
"content": f"""
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
]
|
|
|
|
| 44 |
response = self.mistral_client.chat.complete(
|
| 45 |
model="mistral-large-latest",
|
| 46 |
messages=messages,
|
| 47 |
)
|
| 48 |
|
| 49 |
-
result = response.choices[0].message.content
|
| 50 |
|
| 51 |
return result.strip()
|
| 52 |
|
| 53 |
def process_text(self, text: str) -> Dict[str, Any] | None:
|
| 54 |
"""Convert text into an action if a complete command is detected."""
|
| 55 |
# Get sentiment first
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
command_patterns = {
|
| 60 |
-
r"(?i)\b(stop|now)\b": "stop",
|
| 61 |
-
r"(?i)\b(come back|get back)\b": "return",
|
| 62 |
-
r"(?i)\b(easy)\b": "slow",
|
| 63 |
-
r"(?i)\b(stop drinking)\b": "pause_liquid",
|
| 64 |
-
r"(?i)\b(stop eating)\b": "pause_solid",
|
| 65 |
-
r"(?i)\b(look at me)\b": "look_at_me",
|
| 66 |
-
r"(?i)\b(look away)\b": "look_away",
|
| 67 |
-
r"(?i)\b(don't do that)\b": "stop",
|
| 68 |
-
}
|
| 69 |
-
|
| 70 |
-
# TODO: Remove this test thing
|
| 71 |
-
if len(text) <= 3:
|
| 72 |
return None
|
| 73 |
-
return {"type": text, "sentiment": sentiment}
|
| 74 |
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
|
|
|
|
|
|
|
|
|
| 80 |
|
| 81 |
return None
|
| 82 |
|
|
|
|
| 1 |
from threading import Thread
|
| 2 |
from multiprocessing import Queue
|
| 3 |
+
from typing import Dict, Any, List
|
| 4 |
import json
|
| 5 |
import re
|
| 6 |
import logging
|
|
|
|
| 27 |
super().__init__()
|
| 28 |
self.text_queue = text_queue
|
| 29 |
self.action_queue = action_queue
|
| 30 |
+
self.text_buffers: List[str] = []
|
| 31 |
self.mistral_client = Mistral(api_key=mistral_api_key)
|
| 32 |
self.daemon = True # Thread will exit when main program exits
|
| 33 |
|
| 34 |
+
def get_sentiment_and_action(self, input_text: str) -> str:
|
| 35 |
"""Get sentiment analysis for input text."""
|
| 36 |
messages = [
|
| 37 |
{
|
| 38 |
"role": "user",
|
| 39 |
+
"content": f"""
|
| 40 |
+
The following transcription is a broken transmission overheard by a baby through a baby walkie-talkie. The transmission contains **fragments** of negative parenting commands, and your task is to reconstruct the most likely intended message.
|
| 41 |
+
|
| 42 |
+
Analyze the provided transcription and follow these steps:
|
| 43 |
+
|
| 44 |
+
1. **Reconstruct the most logical full command** by analyzing the given fragments and arranging them into a coherent order.
|
| 45 |
+
2. **Select the closest matching three-word action** from the following predefined options:
|
| 46 |
+
["drop it", "don't drink", "None", "stay back", "stop touching", "move away"].
|
| 47 |
+
Always choose the most contextually relevant option. If no match is appropriate, select "None."
|
| 48 |
+
3. **Determine the parenting sentiment**, which should always be classified as "negative."
|
| 49 |
+
|
| 50 |
+
Your response should strictly follow this JSON format:
|
| 51 |
+
|
| 52 |
+
{{
|
| 53 |
+
"RECONSTRUCTED_ORDER": "[reconstructed text]",
|
| 54 |
+
"ACTION": "[chosen action]",
|
| 55 |
+
"SENTIMENT": "negative"
|
| 56 |
+
}}
|
| 57 |
+
|
| 58 |
+
Transcription fragments: "{input_text}"
|
| 59 |
+
""",
|
| 60 |
+
}
|
| 61 |
]
|
| 62 |
+
|
| 63 |
response = self.mistral_client.chat.complete(
|
| 64 |
model="mistral-large-latest",
|
| 65 |
messages=messages,
|
| 66 |
)
|
| 67 |
|
| 68 |
+
result: str = response.choices[0].message.content
|
| 69 |
|
| 70 |
return result.strip()
|
| 71 |
|
| 72 |
def process_text(self, text: str) -> Dict[str, Any] | None:
|
| 73 |
"""Convert text into an action if a complete command is detected."""
|
| 74 |
# Get sentiment first
|
| 75 |
+
self.text_buffers.append(text)
|
| 76 |
+
|
| 77 |
+
if len(self.text_buffers) < 3:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
return None
|
|
|
|
| 79 |
|
| 80 |
+
if len(self.text_buffers) >= 3:
|
| 81 |
+
_ = self.text_buffers.pop(0)
|
| 82 |
+
|
| 83 |
+
candidate = self.text_buffers[-2]
|
| 84 |
+
|
| 85 |
+
if len(self.text_buffers[0]) <= len(candidate) <= len(self.text_buffers[-1]):
|
| 86 |
+
sentiment_and_action = self.get_sentiment_and_action(candidate)
|
| 87 |
+
return {"type": "action", "sentiment": sentiment_and_action}
|
| 88 |
|
| 89 |
return None
|
| 90 |
|
server/static/godot/index.pck
CHANGED
|
Binary files a/server/static/godot/index.pck and b/server/static/godot/index.pck differ
|
|
|