inventwithdean
commited on
Commit
·
cac18da
1
Parent(s):
07adff1
add fastapi
Browse files- __pycache__/host.cpython-310.pyc +0 -0
- __pycache__/yt_chat.cpython-310.pyc +0 -0
- app.py +16 -6
- yt_chat.py +62 -77
__pycache__/host.cpython-310.pyc
CHANGED
|
Binary files a/__pycache__/host.cpython-310.pyc and b/__pycache__/host.cpython-310.pyc differ
|
|
|
__pycache__/yt_chat.cpython-310.pyc
CHANGED
|
Binary files a/__pycache__/yt_chat.cpython-310.pyc and b/__pycache__/yt_chat.cpython-310.pyc differ
|
|
|
app.py
CHANGED
|
@@ -13,6 +13,8 @@ from timing import TimeManager
|
|
| 13 |
from validity import is_valid_rpm_url
|
| 14 |
from yt_chat import StreamChatHost
|
| 15 |
from prompts import system_prompt_tv_crew_guest, system_prompt_tv_crew_chat
|
|
|
|
|
|
|
| 16 |
|
| 17 |
# Remove these before uploading on Spaces
|
| 18 |
# import dotenv
|
|
@@ -50,13 +52,20 @@ streamChatHost = StreamChatHost(client, tv_crew_chat, guardian)
|
|
| 50 |
thread_time_manager = threading.Thread(target=timeManager.guest_time_limit, daemon=True)
|
| 51 |
thread_time_manager.start()
|
| 52 |
|
| 53 |
-
|
| 54 |
-
target=streamChatHost.chat_interaction_loop, daemon=True
|
| 55 |
-
)
|
| 56 |
-
thread_stream_chat.start()
|
| 57 |
|
|
|
|
| 58 |
|
| 59 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
|
| 61 |
|
| 62 |
def join_show(avatar_url):
|
|
@@ -327,4 +336,5 @@ src="https://www.youtube.com/embed/xBVJIJQg1FY?si=tsCm1DdajNCP45ho" title="YouTu
|
|
| 327 |
|
| 328 |
|
| 329 |
if __name__ == "__main__":
|
| 330 |
-
|
|
|
|
|
|
| 13 |
from validity import is_valid_rpm_url
|
| 14 |
from yt_chat import StreamChatHost
|
| 15 |
from prompts import system_prompt_tv_crew_guest, system_prompt_tv_crew_chat
|
| 16 |
+
from fastapi import FastAPI, Request
|
| 17 |
+
import uvicorn
|
| 18 |
|
| 19 |
# Remove these before uploading on Spaces
|
| 20 |
# import dotenv
|
|
|
|
| 52 |
thread_time_manager = threading.Thread(target=timeManager.guest_time_limit, daemon=True)
|
| 53 |
thread_time_manager.start()
|
| 54 |
|
| 55 |
+
in_construction = False
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
+
app = FastAPI()
|
| 58 |
|
| 59 |
+
|
| 60 |
+
@app.post("/receive_chat")
|
| 61 |
+
async def receive_chat(request: Request):
|
| 62 |
+
api_key = request.headers.get("x-api-key", None)
|
| 63 |
+
if api_key != api_key_unreal:
|
| 64 |
+
return {"status": "forbidden"}
|
| 65 |
+
data = await request.json()
|
| 66 |
+
user_message = f"{data['user']} - {data['message']}"
|
| 67 |
+
streamChatHost.reply_to_chat(user_message)
|
| 68 |
+
return {"status": "received"}
|
| 69 |
|
| 70 |
|
| 71 |
def join_show(avatar_url):
|
|
|
|
| 336 |
|
| 337 |
|
| 338 |
if __name__ == "__main__":
|
| 339 |
+
app = gr.mount_gradio_app(app, demo, path="/", mcp_server=True)
|
| 340 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|
yt_chat.py
CHANGED
|
@@ -5,14 +5,12 @@ from guard import Guardian
|
|
| 5 |
from tv_crew import TVCrew
|
| 6 |
import requests
|
| 7 |
import time
|
| 8 |
-
import pytchat
|
| 9 |
|
| 10 |
# load_dotenv()
|
| 11 |
unreal_orchestrator_url = os.getenv("ORCHESTRATOR_URL")
|
| 12 |
api_key_unreal = os.getenv("API_KEY_UNREAL")
|
| 13 |
|
| 14 |
API_KEY = os.getenv("YOUTUBE_API_KEY")
|
| 15 |
-
POLL_INTERVAL = 3 # seconds
|
| 16 |
|
| 17 |
|
| 18 |
class StreamChatHost:
|
|
@@ -31,87 +29,74 @@ Keep your responses short, like two or three sentences, and be witty."""
|
|
| 31 |
self.messages = [self.system_message]
|
| 32 |
self.headers = {"Content-Type": "application/json", "x-api-key": api_key_unreal}
|
| 33 |
VIDEO_ID = "xBVJIJQg1FY"
|
| 34 |
-
try:
|
| 35 |
-
self.chat = pytchat.create(video_id=VIDEO_ID)
|
| 36 |
-
except Exception as e:
|
| 37 |
-
print(f"Error creating pytchat object: {e}")
|
| 38 |
-
self.chat = None
|
| 39 |
|
| 40 |
-
def
|
| 41 |
"""Checks if the Host is in lobby, then pulls chat messages from the Live Stream. The Host then replies to the message."""
|
| 42 |
-
|
| 43 |
-
|
|
|
|
|
|
|
|
|
|
| 44 |
return
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
f"{unreal_orchestrator_url}/status", headers=self.headers
|
| 49 |
-
)
|
| 50 |
-
in_lobby = request.json().get("in_lobby", False)
|
| 51 |
-
if not in_lobby:
|
| 52 |
-
continue
|
| 53 |
-
if len(self.messages) > 20:
|
| 54 |
-
self.messages = [self.system_message]
|
| 55 |
-
self.tv_crew.clear_context()
|
| 56 |
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
is_safe, _ = self.guardian.moderate_message(user_message)
|
| 61 |
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
# print(f"Error generating host response: {e}")
|
| 91 |
-
continue
|
| 92 |
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
|
|
|
| 5 |
from tv_crew import TVCrew
|
| 6 |
import requests
|
| 7 |
import time
|
|
|
|
| 8 |
|
| 9 |
# load_dotenv()
|
| 10 |
unreal_orchestrator_url = os.getenv("ORCHESTRATOR_URL")
|
| 11 |
api_key_unreal = os.getenv("API_KEY_UNREAL")
|
| 12 |
|
| 13 |
API_KEY = os.getenv("YOUTUBE_API_KEY")
|
|
|
|
| 14 |
|
| 15 |
|
| 16 |
class StreamChatHost:
|
|
|
|
| 29 |
self.messages = [self.system_message]
|
| 30 |
self.headers = {"Content-Type": "application/json", "x-api-key": api_key_unreal}
|
| 31 |
VIDEO_ID = "xBVJIJQg1FY"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
+
def reply_to_chat(self, user_message):
|
| 34 |
"""Checks if the Host is in lobby, then pulls chat messages from the Live Stream. The Host then replies to the message."""
|
| 35 |
+
request = requests.get(
|
| 36 |
+
f"{unreal_orchestrator_url}/status", headers=self.headers
|
| 37 |
+
)
|
| 38 |
+
in_lobby = request.json().get("in_lobby", False)
|
| 39 |
+
if not in_lobby:
|
| 40 |
return
|
| 41 |
+
if len(self.messages) > 20:
|
| 42 |
+
self.messages = [self.system_message]
|
| 43 |
+
self.tv_crew.clear_context()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
|
| 45 |
+
# print(user_message)
|
| 46 |
+
# print("Checking message safety...")
|
| 47 |
+
is_safe, _ = self.guardian.moderate_message(user_message)
|
|
|
|
| 48 |
|
| 49 |
+
if not is_safe:
|
| 50 |
+
# print("Message flagged by Guardian, skipping...")
|
| 51 |
+
return
|
| 52 |
|
| 53 |
+
self.tv_crew.add_context(user_message)
|
| 54 |
+
# print("TV Crew suggesting image...")
|
| 55 |
+
image_base64, caption = self.tv_crew.suggest_image()
|
| 56 |
+
if image_base64:
|
| 57 |
+
# print("Checking image caption safety...")
|
| 58 |
+
is_safe, _ = self.guardian.moderate_message(caption)
|
| 59 |
+
if not is_safe:
|
| 60 |
+
# print("Image caption flagged by Guardian, skipping image...")
|
| 61 |
+
image_base64 = None
|
| 62 |
+
caption = None
|
| 63 |
+
else:
|
| 64 |
+
user_message += f"[Crew Updated Television to: {caption}]\n"
|
| 65 |
|
| 66 |
+
self.messages.append({"role": "user", "content": user_message})
|
| 67 |
+
try:
|
| 68 |
+
# print("Generating host response...")
|
| 69 |
+
response = self.client.responses.create(
|
| 70 |
+
model=self.model, input=self.messages
|
| 71 |
+
)
|
| 72 |
+
host_response = response.output_text
|
| 73 |
+
self.messages.append({"role": "assistant", "content": host_response})
|
| 74 |
+
except Exception as e:
|
| 75 |
+
# print(f"Error generating host response: {e}")
|
| 76 |
+
return
|
|
|
|
|
|
|
| 77 |
|
| 78 |
+
self.tv_crew.add_context(f"Host: {host_response}\n")
|
| 79 |
+
# print(f"Host Response: {host_response}")
|
| 80 |
+
# Just to make sure, that we're in the lobby
|
| 81 |
+
request = requests.get(
|
| 82 |
+
f"{unreal_orchestrator_url}/status", headers=self.headers
|
| 83 |
+
)
|
| 84 |
+
in_lobby = request.json().get("in_lobby", False)
|
| 85 |
+
if not in_lobby:
|
| 86 |
+
return
|
| 87 |
|
| 88 |
+
if image_base64:
|
| 89 |
+
# print("Sending Image Unreal Orchestrator...")
|
| 90 |
+
payload_tv = {"base64": image_base64}
|
| 91 |
+
response = requests.post(
|
| 92 |
+
url=f"{unreal_orchestrator_url}/television",
|
| 93 |
+
json=payload_tv,
|
| 94 |
+
headers=self.headers,
|
| 95 |
+
)
|
| 96 |
+
# print("Sending Host response to Unreal Orchestrator...")
|
| 97 |
+
payload = {"text": host_response, "is_host": True}
|
| 98 |
+
response = requests.post(
|
| 99 |
+
url=f"{unreal_orchestrator_url}/tts",
|
| 100 |
+
json=payload,
|
| 101 |
+
headers=self.headers,
|
| 102 |
+
)
|