Mix.install([
{:pythonx, "~> 0.3.0"},
{:kino, "~> 0.14.2"},
{:kino_vega_lite, "~> 0.1.13"}
])
Pythonx.uv_init("""
[project]
name = "project"
version = "0.0.0"
requires-python = "==3.12.*"
dependencies = [
"transformers==4.49.0",
"accelerate==1.4.0"
]
""")
scaling_factor_input = Kino.Input.number("Scaling Factor", default: 42.0)
import Pythonx
# Set the global based on a form input
scaling_factor = Kino.Input.read(scaling_factor_input)
# Use an Elixir global variable in a Python expression
~PY"""
def calculate(x):
b = 100
return scaling_factor * x + b
calculate(5)
"""
|> Pythonx.decode()
prompt_input = Kino.Input.textarea("What's the problem?", default: "I have a problem with my iphone that needs to be resolved asap!!")
import Pythonx
prompt = Kino.Input.read(prompt_input)
result =
~PY"""
from transformers import pipeline
classifier = pipeline(model="facebook/bart-large-mnli")
classifier(
prompt.decode('utf-8'),
candidate_labels=["urgent", "not urgent", "phone", "tablet", "computer"],
)
"""
|> Pythonx.decode()
chart_data =
Enum.zip(result["labels"], result["scores"])
|> Enum.map(fn {label, score} -> %{"label" => label, "score" => score} end)
alias VegaLite, as: Vl
Vl.new(width: 500, height: 300)
|> Vl.data_from_values(chart_data)
|> Vl.mark(:bar)
|> Vl.encode_field(:x, "label", type: :nominal, title: "Labels")
|> Vl.encode_field(:y, "score", type: :quantitative, title: "Scores")
|> Vl.config(bar: [color: "#4C78A8"])