Skip to main content

Python package for SpeedAI document and text processing API

Project description

SpeedAI Python Package

Official Python package for SpeedAI document and text processing API.

Installation

Install from PyPI

pip install speedai

Quick Start

from speedai import SpeedAIClient, Language, Platform, ProcessingMode

# Initialize client
client = SpeedAIClient(
    api_key="your_api_key",
    token="your_token"
)

# Rewrite text
result = client.rewrite_text(
    text="您的文本内容",
    language=Language.CHINESE,
    platform=Platform.ZHIWANG
)
print(f"Rewritten: {result.processed}")

# Process document
output_path = client.process_document(
    file_path="document.docx",
    output_path="processed_document.docx",
    mode=ProcessingMode.REWRITE,
    progress_callback=lambda p, s: print(f"Progress: {p}% - {s}")
)

Features

  • 🚀 Simple and intuitive API
  • 📄 Support for .doc and .docx files
  • 🌏 Multiple language support (Chinese/English)
  • 🎯 Multiple platform targeting (知网/维普/格子达)
  • ⚡ Automatic status polling for document processing
  • 🔄 Progress tracking with callbacks
  • 🛡️ Comprehensive error handling
  • 🔌 Context manager support

API Reference

Client Initialization

from speedai import SpeedAIClient

client = SpeedAIClient(
    api_key="your_api_key",    # Required
    token="your_token",         # Required
    base_url="https://api3.speedai.chat",  # Optional
    timeout=60                  # Optional, in seconds
)

Text Processing

Rewrite Text (降重)

from speedai import Language, Platform

result = client.rewrite_text(
    text="需要降重的文本",
    language=Language.CHINESE,
    platform=Platform.ZHIWANG
)

print(f"Original: {result.original}")
print(f"Processed: {result.processed}")
print(f"Characters: {result.characters}")

Reduce AI Detection (降AIGC)

result = client.deai_text(
    text="AI generated text",
    language=Language.ENGLISH,
    platform=Platform.WEIPU
)

Document Processing

Simple Processing (with auto-polling)

from speedai import ProcessingMode

# Process document with automatic polling
output_path = client.process_document(
    file_path="input.docx",
    output_path="output.docx",
    mode=ProcessingMode.REWRITE,
    platform=Platform.ZHIWANG,
    skip_english=True,
    progress_callback=lambda progress, status: print(f"{progress}% - {status}")
)

Manual Processing (step by step)

# 1. Upload document
upload_result = client.upload_document(
    file_path="document.docx",
    mode=ProcessingMode.DEAI,
    platform=Platform.GEZIDA
)
document_id = upload_result.document_id

# 2. Check status
import time
while True:
    status = client.check_document_status(document_id)
    print(f"Progress: {status.progress}%")
    
    if status.status == ProcessingStatus.COMPLETED:
        break
    elif status.status == ProcessingStatus.ERROR:
        print(f"Error: {status.error}")
        break
    
    time.sleep(2)

# 3. Download processed document
if status.status == ProcessingStatus.COMPLETED:
    output_path = client.download_document(
        document_id=document_id,
        output_path="processed.docx"
    )

Using Enums

The SDK provides enums for better type safety:

from speedai import Language, Platform, ProcessingMode, ProcessingStatus

# Languages
Language.CHINESE  # "Chinese"
Language.ENGLISH  # "English"

# Platforms
Platform.ZHIWANG  # "zhiwang" - 知网
Platform.WEIPU    # "weipu" - 维普
Platform.GEZIDA   # "gezida" - 格子达

# Processing Modes
ProcessingMode.REWRITE  # "rewrite" - 降重
ProcessingMode.DEAI     # "deai" - 降AIGC

# Processing Status
ProcessingStatus.PROCESSING  # Document is being processed
ProcessingStatus.COMPLETED   # Processing completed
ProcessingStatus.ERROR       # Processing failed

Context Manager

The client supports context manager for automatic resource cleanup:

with SpeedAIClient(api_key="key", token="token") as client:
    result = client.rewrite_text("text")
    # Session is automatically closed when exiting the context

