Spaces:
Sleeping
Sleeping
Update server.py
Browse files1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465from fastmcp import FastMCPimport loggingimport sysfrom pathlib import Pathimport os# Configure logging to stderrlogging.basicConfig(level=logging.INFO,stream=sys.stderr,format='%(asctime)s [%(name)s] [%(levelname)s] %(message)s')logger = logging.getLogger("MathServer")# Initialize FastMCP servermcp = FastMCP("Math-Education-Server")# Use Path for cross-platform compatibilitycurrent_dir = Path(__file__).parentMARKDOWN_FILE = current_dir / "resource" / "jemh114-min (1).md"def get_local_resource():"""Helper to read the markdown file safely."""try:return MARKDOWN_FILE.read_text(encoding="utf-8")except FileNotFoundError:logger.error(f"Resource not found at {MARKDOWN_FILE}")return "Resource file not found."@mcp.tool()async def generate_chapter_summary(chapter_name: str) -> str:"""Generate a summary of a math chapter."""raw_data = get_local_resource()return f"""<source_material>{raw_data[:4000]}</source_material>SYSTEM_INSTRUCTION: You are a Mathematics Expert.1. Read the text inside <source_material>.2. Create a summary with 3 bullet points for chapter: {chapter_name}3. List all formulas found in LaTeX format.4. Do not repeat the raw text; only provide the summary."""@mcp.tool()async def generate_quiz(chapter_name: str, difficulty: str = "medium") -> str:"""Generate a quiz for a math chapter."""raw_data = get_local_resource()return (f"Here is the raw material for {chapter_name}:\n"f"{raw_data[:4000]}\n"f"Difficulty: {difficulty}\n""Task: Create a 3-question quiz using the formulas found in the text.")if __name__ == "__main__":port = int(os.getenv("PORT", 7860))host = os.getenv("HOST", "0.0.0.0")logger.info(f"Starting FastMCP Server via SSE on {host}:{port}")# Use SSE transport for Hugging Face Spaces# FastMCP handles host validation automaticallymcp.run(transport="stdio")12345678910111213141516171819202122232425262728293031323334353637383940from fastmcp import FastMCPimport loggingimport os# Initialize the servermcp = FastMCP("Math-Education-Server")# SENIOR TIP: Use absolute paths relative to the script location# This prevents "File Not Found" errors in Docker.CURRENT_DIR = os.path.dirname(os.path.abspath(__file__))MARKDOWN_FILE = os.path.join(CURRENT_DIR, "resource", "jemh114-min (1).md")def get_local_resource():if os.path.exists(MARKDOWN_FILE):with open(MARKDOWN_FILE, "r", encoding="utf-8") as f:return f.read()return "Resource file not found."@mcp.tool()async def generate_chapter_summary(chapter_name: str) -> str:"""Provides source material and instructions for a math summary."""raw_data = get_local_resource()return f"""<source_material>{raw_data[:4000]}</source_material>SYSTEM_INSTRUCTION: You are a Mathematics Expert.1. Summarize in 3 bullets.2. List formulas like $x = \\frac{{-b \\pm \\sqrt{{b^2-4ac}}}}{{2a}}$."""@mcp.tool()async def generate_quiz(chapter_name: str, difficulty: str = "medium") -> str:raw_data = get_local_resource()return f"Material: {raw_data[:2000]}\nDifficulty: {difficulty}\nCreate a 3-question quiz."if __name__ == "__main__":# Change stdio to sse for Hugging Face deployment# HF Spaces listen on port 7860 by defaultmcp.run(transport="sse")
server.py+16-41