Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,9 +1,90 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
from utils.bgg import get_game_details, get_hot_games, get_similar_games_v2, search
|
| 3 |
from utils.wikipedia_tools import search_wiki, summary_wiki, page_wiki, random_wiki
|
| 4 |
from utils.imdb_tools import search_imdb
|
| 5 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
search_bgg = gr.Interface(
|
| 8 |
fn=search,
|
| 9 |
inputs=["text"],
|
|
@@ -36,12 +117,13 @@ recommend_games = gr.Interface(
|
|
| 36 |
description="Get a list of similar games based on a given game ID"
|
| 37 |
)
|
| 38 |
|
| 39 |
-
#
|
| 40 |
wiki_search = gr.Interface(fn=search_wiki, inputs=["text", "text"], outputs="json", title="Wikipedia Search", description="Search Wikipedia and return summaries")
|
| 41 |
wiki_summary = gr.Interface(fn=summary_wiki, inputs=["text", "text"], outputs="text", title="Summary", description="Get a summary of a Wikipedia topic")
|
| 42 |
wiki_page = gr.Interface(fn=page_wiki, inputs=["text", "text"], outputs="text", title="Full Page", description="Get full content of a topic")
|
| 43 |
wiki_random = gr.Interface(fn=random_wiki, inputs=["text"], outputs="text", title="Random Page", description="Get a random Wikipedia article")
|
| 44 |
|
|
|
|
| 45 |
imdb_interface = gr.Interface(
|
| 46 |
fn=search_imdb,
|
| 47 |
inputs="text",
|
|
@@ -50,41 +132,70 @@ imdb_interface = gr.Interface(
|
|
| 50 |
description="Search IMDb for movie details."
|
| 51 |
)
|
| 52 |
|
| 53 |
-
#
|
| 54 |
-
def
|
| 55 |
if not channel_name:
|
| 56 |
return "Please enter a channel name."
|
| 57 |
-
# Using parent=localhost for local development.
|
| 58 |
-
# In production, this needs to match the domain where the app is hosted.
|
| 59 |
html_content = f"""
|
| 60 |
<iframe id="twitch-chat-embed"
|
| 61 |
-
src="https://www.twitch.tv/embed/{channel_name}/chat?parent=localhost"
|
| 62 |
height="500"
|
| 63 |
width="100%">
|
| 64 |
</iframe>
|
| 65 |
"""
|
| 66 |
return html_content
|
| 67 |
|
| 68 |
-
|
| 69 |
-
fn=
|
| 70 |
inputs=gr.Textbox(label="Channel Name", placeholder="e.g., riotgames"),
|
| 71 |
outputs=gr.HTML(label="Twitch Chat"),
|
| 72 |
-
title="Twitch Chat",
|
| 73 |
-
description="
|
| 74 |
)
|
| 75 |
|
| 76 |
-
#
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
|
|
|
|
|
|
|
|
|
| 80 |
)
|
| 81 |
|
| 82 |
-
|
| 83 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
|
| 85 |
-
|
| 86 |
-
#[search_bgg, game_details, hot_games, recommend_games],
|
| 87 |
-
#["Search", "Details", "Hot Games", "Recommend Games"]
|
| 88 |
-
#)
|
| 89 |
|
| 90 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import os
|
| 3 |
+
import asyncio
|
| 4 |
+
import threading
|
| 5 |
+
import collections
|
| 6 |
+
from twitchio.ext import commands
|
| 7 |
+
|
| 8 |
+
# Existing imports
|
| 9 |
from utils.bgg import get_game_details, get_hot_games, get_similar_games_v2, search
|
| 10 |
from utils.wikipedia_tools import search_wiki, summary_wiki, page_wiki, random_wiki
|
| 11 |
from utils.imdb_tools import search_imdb
|
| 12 |
|
| 13 |
+
# ==========================================
|
| 14 |
+
# 1. TWITCH BOT SETUP (Background Service)
|
| 15 |
+
# ==========================================
|
| 16 |
+
|
| 17 |
+
# Configuration (Load from Environment Variables)
|
| 18 |
+
TWITCH_TOKEN = os.environ.get("TWITCH_TOKEN", "oauth:your_token_here")
|
| 19 |
+
TWITCH_CHANNEL = os.environ.get("TWITCH_CHANNEL", "your_channel_name")
|
| 20 |
+
|
| 21 |
+
# Store recent chat messages for the "Read Chat" tool
|
| 22 |
+
chat_history = collections.deque(maxlen=50)
|
| 23 |
+
|
| 24 |
+
class TwitchBot(commands.Bot):
|
| 25 |
+
def __init__(self):
|
| 26 |
+
super().__init__(token=TWITCH_TOKEN, prefix='!', initial_channels=[TWITCH_CHANNEL])
|
| 27 |
+
self.ready_flag = False
|
| 28 |
+
|
| 29 |
+
async def event_ready(self):
|
| 30 |
+
print(f"Twitch Bot Connected as | {self.nick}")
|
| 31 |
+
self.ready_flag = True
|
| 32 |
+
|
| 33 |
+
async def event_message(self, message):
|
| 34 |
+
if message.echo:
|
| 35 |
+
return
|
| 36 |
+
# Store message: [User]: Message Content
|
| 37 |
+
entry = f"[{message.author.name}]: {message.content}"
|
| 38 |
+
chat_history.append(entry)
|
| 39 |
+
await self.handle_commands(message)
|
| 40 |
+
|
| 41 |
+
async def send_to_chat(self, text: str):
|
| 42 |
+
if not self.ready_flag:
|
| 43 |
+
return "Bot is not connected yet."
|
| 44 |
+
|
| 45 |
+
channel = self.get_channel(TWITCH_CHANNEL)
|
| 46 |
+
if channel:
|
| 47 |
+
await channel.send(text)
|
| 48 |
+
chat_history.append(f"[BOT]: {text}")
|
| 49 |
+
return f"Sent to {TWITCH_CHANNEL}: {text}"
|
| 50 |
+
return "Error: Channel not found."
|
| 51 |
+
|
| 52 |
+
# Initialize Bot Global Instance
|
| 53 |
+
bot = TwitchBot()
|
| 54 |
+
|
| 55 |
+
# Function to run the bot in a separate thread so it doesn't block Gradio
|
| 56 |
+
def run_twitch_bot():
|
| 57 |
+
loop = asyncio.new_event_loop()
|
| 58 |
+
asyncio.set_event_loop(loop)
|
| 59 |
+
try:
|
| 60 |
+
loop.run_until_complete(bot.start())
|
| 61 |
+
except Exception as e:
|
| 62 |
+
print(f"Twitch Bot Error: {e}")
|
| 63 |
+
|
| 64 |
+
# Start the bot in a background thread
|
| 65 |
+
bot_thread = threading.Thread(target=run_twitch_bot, daemon=True)
|
| 66 |
+
bot_thread.start()
|
| 67 |
|
| 68 |
+
|
| 69 |
+
# ==========================================
|
| 70 |
+
# 2. GRADIO WRAPPERS FOR TWITCH TOOLS
|
| 71 |
+
# ==========================================
|
| 72 |
+
|
| 73 |
+
async def tool_send_twitch_message(message):
|
| 74 |
+
"""Sends a message to the active Twitch channel."""
|
| 75 |
+
return await bot.send_to_chat(message)
|
| 76 |
+
|
| 77 |
+
def tool_read_chat_history():
|
| 78 |
+
"""Returns the last 50 messages from chat."""
|
| 79 |
+
if not chat_history:
|
| 80 |
+
return "Chat history is empty."
|
| 81 |
+
return "\n".join(chat_history)
|
| 82 |
+
|
| 83 |
+
# ==========================================
|
| 84 |
+
# 3. GRADIO INTERFACES (Existing + New)
|
| 85 |
+
# ==========================================
|
| 86 |
+
|
| 87 |
+
# --- BGG Interfaces ---
|
| 88 |
search_bgg = gr.Interface(
|
| 89 |
fn=search,
|
| 90 |
inputs=["text"],
|
|
|
|
| 117 |
description="Get a list of similar games based on a given game ID"
|
| 118 |
)
|
| 119 |
|
| 120 |
+
# --- Wikipedia Interfaces ---
|
| 121 |
wiki_search = gr.Interface(fn=search_wiki, inputs=["text", "text"], outputs="json", title="Wikipedia Search", description="Search Wikipedia and return summaries")
|
| 122 |
wiki_summary = gr.Interface(fn=summary_wiki, inputs=["text", "text"], outputs="text", title="Summary", description="Get a summary of a Wikipedia topic")
|
| 123 |
wiki_page = gr.Interface(fn=page_wiki, inputs=["text", "text"], outputs="text", title="Full Page", description="Get full content of a topic")
|
| 124 |
wiki_random = gr.Interface(fn=random_wiki, inputs=["text"], outputs="text", title="Random Page", description="Get a random Wikipedia article")
|
| 125 |
|
| 126 |
+
# --- IMDb Interface ---
|
| 127 |
imdb_interface = gr.Interface(
|
| 128 |
fn=search_imdb,
|
| 129 |
inputs="text",
|
|
|
|
| 132 |
description="Search IMDb for movie details."
|
| 133 |
)
|
| 134 |
|
| 135 |
+
# --- Twitch UI Interface (The Visual Iframe) ---
|
| 136 |
+
def get_twitch_chat_embed(channel_name):
|
| 137 |
if not channel_name:
|
| 138 |
return "Please enter a channel name."
|
|
|
|
|
|
|
| 139 |
html_content = f"""
|
| 140 |
<iframe id="twitch-chat-embed"
|
| 141 |
+
src="https://www.twitch.tv/embed/{channel_name}/chat?parent=localhost&parent=127.0.0.1"
|
| 142 |
height="500"
|
| 143 |
width="100%">
|
| 144 |
</iframe>
|
| 145 |
"""
|
| 146 |
return html_content
|
| 147 |
|
| 148 |
+
twitch_ui_interface = gr.Interface(
|
| 149 |
+
fn=get_twitch_chat_embed,
|
| 150 |
inputs=gr.Textbox(label="Channel Name", placeholder="e.g., riotgames"),
|
| 151 |
outputs=gr.HTML(label="Twitch Chat"),
|
| 152 |
+
title="Twitch Chat UI",
|
| 153 |
+
description="Visual view of Twitch chat (HTML Embed)."
|
| 154 |
)
|
| 155 |
|
| 156 |
+
# --- Twitch MCP Interfaces (The AI Tools) ---
|
| 157 |
+
twitch_send_interface = gr.Interface(
|
| 158 |
+
fn=tool_send_twitch_message,
|
| 159 |
+
inputs=gr.Textbox(label="Message"),
|
| 160 |
+
outputs="text",
|
| 161 |
+
title="Send Twitch Message",
|
| 162 |
+
description="Send a message to the connected Twitch chat channel."
|
| 163 |
)
|
| 164 |
|
| 165 |
+
twitch_read_interface = gr.Interface(
|
| 166 |
+
fn=tool_read_chat_history,
|
| 167 |
+
inputs=None, # No inputs needed
|
| 168 |
+
outputs="text",
|
| 169 |
+
title="Read Twitch Chat",
|
| 170 |
+
description="Read the last 50 messages from the Twitch chat."
|
| 171 |
+
)
|
| 172 |
+
|
| 173 |
+
# ==========================================
|
| 174 |
+
# 4. COMBINE AND LAUNCH
|
| 175 |
+
# ==========================================
|
| 176 |
+
|
| 177 |
+
tool_list = [
|
| 178 |
+
search_bgg, game_details, hot_games, recommend_games,
|
| 179 |
+
wiki_search, wiki_summary, wiki_page, wiki_random,
|
| 180 |
+
imdb_interface,
|
| 181 |
+
twitch_ui_interface, # Visual only
|
| 182 |
+
twitch_send_interface, # MCP Tool
|
| 183 |
+
twitch_read_interface # MCP Tool
|
| 184 |
+
]
|
| 185 |
+
|
| 186 |
+
tab_names = [
|
| 187 |
+
"Search BGG", "Details", "Hot Games", "Recommend",
|
| 188 |
+
"Wiki Search", "Wiki Summary", "Wiki Page", "Wiki Random",
|
| 189 |
+
"IMDb Search",
|
| 190 |
+
"Twitch UI",
|
| 191 |
+
"Send Twitch Msg",
|
| 192 |
+
"Read Twitch Chat"
|
| 193 |
+
]
|
| 194 |
|
| 195 |
+
combined_tools = gr.TabbedInterface(tool_list, tab_names)
|
|
|
|
|
|
|
|
|
|
| 196 |
|
| 197 |
+
# Launch MCP-enabled Gradio Server
|
| 198 |
+
if __name__ == "__main__":
|
| 199 |
+
print("Starting Gradio MCP Server...")
|
| 200 |
+
# NOTE: Set TWITCH_TOKEN and TWITCH_CHANNEL env vars before running!
|
| 201 |
+
combined_tools.launch(mcp_server=True)
|