jadehardouin
commited on
Commit
•
14ace35
1
Parent(s):
e04987d
Create contribution_example.py
Browse files- contribution_example.py +99 -0
contribution_example.py
ADDED
@@ -0,0 +1,99 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from gradio.components import Component
|
2 |
+
import gradio as gr
|
3 |
+
from abc import ABC, abstractclassmethod
|
4 |
+
import inspect
|
5 |
+
|
6 |
+
class BaseTCOModel(ABC):
|
7 |
+
# TO DO: Find way to specify which component should be used for computing cost
|
8 |
+
def __setattr__(self, name, value):
|
9 |
+
if isinstance(value, Component):
|
10 |
+
self._components.append(value)
|
11 |
+
self.__dict__[name] = value
|
12 |
+
|
13 |
+
def __init__(self):
|
14 |
+
super(BaseTCOModel, self).__setattr__("_components", [])
|
15 |
+
|
16 |
+
def get_components(self) -> list[Component]:
|
17 |
+
return self._components
|
18 |
+
|
19 |
+
def get_components_for_cost_computing(self):
|
20 |
+
return self.components_for_cost_computing
|
21 |
+
|
22 |
+
def get_name(self):
|
23 |
+
return self.name
|
24 |
+
|
25 |
+
def register_components_for_cost_computing(self):
|
26 |
+
args = inspect.getfullargspec(self.compute_cost_per_token)[0][1:]
|
27 |
+
self.components_for_cost_computing = [self.__getattribute__(arg) for arg in args]
|
28 |
+
|
29 |
+
@abstractclassmethod
|
30 |
+
def compute_cost_per_token(self):
|
31 |
+
pass
|
32 |
+
|
33 |
+
@abstractclassmethod
|
34 |
+
def render(self):
|
35 |
+
pass
|
36 |
+
|
37 |
+
def set_name(self, name):
|
38 |
+
self.name = name
|
39 |
+
|
40 |
+
def set_formula(self, formula):
|
41 |
+
self.formula = formula
|
42 |
+
|
43 |
+
def get_formula(self):
|
44 |
+
return self.formula
|
45 |
+
|
46 |
+
#The name of your new model service's class
|
47 |
+
class NewModel(BaseTCOModel):
|
48 |
+
|
49 |
+
def __init__(self):
|
50 |
+
#Name of the AI model service and the category it belongs to (SaaS, Open source)
|
51 |
+
self.set_name("(Category) Service name")
|
52 |
+
self.set_latency("The average latency of your model")
|
53 |
+
super().__init__()
|
54 |
+
|
55 |
+
def render(self):
|
56 |
+
#Create update functions that adjust the values of your cost/token depending on user's choices
|
57 |
+
def on_model_parameter_change(model_parameter):
|
58 |
+
if model_parameter == "Option 1":
|
59 |
+
input_tokens_cost_per_token = 0.1
|
60 |
+
output_tokens_cost_per_token = 0.2
|
61 |
+
else:
|
62 |
+
input_tokens_cost_per_token = 0.2
|
63 |
+
output_tokens_cost_per_token = 0.4
|
64 |
+
return input_tokens_cost_per_token, output_tokens_cost_per_token
|
65 |
+
|
66 |
+
#Create as many Gradio components as you want to provide information or customization to the user
|
67 |
+
#Put all their visibility to False
|
68 |
+
#Don't forget to put the interactive parameter of the component to False if the value is fixed
|
69 |
+
self.model_parameter = gr.Dropdown(["Option 1", "Option 2"], value="Option 1", interactive=True,
|
70 |
+
label="Title for this parameter",
|
71 |
+
visible=False, info="Add some information to clarify specific aspects of your parameter")
|
72 |
+
|
73 |
+
#Put the values of the input and output cost per token
|
74 |
+
#These values can be updated using a function above that is triggered by a change in the parameters
|
75 |
+
#Put default values accordingly to the default parameters
|
76 |
+
self.input_cost_per_token = gr.Number(0.1, visible=False,
|
77 |
+
label="($) Price/1K input prompt tokens",
|
78 |
+
interactive=False
|
79 |
+
)
|
80 |
+
self.output_cost_per_token = gr.Number(0.2, visible=False,
|
81 |
+
label="($) Price/1K output prompt tokens",
|
82 |
+
interactive=False
|
83 |
+
)
|
84 |
+
|
85 |
+
#Trigger the values modification linked to the parameter change
|
86 |
+
self.model_parameter.change(on_model_parameter_change, inputs=self.model_parameter, outputs=[self.input_cost_per_token, self.output_cost_per_token])
|
87 |
+
|
88 |
+
#Add the labor cost of your solution
|
89 |
+
#Note that for an Open Source solution, we estimate it to 1000 $ per month and for a SaaS solution to 0
|
90 |
+
self.labor = gr.Number(0, visible=False,
|
91 |
+
label="($) Labor cost per month",
|
92 |
+
info="This is an estimate of the labor cost of the AI engineer in charge of deploying the model",
|
93 |
+
interactive=True
|
94 |
+
)
|
95 |
+
|
96 |
+
def compute_cost_per_token(self, input_cost_per_token, output_cost_per_token, labor):
|
97 |
+
#Additional computation on your cost_per_token values
|
98 |
+
#You often need to convert some values here
|
99 |
+
return input_cost_per_token, output_cost_per_token, labor
|