Spaces:
Sleeping
Sleeping
| #!/usr/bin/env python3 | |
| import requests | |
| import json | |
| import argparse | |
| import sys | |
| # API base URL - adjust as needed | |
| API_BASE_URL = "https://wjbmattingly-pyvoynich.hf.space" | |
| def print_section(title): | |
| """Print a section header.""" | |
| print("\n" + "=" * 50) | |
| print(f" {title}") | |
| print("=" * 50) | |
| def direct_api_examples(): | |
| """Example usage with direct API calls.""" | |
| print_section("DIRECT API EXAMPLES") | |
| # Example 1: List available rulesets | |
| print("\n1. List available rulesets:") | |
| response = requests.get(f"{API_BASE_URL}/rulesets") | |
| if response.status_code == 200: | |
| data = response.json() | |
| print(f"Available rulesets: {', '.join(data['rulesets'])}") | |
| else: | |
| print(f"Error: {response.status_code} - {response.text}") | |
| # Example 2: Get ruleset info | |
| print("\n2. Get information about a specific ruleset:") | |
| response = requests.get(f"{API_BASE_URL}/ruleset/STA_Eva_def") | |
| if response.status_code == 200: | |
| data = response.json() | |
| print(f"Ruleset: {data['name']}") | |
| print(f"Description: {data['description']}") | |
| print(f"Entry count: {data['entry_count']}") | |
| print("Sample entries:") | |
| for key, value in data['sample_entries'].items(): | |
| print(f" {key}: {value}") | |
| else: | |
| print(f"Error: {response.status_code} - {response.text}") | |
| # Example 3: Translate with STA_Eva_def (reverse direction) | |
| print("\n3. Translate using STA_Eva_def (reverse direction):") | |
| input_text = "tchor. ckhoiin. daiin. cphchar-" | |
| payload = { | |
| "text": input_text, | |
| "direction": 2 # reverse direction | |
| } | |
| response = requests.post( | |
| f"{API_BASE_URL}/translate/STA_Eva_def", | |
| json=payload | |
| ) | |
| if response.status_code == 200: | |
| data = response.json() | |
| print(f"Input: {data['original_text']}") | |
| print(f"Output: {data['translated_text']}") | |
| print(f"Direction: {data['direction']}") | |
| print(f"Ruleset: {data['ruleset']}") | |
| else: | |
| print(f"Error: {response.status_code} - {response.text}") | |
| # Example 4: Translate with STA_Eva_def (forward direction) | |
| print("\n4. Translate using STA_Eva_def (forward direction):") | |
| input_text = "Q2K1A1C1.U1A3G1.B1A3G1.T1K1A3C1" | |
| payload = { | |
| "text": input_text, | |
| "direction": 1 # forward direction | |
| } | |
| response = requests.post( | |
| f"{API_BASE_URL}/translate/STA_Eva_def", | |
| json=payload | |
| ) | |
| if response.status_code == 200: | |
| data = response.json() | |
| print(f"Input: {data['original_text']}") | |
| print(f"Output: {data['translated_text']}") | |
| print(f"Direction: {data['direction']}") | |
| print(f"Ruleset: {data['ruleset']}") | |
| else: | |
| print(f"Error: {response.status_code} - {response.text}") | |
| # Example 5: Transform text from one ruleset to another | |
| print("\n5. Transform from Curr_Eva_def to Eva_Cuva:") | |
| input_text = "FCRO8D" # Currier format | |
| payload = { | |
| "text": input_text, | |
| "direction": 1 | |
| } | |
| response = requests.post( | |
| f"{API_BASE_URL}/transform/Curr_Eva_def/Eva_Cuva", | |
| json=payload | |
| ) | |
| if response.status_code == 200: | |
| data = response.json() | |
| print(f"Input: {data['original_text']}") | |
| print(f"Output: {data['translated_text']}") | |
| print(f"Direction: {data['direction']}") | |
| print(f"Transformation: {data['ruleset']}") | |
| else: | |
| print(f"Error: {response.status_code} - {response.text}") | |
| # Example 6: Custom ruleset translation | |
| print("\n6. Translate using custom ruleset:") | |
| custom_rules = { | |
| "hello": "world", | |
| "test": "success", | |
| "ai": "assistant" | |
| } | |
| payload = { | |
| "rules": custom_rules, | |
| "direction": 1, | |
| "text": "hello test ai" | |
| } | |
| response = requests.post( | |
| f"{API_BASE_URL}/translate/custom", | |
| json=payload | |
| ) | |
| if response.status_code == 200: | |
| data = response.json() | |
| print(f"Input: {data['original_text']}") | |
| print(f"Output: {data['translated_text']}") | |
| print(f"Direction: {data['direction']}") | |
| print(f"Ruleset: {data['ruleset']}") | |
| else: | |
| print(f"Error: {response.status_code} - {response.text}") | |
| def parse_args(): | |
| """Parse command line arguments.""" | |
| parser = argparse.ArgumentParser(description="Pyvoynich API Example Usage") | |
| subparsers = parser.add_subparsers(dest="command", help="Command to execute") | |
| # List rulesets command | |
| subparsers.add_parser("list", help="List available rulesets") | |
| # Get ruleset info command | |
| info_parser = subparsers.add_parser("info", help="Get information about a ruleset") | |
| info_parser.add_argument("ruleset", help="Name of the ruleset") | |
| # Translate command | |
| translate_parser = subparsers.add_parser("translate", help="Translate text using a ruleset") | |
| translate_parser.add_argument("ruleset", help="Name of the ruleset to use") | |
| translate_parser.add_argument("text", help="Text to translate") | |
| translate_parser.add_argument("--direction", "-d", type=int, choices=[1, 2], | |
| default=1, help="Translation direction (1=forward, 2=reverse)") | |
| # Transform command | |
| transform_parser = subparsers.add_parser("transform", help="Transform text from one ruleset to another") | |
| transform_parser.add_argument("source", help="Source ruleset name") | |
| transform_parser.add_argument("target", help="Target ruleset name") | |
| transform_parser.add_argument("text", help="Text to transform") | |
| transform_parser.add_argument("--direction", "-d", type=int, choices=[1, 2], | |
| default=1, help="Translation direction (1=forward, 2=reverse)") | |
| # Examples command | |
| subparsers.add_parser("examples", help="Run example API usage") | |
| return parser.parse_args() | |
| def main(): | |
| args = parse_args() | |
| if args.command == "list": | |
| response = requests.get(f"{API_BASE_URL}/rulesets") | |
| if response.status_code == 200: | |
| data = response.json() | |
| print("Available rulesets:") | |
| for ruleset in data["rulesets"]: | |
| print(f" - {ruleset}") | |
| else: | |
| print(f"Error: {response.status_code} - {response.text}") | |
| elif args.command == "info": | |
| response = requests.get(f"{API_BASE_URL}/ruleset/{args.ruleset}") | |
| if response.status_code == 200: | |
| data = response.json() | |
| print(f"Ruleset: {data['name']}") | |
| print(f"Description: {data['description']}") | |
| print(f"Entry count: {data['entry_count']}") | |
| print("Sample entries:") | |
| for key, value in data['sample_entries'].items(): | |
| print(f" {key}: {value}") | |
| else: | |
| print(f"Error: {response.status_code} - {response.text}") | |
| elif args.command == "translate": | |
| payload = { | |
| "text": args.text, | |
| "direction": args.direction | |
| } | |
| response = requests.post(f"{API_BASE_URL}/translate/{args.ruleset}", json=payload) | |
| if response.status_code == 200: | |
| data = response.json() | |
| print(f"Input: {data['original_text']}") | |
| print(f"Output: {data['translated_text']}") | |
| print(f"Direction: {data['direction']}") | |
| print(f"Ruleset: {data['ruleset']}") | |
| else: | |
| print(f"Error: {response.status_code} - {response.text}") | |
| elif args.command == "transform": | |
| payload = { | |
| "text": args.text, | |
| "direction": args.direction | |
| } | |
| response = requests.post( | |
| f"{API_BASE_URL}/transform/{args.source}/{args.target}", | |
| json=payload | |
| ) | |
| if response.status_code == 200: | |
| data = response.json() | |
| print(f"Input: {data['original_text']}") | |
| print(f"Output: {data['translated_text']}") | |
| print(f"Direction: {data['direction']}") | |
| print(f"Transformation: {data['ruleset']}") | |
| else: | |
| print(f"Error: {response.status_code} - {response.text}") | |
| elif args.command == "examples": | |
| direct_api_examples() | |
| else: | |
| print("Please provide a command. Run 'python example_usage.py -h' for help.") | |
| if __name__ == "__main__": | |
| # Check if there are command line arguments | |
| if len(sys.argv) > 1: | |
| main() | |
| else: | |
| # If no arguments, run the direct examples | |
| print("\nRunning API examples. Make sure the API server is running.") | |
| print("To start the server, run: uvicorn api:app --reload") | |
| print("\nFor command-line usage, try: python example_usage.py -h") | |
| direct_api_examples() | |