Fraser's picture
monster-piclet
55b1a24
Note the "Sign in with Hugging Face" button that is added with my page, can I just auto
<iframe src="https://fraser-piclets.static.hf.space/index.html?embed=true&amp;__sign=eyJhbGciOiJFZERTQSJ9.eyJyZWFkIjp0cnVlLCJwZXJtaXNzaW9ucyI6eyJyZXBvLmNvbnRlbnQucmVhZCI6dHJ1ZX0sIm9uQmVoYWxmT2YiOnsia2luZCI6InVzZXIiLCJfaWQiOiI1ZjE5NTc4NDkyNWI5ODYzZTI4YWQ2MTAiLCJ1c2VyIjoiRnJhc2VyIiwic2Vzc2lvbklkIjoiNjg3NjU0ZjZhYmI2ZWE2ZTk0OThkNjVmIn0sImlhdCI6MTc1MjY1NzYxMiwic3ViIjoiL3NwYWNlcy9GcmFzZXIvcGljbGV0cyIsImV4cCI6MTc1Mjc0NDAxMiwiaXNzIjoiaHR0cHM6Ly9odWdnaW5nZmFjZS5jbyJ9.vH_qEMDwpCpEapX36n-JPgfj6P7jxGdpwomhT6MIpY-r2OS9Wc1bFsQq0USfbQqKxif2rR9XL7sB8f0ximxCDA" aria-label="static space app" class="space-iframe outline-hidden grow bg-white p-0" allow="accelerometer; ambient-light-sensor; autoplay; battery; camera; clipboard-read; clipboard-write; display-capture; document-domain; encrypted-media; fullscreen; geolocation; gyroscope; layout-animations; legacy-image-formats; magnetometer; microphone; midi; oversized-images; payment; picture-in-picture; publickey-credentials-get; serial; sync-xhr; usb; vr ; wake-lock; xr-spatial-tracking" sandbox="allow-downloads allow-forms allow-modals allow-pointer-lock allow-popups allow-popups-to-escape-sandbox allow-same-origin allow-scripts allow-storage-access-by-user-activation" scrolling="yes" id="iFrameResizer0" style="overflow: auto;"></iframe>
---
Now I would also like the text generator to produce a JSON object based on the monster concept.
This will be created with the following schema:
- name: string
- description: string
- rarity: likert (very-low, low, medium, high, very-high)
- HP: likert
- defence: likert
- attack: likert
- speed: likert
- special ability: string (passive trait that gives the monster a unique advantage in battle)
- attack action description: string (deals damage)
- boost action description: string (buff monsters own stats/status)
- disparage action description: string (lowers enemy stats/status)
- special action description: string (powerful action with single use per battle)
Also update the DB to use this JSON data in its schema.
---
```python
import json
from pydantic import BaseModel
STRUCTURED_OUTPUT_FORMAT_INSTRUCTIONS = """The output should be formatted as a JSON instance that conforms to the JSON schema below.
As an example, for the schema {{"properties": {{"foo": {{"title": "Foo", "description": "a list of strings", "type": "array", "items": {{"type": "string"}}}}}}, "required": ["foo"]}}
the object {{"foo": ["bar", "baz"]}} is a well-formatted instance of the schema. The object {{"properties": {{"foo": ["bar", "baz"]}}}} is not well-formatted.
Here is the output schema:
```
{schema}
```"""
class StructuredOutputParser(BaseModel):
pydantic_class: type[BaseModel]
def get_schema(self, indent: int = 2) -> str:
# Copy schema to avoid altering original Pydantic schema.
schema = self.pydantic_class.model_json_schema()
# Iterate over fields to remove from the schema
for field_name in ["title", "type"]:
if field_name in schema:
schema.pop(field_name)
return json.dumps(schema, indent=indent, ensure_ascii=False)
def get_format_instructions(self) -> str:
schema_str = self.get_schema()
# Ensure json in context is well-formed with double quotes.
return STRUCTURED_OUTPUT_FORMAT_INSTRUCTIONS.format(schema=schema_str)
def parse(self, json_string: str) -> BaseModel:
return self.pydantic_class.model_validate_json(json_string)
```