Spaces:
Sleeping
Sleeping
feat: more tools
Browse files
app.py
CHANGED
|
@@ -1,32 +1,64 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import requests
|
| 3 |
import json
|
|
|
|
| 4 |
from typing import Dict, Any, Optional
|
| 5 |
|
| 6 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
"""
|
| 8 |
-
Fetch XKCD comic
|
| 9 |
|
| 10 |
Args:
|
| 11 |
-
comic_id (str): Comic ID number
|
| 12 |
|
| 13 |
Returns:
|
| 14 |
-
str: JSON string containing comic information
|
| 15 |
"""
|
| 16 |
try:
|
| 17 |
-
if comic_id.strip():
|
| 18 |
-
|
| 19 |
-
url = f"https://xkcd.com/{comic_id.strip()}/info.0.json"
|
| 20 |
-
else:
|
| 21 |
-
# Get latest comic
|
| 22 |
-
url = "https://xkcd.com/info.0.json"
|
| 23 |
|
|
|
|
| 24 |
response = requests.get(url, timeout=10)
|
| 25 |
response.raise_for_status()
|
| 26 |
|
| 27 |
comic_data = response.json()
|
| 28 |
|
| 29 |
-
# Format the response nicely
|
| 30 |
formatted_response = {
|
| 31 |
"num": comic_data["num"],
|
| 32 |
"title": comic_data["title"],
|
|
@@ -42,7 +74,52 @@ def get_xkcd_comic(comic_id: str = "") -> str:
|
|
| 42 |
return json.dumps(formatted_response, indent=2)
|
| 43 |
|
| 44 |
except requests.exceptions.RequestException as e:
|
| 45 |
-
return f"Error fetching comic: {str(e)}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
except KeyError as e:
|
| 47 |
return f"Error parsing comic data: Missing field {str(e)}"
|
| 48 |
except Exception as e:
|
|
@@ -104,23 +181,40 @@ def search_xkcd_transcript(search_term: str) -> str:
|
|
| 104 |
except Exception as e:
|
| 105 |
return f"Search error: {str(e)}"
|
| 106 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
# Create Gradio interface
|
| 108 |
with gr.Blocks(title="XKCD MCP Server") as demo:
|
| 109 |
gr.Markdown("# XKCD MCP Server")
|
| 110 |
gr.Markdown("This server provides tools to fetch and search XKCD comics via MCP protocol.")
|
| 111 |
|
| 112 |
with gr.Tab("Get Comic"):
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 118 |
comic_output = gr.Textbox(
|
| 119 |
label="Comic Data (JSON)",
|
| 120 |
lines=15
|
| 121 |
)
|
| 122 |
-
|
| 123 |
-
|
|
|
|
|
|
|
| 124 |
|
| 125 |
with gr.Tab("Search Comics"):
|
| 126 |
search_input = gr.Textbox(
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import requests
|
| 3 |
import json
|
| 4 |
+
import random
|
| 5 |
from typing import Dict, Any, Optional
|
| 6 |
|
| 7 |
+
def get_latest() -> str:
|
| 8 |
+
"""
|
| 9 |
+
Fetch the latest XKCD comic.
|
| 10 |
+
|
| 11 |
+
Returns:
|
| 12 |
+
str: JSON string containing latest comic information
|
| 13 |
+
"""
|
| 14 |
+
try:
|
| 15 |
+
url = "https://xkcd.com/info.0.json"
|
| 16 |
+
response = requests.get(url, timeout=10)
|
| 17 |
+
response.raise_for_status()
|
| 18 |
+
|
| 19 |
+
comic_data = response.json()
|
| 20 |
+
|
| 21 |
+
formatted_response = {
|
| 22 |
+
"num": comic_data["num"],
|
| 23 |
+
"title": comic_data["title"],
|
| 24 |
+
"alt": comic_data["alt"],
|
| 25 |
+
"img": comic_data["img"],
|
| 26 |
+
"year": comic_data["year"],
|
| 27 |
+
"month": comic_data["month"],
|
| 28 |
+
"day": comic_data["day"],
|
| 29 |
+
"transcript": comic_data.get("transcript", ""),
|
| 30 |
+
"safe_title": comic_data["safe_title"]
|
| 31 |
+
}
|
| 32 |
+
|
| 33 |
+
return json.dumps(formatted_response, indent=2)
|
| 34 |
+
|
| 35 |
+
except requests.exceptions.RequestException as e:
|
| 36 |
+
return f"Error fetching latest comic: {str(e)}"
|
| 37 |
+
except KeyError as e:
|
| 38 |
+
return f"Error parsing comic data: Missing field {str(e)}"
|
| 39 |
+
except Exception as e:
|
| 40 |
+
return f"Unexpected error: {str(e)}"
|
| 41 |
+
|
| 42 |
+
def get_comic(comic_id: str) -> str:
|
| 43 |
"""
|
| 44 |
+
Fetch a specific XKCD comic by ID.
|
| 45 |
|
| 46 |
Args:
|
| 47 |
+
comic_id (str): Comic ID number
|
| 48 |
|
| 49 |
Returns:
|
| 50 |
+
str: JSON string containing comic information
|
| 51 |
"""
|
| 52 |
try:
|
| 53 |
+
if not comic_id.strip():
|
| 54 |
+
return "Error: Comic ID is required"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
|
| 56 |
+
url = f"https://xkcd.com/{comic_id.strip()}/info.0.json"
|
| 57 |
response = requests.get(url, timeout=10)
|
| 58 |
response.raise_for_status()
|
| 59 |
|
| 60 |
comic_data = response.json()
|
| 61 |
|
|
|
|
| 62 |
formatted_response = {
|
| 63 |
"num": comic_data["num"],
|
| 64 |
"title": comic_data["title"],
|
|
|
|
| 74 |
return json.dumps(formatted_response, indent=2)
|
| 75 |
|
| 76 |
except requests.exceptions.RequestException as e:
|
| 77 |
+
return f"Error fetching comic {comic_id}: {str(e)}"
|
| 78 |
+
except KeyError as e:
|
| 79 |
+
return f"Error parsing comic data: Missing field {str(e)}"
|
| 80 |
+
except Exception as e:
|
| 81 |
+
return f"Unexpected error: {str(e)}"
|
| 82 |
+
|
| 83 |
+
def get_random() -> str:
|
| 84 |
+
"""
|
| 85 |
+
Fetch a random XKCD comic.
|
| 86 |
+
|
| 87 |
+
Returns:
|
| 88 |
+
str: JSON string containing random comic information
|
| 89 |
+
"""
|
| 90 |
+
try:
|
| 91 |
+
# First get the latest comic to know the range
|
| 92 |
+
latest_response = requests.get("https://xkcd.com/info.0.json", timeout=10)
|
| 93 |
+
latest_response.raise_for_status()
|
| 94 |
+
latest_num = latest_response.json()["num"]
|
| 95 |
+
|
| 96 |
+
# Generate random comic ID (excluding 404 which doesn't exist)
|
| 97 |
+
comic_id = random.randint(1, latest_num)
|
| 98 |
+
if comic_id == 404:
|
| 99 |
+
comic_id = 405
|
| 100 |
+
|
| 101 |
+
url = f"https://xkcd.com/{comic_id}/info.0.json"
|
| 102 |
+
response = requests.get(url, timeout=10)
|
| 103 |
+
response.raise_for_status()
|
| 104 |
+
|
| 105 |
+
comic_data = response.json()
|
| 106 |
+
|
| 107 |
+
formatted_response = {
|
| 108 |
+
"num": comic_data["num"],
|
| 109 |
+
"title": comic_data["title"],
|
| 110 |
+
"alt": comic_data["alt"],
|
| 111 |
+
"img": comic_data["img"],
|
| 112 |
+
"year": comic_data["year"],
|
| 113 |
+
"month": comic_data["month"],
|
| 114 |
+
"day": comic_data["day"],
|
| 115 |
+
"transcript": comic_data.get("transcript", ""),
|
| 116 |
+
"safe_title": comic_data["safe_title"]
|
| 117 |
+
}
|
| 118 |
+
|
| 119 |
+
return json.dumps(formatted_response, indent=2)
|
| 120 |
+
|
| 121 |
+
except requests.exceptions.RequestException as e:
|
| 122 |
+
return f"Error fetching random comic: {str(e)}"
|
| 123 |
except KeyError as e:
|
| 124 |
return f"Error parsing comic data: Missing field {str(e)}"
|
| 125 |
except Exception as e:
|
|
|
|
| 181 |
except Exception as e:
|
| 182 |
return f"Search error: {str(e)}"
|
| 183 |
|
| 184 |
+
# Helper function for the Gradio interface
|
| 185 |
+
def get_xkcd_comic(comic_id: str = "") -> str:
|
| 186 |
+
"""Wrapper function for Gradio interface compatibility"""
|
| 187 |
+
if not comic_id.strip():
|
| 188 |
+
return get_latest()
|
| 189 |
+
else:
|
| 190 |
+
return get_comic(comic_id)
|
| 191 |
+
|
| 192 |
# Create Gradio interface
|
| 193 |
with gr.Blocks(title="XKCD MCP Server") as demo:
|
| 194 |
gr.Markdown("# XKCD MCP Server")
|
| 195 |
gr.Markdown("This server provides tools to fetch and search XKCD comics via MCP protocol.")
|
| 196 |
|
| 197 |
with gr.Tab("Get Comic"):
|
| 198 |
+
with gr.Row():
|
| 199 |
+
with gr.Column():
|
| 200 |
+
latest_btn = gr.Button("Get Latest Comic")
|
| 201 |
+
random_btn = gr.Button("Get Random Comic")
|
| 202 |
+
with gr.Column():
|
| 203 |
+
comic_input = gr.Textbox(
|
| 204 |
+
label="Comic ID",
|
| 205 |
+
placeholder="Enter comic ID number",
|
| 206 |
+
value=""
|
| 207 |
+
)
|
| 208 |
+
specific_btn = gr.Button("Get Specific Comic")
|
| 209 |
+
|
| 210 |
comic_output = gr.Textbox(
|
| 211 |
label="Comic Data (JSON)",
|
| 212 |
lines=15
|
| 213 |
)
|
| 214 |
+
|
| 215 |
+
latest_btn.click(get_latest, outputs=[comic_output])
|
| 216 |
+
random_btn.click(get_random, outputs=[comic_output])
|
| 217 |
+
specific_btn.click(get_comic, inputs=[comic_input], outputs=[comic_output])
|
| 218 |
|
| 219 |
with gr.Tab("Search Comics"):
|
| 220 |
search_input = gr.Textbox(
|