Skip to content

Instantly share code, notes, and snippets.

View simoninithomas's full-sized avatar

Thomas Simonini simoninithomas

View GitHub Profile
@simoninithomas
simoninithomas / JammoBehavior_Tutorial.cs
Created September 16, 2021 09:18
JammoBehavior_Tutorial Part 3
/// <summary>
/// Utility function: Given the results of HuggingFace API, select the State with the highest score
/// </summary>
/// <param name="maxValue">Value of the option with the highest score</param>
/// <param name="maxIndex">Index of the option with the highest score</param>
private void Utility(float maxScore, int maxScoreIndex)
{
// First we check that the score is > of 0.3, otherwise we let our agent perplexed;
// This way we can handle strange input text (for instance if we write "Go see the dog!" the agent will be puzzled).
if (maxScore < 0.20f)
@simoninithomas
simoninithomas / JammoBehavior_Tutorial.cs
Created September 16, 2021 09:17
JammoBehavior_Tutorial Part 2
private void Update()
{
// Here's the State Machine, where given its current state, the agent will act accordingly
switch(state)
{
default:
case State.Idle:
Debug.Log("STATE IDLE");
break;
@simoninithomas
simoninithomas / JammoBehavior_Tutorial.cs
Created September 16, 2021 09:13
JammoBehavior_Tutorial Part 1
/// <summary>
/// Enum of the different possible states of our Robot
/// </summary>
private enum State
{
Idle,
Hello, // Say hello
Happy, // Be happy
Puzzled, // Be Puzzled
MoveTo, // Move to a pillar
@simoninithomas
simoninithomas / HuggingFaceAPI_Tutorial.cs
Created September 16, 2021 08:55
HuggingFaceAPI Part 3
/// <summary>
/// We receive a string like "[0.7777, 0.19, 0.01]", we need to process this data to transform it to an array of floats
/// [0.77, 0.19, 0.01] to be able to perform operations on it.
/// </summary>
/// <param name="result">json return from API call</param>
private IEnumerator ProcessResult(string result)
{
// The data is a score for each possible sentence candidate
// But, it looks something like this "[0.7777, 0.19, 0.01]"
// First, we need to remove [ and ]
@simoninithomas
simoninithomas / HuggingFaceAPI_Tutorial.cs
Created September 16, 2021 08:47
HuggingFace API Part 2
public IEnumerator HFScore(string prompt)
{
...
// Make the web request
UnityWebRequest request = UnityWebRequest.Put(model_url, bytes);
request.SetRequestHeader("Content-Type", "application/json");
request.SetRequestHeader("Authorization", "Bearer " + hf_api_key);
request.method = "POST"; // Hack to send POST to server instead of PUT
@simoninithomas
simoninithomas / HuggingFaceAPI_Tutorial.cs
Created September 16, 2021 08:46
HuggingFace API Part 1
public IEnumerator HFScore(string prompt)
{
// Form the JSON
var form = new Dictionary<string, object>();
var attributes = new Dictionary<string, object>();
attributes["source_sentence"] = prompt;
attributes["sentences"] = sentences;
form["inputs"] = attributes;
var json = Json.Serialize(form);
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
import gym
from stable_baselines.common.policies import MlpPolicy
from stable_baselines.common.vec_env import DummyVecEnv
from stable_baselines import PPO2
# Create the environment
env = gym.make('BipedalWalker-v2')
env = DummyVecEnv([lambda: env]) # The algorithms require a vectorized environment to run
import numpy as np
import gym
from keras.models import Sequential
from keras.layers import Dense, Activation, Flatten
from keras.optimizers import Adam
from rl.agents.dqn import DQNAgent
from rl.policy import BoltzmannQPolicy
from rl.memory import SequentialMemory
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.