import json import subprocess import gradio as gr subprocess.run(["gem", "install", "food_ingredient_parser"]) subprocess.run(["chmod", "+x", "./bin/food_ingredient_parser"]) def convert_hash_to_dict(ruby_hash): dict_str = ruby_hash.replace(":", '"') # Remove the ruby object key prefix dict_str = dict_str.replace( "=>", '" : ' ) # swap the k => v notation, and close any unshut quotes dict_str = dict_str.replace( '""', '"' ) # strip back any double quotes we created to sinlges return json.loads(dict_str) def parse(ingredients: str): ruby_dict_str = "\n".join( subprocess.run( ["./bin/food_ingredient_parser", "--no-color", "-s", f"{ingredients}"], capture_output=True, text=True, ).stdout.split("\n")[1:] ) return convert_hash_to_dict(ruby_dict_str) demo = gr.Interface( fn=parse, inputs=["text"], outputs=["json"], ) demo.launch()