huggingface / unity-api

Apache License 2.0
305 stars 25 forks source link

TextToImage Queries #5

Open 2600th opened 1 year ago

2600th commented 1 year ago

1.) How to pass various parameters like a seed, output resolution, etc? 2.) Caching of responses, is this being worked upon? 3.) Changing the endpoint to https://api-inference.huggingface.co/models/stabilityai/stable-diffusion-2-1 and hitting generate produces the same result for the same string always.

gschian0 commented 11 months ago

I am encountering this as well... I'm trying to go through the api code to see if the seed is locked ... the seed is not provided in the example files. I did convert the 2d example to 3d however :) put this script on a 3d object you want with a text to image texture and build an overlay ui to call the functions.

`using System; using UnityEngine; using System.Collections; using HuggingFace.API;// Replace with the actual namespace for the HuggingFace API

public class ai_cube : MonoBehaviour { public string textToConvert = "a cat in a hat"; // The text you want to convert to an image

void Start()
{
    Debug.Log("starting");
}

public void setText(string Text)
{
    textToConvert = Text;
}

public void onButtonClick()
{
    HuggingFaceAPI.TextToImage(textToConvert, result =>
    {
        // Do something with the result, which in this case is a Texture2D
        Debug.Log(result);

        // Get the Material from the MeshRenderer attached to the same GameObject
        MeshRenderer meshRenderer = GetComponent<MeshRenderer>();

        // Ensure we have a valid MeshRenderer component
        if (meshRenderer != null)
        {
            // Create a new material instance and assign the generated texture to its main texture slot
            Material newMaterial = new Material(meshRenderer.sharedMaterial);
            newMaterial.mainTexture = result;

            // Assign the new material to the MeshRenderer
            meshRenderer.material = newMaterial;
        }
        else
        {
            Debug.LogWarning("MeshRenderer component not found on the GameObject.");
        }
    }, error =>
    {
        // Handle errors
        Debug.LogError(error);
    });
}

} `

pair with a button and a text box with the ui system and trigger the 2 functions :)

StephenHodgson commented 11 months ago

It's important to note that the above example has a memory leak when creating a new material.

It would be better to save the file to disk then load the image using the web request texture loader.

See my implementation in my hugging face api package