"""Small, billed end-to-end checks against an already deployed private Space. Requires gradio_client and huggingface_hub. Fixtures are deliberately synthetic. Never reads or prints the OpenRouter API key; the Space uses its Secret. """ import argparse import json from pathlib import Path from gradio_client import Client, handle_file from huggingface_hub import get_token def answer_text(result: tuple) -> str: messages = result[0] if not messages or messages[-1]["role"] != "assistant": return "" content = messages[-1]["content"] if isinstance(content, str): return content return "\n".join(part.get("text", "") for part in content if part.get("type") == "text") def main() -> None: parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("repo_id") parser.add_argument("--fixtures", type=Path) parser.add_argument("--case", choices=["models", "files", "search", "errors"], required=True) args = parser.parse_args() client = Client(args.repo_id, token=get_token(), httpx_kwargs={"timeout": 360}, verbose=False) def chat(text: str, files=None, model="anthropic/claude-sonnet-5.5", search=False, max_tokens=1024): result = client.predict(text, files or [], model, search, max_tokens, "low", api_name="/chat") answer = answer_text(result) print(json.dumps({"model": model, "answer": answer[:2000], "status": result[-1]}, ensure_ascii=False), flush=True) return answer, result if args.case == "models": for model_id in ["anthropic/claude-sonnet-5.5", "anthropic/claude-opus-5.5", "anthropic/claude-fable-5.1", "deepseek/deepseek-v4.1-flash"]: answer, result = chat("This is a connectivity test. Reply with exactly CONNECTED.", model=model_id) assert "CONNECTED" in answer, result[-1] elif args.case == "files": if not args.fixtures: parser.error("--fixtures is required") files = [handle_file(str(args.fixtures / ("sample." + ext))) for ext in ["pdf", "docx", "pptx", "xlsx", "png", "csv"]] answer, result = chat("Read all six attachments. Return the PDF_CODE, WORD_CODE, SLIDE_CODE, SHEET_CODE, CSV_CODE values and the image's dominant color. Be brief.", files=files, max_tokens=2048) for code in ["ORANGE-42", "MAPLE-17", "OCEAN-29", "CEDAR-63", "BIRCH-36"]: assert code in answer, (code, result[-1]) assert "red" in answer.lower() or "红" in answer answer, result = chat("Without uploading again, what was WORD_CODE in the earlier Word attachment? Reply with its value only.") assert "MAPLE-17" in answer, result[-1] elif args.case == "search": answer, result = chat("Use web search now to find the official Hugging Face Spaces overview documentation. State one hosting option and cite its URL. Keep the answer under 100 words.", search=True, max_tokens=2048) assert "huggingface.co" in answer, result[-1] assert "参考来源" in answer or "联网搜索 1" in result[-1], "Need evidence of tool execution, not just a guessed URL" elif args.case == "errors": if not args.fixtures: parser.error("--fixtures is required") answer, result = chat("Read this file", files=[handle_file(str(args.fixtures / "invalid.pdf"))]) assert "无法解析" in result[-1] and not answer print("PASS: " + args.case, flush=True) if __name__ == "__main__": main()