| 89 | return img_data |
| 90 | |
| 91 | def build_tool(conf) -> Tool: |
| 92 | task_list = [] |
| 93 | tool = Tool( |
| 94 | tool_name="hugging_tools", |
| 95 | description="API interface for HuggingGPT-like applications.", |
| 96 | name_for_model="hugging_tools", |
| 97 | description_for_model='''This API interface provides easy access to popular models available on the Huggingface model hub. You MUST check model_docs to fetch the available models FIRST: |
| 98 | Action: model_docs |
| 99 | Action Input: {"task" : <task_name>} |
| 100 | After that you can choose an available models. ''' , |
| 101 | logo_url="https://your-app-url.com/.well-known/logo.png", |
| 102 | contact_email="test@123.com", |
| 103 | legal_info_url="hello@legal.com" |
| 104 | ) |
| 105 | |
| 106 | # set the get route to /func.__name__ and format docs |
| 107 | def task(func): |
| 108 | func.__doc__ = '''You MUST check model_docs to fetch the available models FIRST: |
| 109 | Action: model_docs |
| 110 | Action Input: {"task" : "%s"} |
| 111 | After that you can choose an available models in the list. |
| 112 | ''' % func.__name__ |
| 113 | @wraps(func) |
| 114 | def try_run_task(*args, **kwargs): |
| 115 | try: |
| 116 | return func(*args, **kwargs) |
| 117 | except RepositoryNotFoundError as e: |
| 118 | return '''The model with model_id you input is not available. Plese check the model_docs to get other available models: |
| 119 | Action: model_docs |
| 120 | Action Input: {"task" : "%s"} |
| 121 | After that you can choose an available models in the list. |
| 122 | ''' |
| 123 | path = "/" + func.__name__ |
| 124 | try_run_task.route = path |
| 125 | task_list.append(func.__name__) |
| 126 | return tool.get(path)(try_run_task) |
| 127 | |
| 128 | def format_docs(str): |
| 129 | def set_docs(func): |
| 130 | func.__doc__ = func.__doc__ % str |
| 131 | @wraps(func) |
| 132 | def original_func(*args, **kwargs): |
| 133 | return func(*args, **kwargs) |
| 134 | return original_func |
| 135 | return set_docs |
| 136 | |
| 137 | @task |
| 138 | def question_answering(model_id: str, question: str, context: str) -> str: |
| 139 | inference = InferenceApi(repo_id=model_id, token=CONFIG["huggingface"]["token"]) |
| 140 | return str(inference({"question": question, "context" : (context if context else "")})) |
| 141 | @task |
| 142 | def sentence_similarity(model_id: str, text: str, context: str) -> str: |
| 143 | inference = InferenceApi(repo_id=model_id, token=CONFIG["huggingface"]["token"]) |
| 144 | return str(inference({"source_sentence": text, "sentences" : [(context if context else "")]})) |
| 145 | @task |
| 146 | def text_classification(model_id: str, text: str) -> str: |
| 147 | inference = InferenceApi(repo_id=model_id, token=CONFIG["huggingface"]["token"]) |
| 148 | return str(inference(text)) |