Spaces:
Runtime error
Runtime error
File size: 3,623 Bytes
afe2831 79f852c afe2831 79f852c afe2831 bcb1873 afe2831 d26938f 539c396 3ec3001 afe2831 f61c5df ea36138 f61c5df f0beecf f61c5df afe2831 |
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 |
import requests
from transformers import Tool
class RandomCharatorGeneratorTool(Tool):
name = "random_character"
description = "This tool generates a random character. Returns json."
inputs = ["text"] # Adding an empty list for inputs
outputs = ["json"]
def __init__(self, device=None, **hub_kwargs) -> None:
#if not is_accelerate_available():
# raise ImportError("Accelerate should be installed in order to use tools.")
super().__init__()
self.device = device
self.pipeline = None
self.hub_kwargs = hub_kwargs
def setup(self):
#if self.device is None:
# self.device = get_default_device()
#self.pipeline = DiffusionPipeline.from_pretrained(self.default_checkpoint)
#self.pipeline.scheduler = DPMSolverMultistepScheduler.from_config(self.pipeline.scheduler.config)
#self.pipeline.to(self.device)
#if self.device.type == "cuda":
# self.pipeline.to(torch_dtype=torch.float16)
self.is_initialized = True
def __call__(self, input:str="", *args, **kwargs):
if not self.is_initialized:
self.setup()
API_URL = "https://randomuser.me/api/"
response = requests.get(API_URL)
data = response.json()['results'][0]
# Extract the relevant character information
character = {
"gender": data['gender'],
"name": {
"title": data['name']['title'],
"first": data['name']['first'],
"last": data['name']['last']
},
"location": {
"street": {
"number": data['location']['street']['number'],
"name": data['location']['street']['name']
},
"city": data['location']['city'],
"state": data['location']['state'],
"country": data['location']['country'],
"postcode": data['location']['postcode'],
"coordinates": {
"latitude": data['location']['coordinates']['latitude'],
"longitude": data['location']['coordinates']['longitude']
},
"timezone": {
"offset": data['location']['timezone']['offset'],
"description": data['location']['timezone']['description']
}
},
"email": data['email'],
"login": {
"uuid": data['login']['uuid'],
"username": data['login']['username'],
"password": data['login']['password'],
"salt": data['login']['salt'],
"md5": data['login']['md5'],
"sha1": data['login']['sha1'],
"sha256": data['login']['sha256']
},
"dob": {
"date": data['dob']['date'],
"age": data['dob']['age']
},
"registered": {
"date": data['registered']['date'],
"age": data['registered']['age']
},
"phone": data['phone'],
"cell": data['cell'],
"id": {
"name": data['id']['name'],
"value": data['id']['value']
},
"picture": {
"large": data['picture']['large'],
"medium": data['picture']['medium'],
"thumbnail": data['picture']['thumbnail']
},
"nat": data['nat']
}
return {"character": character}
|