Spaces:
Sleeping
Sleeping
File size: 8,728 Bytes
e6c6cdf | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 | #!/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()
|