Spaces:
Paused
Paused
Download app.py from rahon/ingredient-parser-hf: direct link, hf CLI and curl.
- Browser
- Download file 966 Bytes
-
https://huggingface.co/spaces/rahon/ingredient-parser-hf/resolve/main/app.py
- Command line
-
hf download hf://spaces/rahon/ingredient-parser-hf/app.py
-
curl -L -o app.py https://huggingface.co/spaces/rahon/ingredient-parser-hf/resolve/main/app.py
966 Bytes
| 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() | |