|
|
import os |
|
|
|
|
|
|
|
|
from guard import Guardian |
|
|
from tv_crew import TVCrew |
|
|
import requests |
|
|
import time |
|
|
|
|
|
unreal_orchestrator_url = os.getenv("ORCHESTRATOR_URL") |
|
|
api_key_unreal = os.getenv("API_KEY_UNREAL") |
|
|
|
|
|
API_KEY = os.getenv("YOUTUBE_API_KEY") |
|
|
|
|
|
|
|
|
class StreamChatHost: |
|
|
def __init__(self, client, tv_crew: TVCrew, guardian: Guardian): |
|
|
self.client = client |
|
|
self.guardian = guardian |
|
|
self.tv_crew = tv_crew |
|
|
self.model = "deepseek/deepseek-v3.2-exp" |
|
|
system_prompt = """You are the Host of a Talk Show called "The Emergent Show", and youtube live chat is ongoing. |
|
|
There is a TV Display in the room which crew will update as conversation goes and let you know. |
|
|
Keep your responses short, like three or four sentences, and be witty.""" |
|
|
self.system_message = {"role": "system", "content": system_prompt} |
|
|
|
|
|
self.messages = [self.system_message] |
|
|
self.headers = {"Content-Type": "application/json", "x-api-key": api_key_unreal} |
|
|
|
|
|
def reply_to_chat(self, user_message): |
|
|
"""Checks if the Host is in lobby, then pulls chat messages from the Live Stream. The Host then replies to the message.""" |
|
|
request = requests.get( |
|
|
f"{unreal_orchestrator_url}/status", headers=self.headers |
|
|
) |
|
|
in_lobby = request.json().get("in_lobby", False) |
|
|
if not in_lobby: |
|
|
return |
|
|
if len(self.messages) > 20: |
|
|
self.messages = [self.system_message] |
|
|
self.tv_crew.clear_context() |
|
|
|
|
|
|
|
|
|
|
|
is_safe, _ = self.guardian.moderate_message(user_message) |
|
|
|
|
|
if not is_safe: |
|
|
|
|
|
return |
|
|
|
|
|
self.tv_crew.add_context(user_message) |
|
|
|
|
|
image_base64, caption = self.tv_crew.suggest_image() |
|
|
if image_base64: |
|
|
|
|
|
is_safe, _ = self.guardian.moderate_message(caption) |
|
|
if not is_safe: |
|
|
|
|
|
image_base64 = None |
|
|
caption = None |
|
|
else: |
|
|
user_message += f"[Crew Updated Television to: {caption}]\n" |
|
|
|
|
|
self.messages.append({"role": "user", "content": user_message}) |
|
|
try: |
|
|
|
|
|
response = self.client.responses.create( |
|
|
model=self.model, input=self.messages |
|
|
) |
|
|
host_response = response.output_text |
|
|
self.messages.append({"role": "assistant", "content": host_response}) |
|
|
except Exception as e: |
|
|
|
|
|
return |
|
|
|
|
|
self.tv_crew.add_context(f"Host: {host_response}\n") |
|
|
|
|
|
|
|
|
request = requests.get( |
|
|
f"{unreal_orchestrator_url}/status", headers=self.headers |
|
|
) |
|
|
in_lobby = request.json().get("in_lobby", False) |
|
|
if not in_lobby: |
|
|
return |
|
|
|
|
|
if image_base64: |
|
|
|
|
|
payload_tv = {"base64": image_base64} |
|
|
response = requests.post( |
|
|
url=f"{unreal_orchestrator_url}/television", |
|
|
json=payload_tv, |
|
|
headers=self.headers, |
|
|
) |
|
|
|
|
|
payload = {"text": host_response, "is_host": True} |
|
|
response = requests.post( |
|
|
url=f"{unreal_orchestrator_url}/tts", |
|
|
json=payload, |
|
|
headers=self.headers, |
|
|
) |
|
|
|