TarotDraw / app.py
admin321's picture
mcp mode
fee804f
import gradio as gr
import requests
import random
from typing import Union
# API Configuration
API_BASE_URL = "https://tarotapi.dev/api/v1"
def get_tarot_reading(question: str, num_cards: float) -> str:
"""Get a complete tarot reading"""
if not question or not question.strip():
return "Please enter a question for your tarot reading."
try:
# Convert float to int for API call
card_count = int(num_cards)
# Make API request
response = requests.get(f"{API_BASE_URL}/cards/random?n={card_count}")
if response.status_code != 200:
return f"API Error: Unable to fetch cards (Status: {response.status_code})"
data = response.json()
if 'cards' not in data or not data['cards']:
return "Error: No cards returned from the API"
# Build the reading
reading = "๐Ÿ”ฎ Your Tarot Reading ๐Ÿ”ฎ\n\n"
reading += f"Question: {question}\n\n"
for i, card in enumerate(data['cards'], 1):
name = card.get('name', 'Unknown Card')
orientation = random.choice(['Upright', 'Reversed'])
reading += f"Card {i}: {name} ({orientation})\n"
# Add meaning
if orientation == 'Upright':
meaning = card.get('meaning_up', 'No meaning available')
else:
meaning = card.get('meaning_rev', 'No meaning available')
reading += f"Meaning: {meaning}\n\n"
return reading
except requests.exceptions.RequestException as e:
return f"Network Error: {str(e)}"
except Exception as e:
return f"Error: {str(e)}"
# Create the interface with explicit types
demo = gr.Interface(
fn=get_tarot_reading,
inputs=[
gr.Textbox(
label="What would you like to know?",
placeholder="Enter your question here...",
lines=2,
value=""
),
gr.Slider(
minimum=1,
maximum=5,
value=3,
step=1,
label="Number of cards to draw"
)
],
outputs=gr.Textbox(
label="Your Reading",
lines=15,
value=""
),
title="๐Ÿ”ฎ Tarot MCP Server ๐Ÿ”ฎ",
description="A simple MCP server for tarot readings using the Tarot API",
allow_flagging="never"
)
if __name__ == "__main__":
demo.launch(mcp_server=True)