deepseek-dnd-lora / inference_api_example.py
chendren's picture
Upload inference_api_example.py with huggingface_hub
bc776cd verified
#!/usr/bin/env python3
# Example script for using the Hugging Face Inference API with the D&D model
# This approach will track download metrics on Hugging Face
import requests
import json
import time
# Configuration
API_URL = "https://api-inference.huggingface.co/models/chendren/deepseek-dnd-lora"
headers = {
"Content-Type": "application/json",
# Replace with your Hugging Face API token
"Authorization": "Bearer YOUR_API_TOKEN"
}
# Test prompts
test_prompts = [
"Create a D&D character with the following details: Race: Half-Elf, Class: Bard, Background: Entertainer",
"Design a D&D adventure hook set in a dark forest",
"Create a magical item for D&D 5e that would be suitable for a level 5 rogue",
"Write a description for a fantasy tavern in a D&D setting"
]
def query(payload):
"""
Send a query to the Hugging Face API
"""
response = requests.post(API_URL, headers=headers, json=payload)
if response.status_code == 503:
# Model is loading
print("Model is loading. Waiting...")
time.sleep(20)
return query(payload)
return response.json()
# Run tests
for i, prompt in enumerate(test_prompts):
print(f"\n==== Test Prompt {i+1} ====")
print(prompt)
print("\n==== Response ====")
# Add a system prompt to help guide the model
full_prompt = f"You are a Dungeons & Dragons assistant. {prompt}"
# Make the API request - this will be tracked by HF Hub
payload = {
"inputs": full_prompt,
"parameters": {
"max_new_tokens": 500,
"temperature": 0.7,
"top_p": 0.9,
"top_k": 50,
"repetition_penalty": 1.1,
"do_sample": True
}
}
try:
result = query(payload)
print(json.dumps(result, indent=2))
except Exception as e:
print(f"Error: {e}")
print("\n" + "="*50)
# Wait a bit between requests to avoid rate limiting
time.sleep(3)
print("\nTesting complete!")