Error Handling

The SDK provides specific exception types:

from speedai import (
    SpeedAIError,
    AuthenticationError,
    ValidationError,
    ProcessingError,
    NetworkError
)

try:
    result = client.rewrite_text("")
except ValidationError as e:
    print(f"Invalid input: {e}")
except ProcessingError as e:
    print(f"Processing failed: {e}")
except NetworkError as e:
    print(f"Network error: {e}")
except SpeedAIError as e:
    print(f"General error: {e}")

Examples

Batch Text Processing

texts = [
    "第一段文本",
    "第二段文本",
    "第三段文本"
]

for text in texts:
    try:
        result = client.rewrite_text(text)
        print(f"Processed: {result.processed}")
    except Exception as e:
        print(f"Error processing text: {e}")

Processing Multiple Documents

import os
from pathlib import Path

input_dir = Path("documents")
output_dir = Path("processed")
output_dir.mkdir(exist_ok=True)

for doc_path in input_dir.glob("*.docx"):
    print(f"Processing {doc_path.name}...")
    
    try:
        output_path = output_dir / f"processed_{doc_path.name}"
        client.process_document(
            file_path=str(doc_path),
            output_path=str(output_path),
            progress_callback=lambda p, s: print(f"  {p}% - {s}")
        )
        print(f"  Saved to: {output_path}")
    except Exception as e:
        print(f"  Error: {e}")

Custom Progress Tracking

from datetime import datetime

def progress_tracker(progress, status):
    timestamp = datetime.now().strftime("%H:%M:%S")
    print(f"[{timestamp}] Progress: {progress}% - Status: {status}")

output_path = client.process_document(
    file_path="large_document.docx",
    output_path="processed_large.docx",
    progress_callback=progress_tracker,
    poll_interval=3,  # Check every 3 seconds
    max_wait_time=600  # Max 10 minutes
)

Environment Variables

You can use environment variables for credentials:

import os
from speedai import SpeedAIClient

client = SpeedAIClient(
    api_key=os.getenv("SPEEDAI_API_KEY"),
    token=os.getenv("SPEEDAI_TOKEN")
)

Rate Limiting and Best Practices

  1. Batch Processing: Process multiple texts in sequence, not in parallel
  2. Error Handling: Always handle exceptions appropriately
  3. Resource Management: Use context managers or close sessions explicitly
  4. Timeouts: Adjust timeout values for large documents
  5. Progress Tracking: Implement callbacks for better user experience

Requirements

  • Python 3.6 or higher
  • requests >= 2.25.0
  • aiohttp >= 3.8.0 (for async support)
  • typing-extensions >= 4.0.0

Development

Running Tests

pip install -e .[dev]
pytest

Code Formatting

black speedai
flake8 speedai

License

MIT License

Support

For issues and feature requests, please visit:

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

speedai-1.0.0.tar.gz (30.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

speedai-1.0.0-py3-none-any.whl (20.8 kB view details)

Uploaded Python 3

File details

Details for the file speedai-1.0.0.tar.gz.

File metadata

  • Download URL: speedai-1.0.0.tar.gz
  • Upload date:
  • Size: 30.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.9.23

File hashes

Hashes for speedai-1.0.0.tar.gz
Algorithm Hash digest
SHA256 71f480f0ea6ccbae8e43d6ce26a6947b19601fdff247bbea3b5da7869cabef12
MD5 e6919ca2e3deffd3cb9dd9047703577e
BLAKE2b-256 f055003c7db9a7e641bd87a7236cb2a1a0262cfde264b290075dcf517b9b39bd

See more details on using hashes here.

File details

Details for the file speedai-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: speedai-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 20.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.9.23

File hashes

Hashes for speedai-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 6ea9f7ee689d489772483bd203a9486c0dea5d05c8b6db3f1ef26c3203f09c8a
MD5 fccbc12180c3286966c288690ee6f5d2
BLAKE2b-256 0bd5997770dce9f9f90d7671fcc589773fb0365a567a9c7fe7f18874cebf3734

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page