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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import inspect
|
| 2 |
+
import json
|
| 3 |
+
import ast
|
| 4 |
+
|
| 5 |
+
def function_to_json(func_str, func_description, param_descriptions, required_params):
|
| 6 |
+
# Create a new Module instance with the missing field
|
| 7 |
+
module_ast = ast.Module(body=[ast.Pass()], type_ignores=[])
|
| 8 |
+
|
| 9 |
+
# Parse the function string into the AST and replace the body
|
| 10 |
+
func_ast = ast.parse(func_str)
|
| 11 |
+
module_ast.body = func_ast.body
|
| 12 |
+
|
| 13 |
+
# Extract the function definition node
|
| 14 |
+
func_def = next(node for node in module_ast.body if isinstance(node, ast.FunctionDef))
|
| 15 |
+
|
| 16 |
+
# Get function signature
|
| 17 |
+
code_obj = compile(module_ast, '<string>', 'exec')
|
| 18 |
+
func_globals = {}
|
| 19 |
+
exec(code_obj, func_globals)
|
| 20 |
+
signature = inspect.signature(func_globals[func_def.name])
|
| 21 |
+
parameters = signature.parameters
|
| 22 |
+
|
| 23 |
+
# Convert param_descriptions string to a dictionary
|
| 24 |
+
param_desc_dict = json.loads(param_descriptions)
|
| 25 |
+
|
| 26 |
+
# Create JSON structure
|
| 27 |
+
function_json = {
|
| 28 |
+
"name": func_def.name,
|
| 29 |
+
"description": func_description,
|
| 30 |
+
"parameters": {
|
| 31 |
+
"type": "object",
|
| 32 |
+
"properties": {}
|
| 33 |
+
}
|
| 34 |
+
}
|
| 35 |
+
|
| 36 |
+
# Add parameter information to JSON structure
|
| 37 |
+
for param_name, param in parameters.items():
|
| 38 |
+
param_info = param_desc_dict.get(param_name, {})
|
| 39 |
+
param_type = param_info.get("type", str(param.annotation))
|
| 40 |
+
param_desc = param_info.get("description", param_name.replace('_', ' '))
|
| 41 |
+
|
| 42 |
+
function_json["parameters"]["properties"][param_name] = {
|
| 43 |
+
"type": param_type,
|
| 44 |
+
"description": param_desc
|
| 45 |
+
}
|
| 46 |
+
|
| 47 |
+
# Add required parameters based on user input
|
| 48 |
+
if param_name in required_params:
|
| 49 |
+
if "required" not in function_json["parameters"]:
|
| 50 |
+
function_json["parameters"]["required"] = []
|
| 51 |
+
function_json["parameters"]["required"].append(param_name)
|
| 52 |
+
|
| 53 |
+
return json.dumps(function_json, indent=4)
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
""" Example uasge:
|
| 58 |
+
# Example usage with user-provided function information
|
| 59 |
+
sample_function_str = '''
|
| 60 |
+
def generate_music(input_text, input_melody):
|
| 61 |
+
"""
|
| 62 |
+
generate music based on an input text
|
| 63 |
+
"""
|
| 64 |
+
client = Client("https://ysharma-musicgendupe.hf.space/", hf_token="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")
|
| 65 |
+
result = client.predict(
|
| 66 |
+
"melody",
|
| 67 |
+
input_text,
|
| 68 |
+
input_melody,
|
| 69 |
+
5,
|
| 70 |
+
250,
|
| 71 |
+
0,
|
| 72 |
+
1,
|
| 73 |
+
3,
|
| 74 |
+
fn_index=1
|
| 75 |
+
)
|
| 76 |
+
return result
|
| 77 |
+
'''
|
| 78 |
+
|
| 79 |
+
sample_func_description = "generate music based on an input text and input melody"
|
| 80 |
+
|
| 81 |
+
sample_param_descriptions = '''
|
| 82 |
+
{
|
| 83 |
+
"input_text": {
|
| 84 |
+
"type": "str",
|
| 85 |
+
"description": "Input text for music generation."
|
| 86 |
+
},
|
| 87 |
+
"input_melody": {
|
| 88 |
+
"type": "str",
|
| 89 |
+
"description": "File path of the input melody."
|
| 90 |
+
}
|
| 91 |
+
}
|
| 92 |
+
'''
|
| 93 |
+
|
| 94 |
+
sample_required_params = ["input_text"]
|
| 95 |
+
|
| 96 |
+
# Convert the sample function information to JSON
|
| 97 |
+
json_str = function_to_json(sample_function_str, sample_func_description, sample_param_descriptions, sample_required_params)
|
| 98 |
+
print(json_str)
|
| 99 |
+
|
| 100 |
+
{
|
| 101 |
+
"name": "generate_music",
|
| 102 |
+
"description": "generate music based on an input text and input melody",
|
| 103 |
+
"parameters": {
|
| 104 |
+
"type": "object",
|
| 105 |
+
"properties": {
|
| 106 |
+
"input_text": {
|
| 107 |
+
"type": "str",
|
| 108 |
+
"description": "Input text for music generation."
|
| 109 |
+
},
|
| 110 |
+
"input_melody": {
|
| 111 |
+
"type": "str",
|
| 112 |
+
"description": "File path of the input melody."
|
| 113 |
+
}
|
| 114 |
+
},
|
| 115 |
+
"required": [
|
| 116 |
+
"input_text"
|
| 117 |
+
]
|
| 118 |
+
}
|
| 119 |
+
}
|
| 120 |
+
|
| 121 |
+
"""
|
| 122 |
+
|
| 123 |
+
|
| 124 |
+
title = "<h1 align='center'>Convert any function to function definitions required for GPT</h1>"
|
| 125 |
+
demo = gr.Blocks()
|
| 126 |
+
|
| 127 |
+
with demo:
|
| 128 |
+
gr.HTML(title)
|
| 129 |
+
with gr.Row():
|
| 130 |
+
input_function_str = gr.Code(label="Enter function definition", language='python', lines=10)
|
| 131 |
+
#input_function_str = gr.Textbox(lines=10, label='Enter function definition')
|
| 132 |
+
with gr.Column():
|
| 133 |
+
input_func_description = gr.Textbox(placeholder='', label='Enter your function description:')
|
| 134 |
+
input_param_description = gr.Textbox(
|
| 135 |
+
placeholder="""Enter description as a dictionary with keys as param_name and values as param type and description as shown, eg. -
|
| 136 |
+
{
|
| 137 |
+
"param1": {
|
| 138 |
+
"type": "str",
|
| 139 |
+
"description": "description of param1"
|
| 140 |
+
},
|
| 141 |
+
"param2": {
|
| 142 |
+
"type": "int/float/list/tuple/dict/set/bool etc..",
|
| 143 |
+
"description": "description of param2"
|
| 144 |
+
}
|
| 145 |
+
}""",
|
| 146 |
+
label='Enter descriptions for parameters:')
|
| 147 |
+
input_required_params = gr.Textbox(placeholder="""Enter a list of required parameters, eg. - ['param1', 'param2', ...]""",
|
| 148 |
+
label='Enter required parameters for your function:')
|
| 149 |
+
generate_json = gr.Button('Get JSON definition')
|
| 150 |
+
gpt_function = gr.Code(label="GPT function definition", language='python', lines=7)
|
| 151 |
+
|
| 152 |
+
generate_json.click(function_to_json,
|
| 153 |
+
[input_function_str, input_func_description, input_param_description, input_required_params],
|
| 154 |
+
[gpt_function])
|
| 155 |
+
|
| 156 |
+
gr.Examples(
|
| 157 |
+
[ ["""
|
| 158 |
+
def generate_music(input_text, input_melody):
|
| 159 |
+
"generate music based on an input text"
|
| 160 |
+
client = Client("https://ysharma-musicgendupe.hf.space/", hf_token="hf_...")
|
| 161 |
+
result = client.predict(
|
| 162 |
+
"melody",
|
| 163 |
+
input_text,
|
| 164 |
+
input_melody,
|
| 165 |
+
5,
|
| 166 |
+
250,
|
| 167 |
+
0,
|
| 168 |
+
1,
|
| 169 |
+
3,
|
| 170 |
+
fn_index=1
|
| 171 |
+
)
|
| 172 |
+
return result
|
| 173 |
+
""",
|
| 174 |
+
"""Generate music based on an input text.""",
|
| 175 |
+
"""{
|
| 176 |
+
"input_text": {
|
| 177 |
+
"type": "string",
|
| 178 |
+
"description": "Input text for music generation."
|
| 179 |
+
},
|
| 180 |
+
"input_melody": {
|
| 181 |
+
"type": "string",
|
| 182 |
+
"description": "File path of the input melody."
|
| 183 |
+
}
|
| 184 |
+
}""",
|
| 185 |
+
"""["input_text"]""" ],
|
| 186 |
+
|
| 187 |
+
["""
|
| 188 |
+
def generate_image(prompt):
|
| 189 |
+
client = Client("https://jingyechen22-textdiffuser.hf.space/")
|
| 190 |
+
result = client.predict(
|
| 191 |
+
prompt,
|
| 192 |
+
20,
|
| 193 |
+
7.5,
|
| 194 |
+
1,
|
| 195 |
+
"Stable Diffusion v2.1",
|
| 196 |
+
fn_index=1)
|
| 197 |
+
return result[0]
|
| 198 |
+
""",
|
| 199 |
+
"""generate image based on the input text prompt""",
|
| 200 |
+
"""{
|
| 201 |
+
"prompt": {
|
| 202 |
+
"type": "string",
|
| 203 |
+
"description": "input text prompt for the image generation."
|
| 204 |
+
}
|
| 205 |
+
}""",
|
| 206 |
+
"""["prompt"]""" ,
|
| 207 |
+
],
|
| 208 |
+
],
|
| 209 |
+
[input_function_str, input_func_description, input_param_description, input_required_params],
|
| 210 |
+
)
|
| 211 |
+
demo.launch() #(debug=True)
|
| 212 |
+
|