miguelamendez commited on
Commit
9de707e
·
verified ·
1 Parent(s): 8c5c24b

Update app.py

Browse files

Added text_to_json tool

Files changed (1) hide show
  1. app.py +40 -7
app.py CHANGED
@@ -3,20 +3,53 @@ import datetime
3
  import requests
4
  import pytz
5
  import yaml
 
6
  from tools.final_answer import FinalAnswerTool
7
 
8
  from Gradio_UI import GradioUI
9
 
10
- # Below is an example of a tool that does nothing. Amaze us with your creativity !
 
11
  @tool
12
- def my_custom_tool(arg1:str, arg2:int)-> str: #it's import to specify the return type
13
- #Keep this format for the description / args / args description but feel free to modify the tool
14
- """A tool that does nothing yet
 
15
  Args:
16
- arg1: the first argument
17
- arg2: the second argument
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
  """
19
- return "What magic will you build ?"
 
 
 
 
 
 
 
 
 
 
 
20
 
21
  @tool
22
  def get_current_time_in_timezone(timezone: str) -> str:
 
3
  import requests
4
  import pytz
5
  import yaml
6
+ import json
7
  from tools.final_answer import FinalAnswerTool
8
 
9
  from Gradio_UI import GradioUI
10
 
11
+ import json
12
+
13
  @tool
14
+ def text_to_json(text: str, json_schema: dict) -> dict:
15
+ """
16
+ Uses an LLM to transform the information of plain text into structured data using a JSON schema.
17
+
18
  Args:
19
+ text (str): The desired text to be transformed into JSON.
20
+ json_schema (dict): The JSON schema that defines the structure of the valid JSON.
21
+
22
+ Returns:
23
+ dict: A valid JSON object as a Python dictionary.
24
+
25
+ Example:
26
+ >>> text = "John Doe, 30 years old, lives in New York and works as a software engineer."
27
+ >>> json_schema = {
28
+ ... "type": "object",
29
+ ... "properties": {
30
+ ... "name": {"type": "string"},
31
+ ... "age": {"type": "integer"},
32
+ ... "city": {"type": "string"},
33
+ ... "occupation": {"type": "string"}
34
+ ... },
35
+ ... "required": ["name", "age", "city", "occupation"]
36
+ ... }
37
+ >>> result = text_to_json(text, json_schema)
38
+ >>> print(result)
39
+ {'name': 'John Doe', 'age': 30, 'city': 'New York', 'occupation': 'software engineer'}
40
  """
41
+ model = HfApiModel(
42
+ max_tokens=2096,
43
+ temperature=0.5,
44
+ model_id='Qwen/Qwen2.5-Coder-32B-Instruct',
45
+ custom_role_conversions=None,
46
+ )
47
+ messages = [
48
+ {"role": "system", "content": "You are an assistant that converts text into JSON. Convert the provided text into a JSON object based on the provided schema. JSON_SCHEMA:\n```json " + json.dumps(json_schema) + "```"},
49
+ {"role": "user", "content": text},
50
+ ]
51
+ return json.loads(model(messages, response_format={"type": "json_object", "schema": json_schema}).content)
52
+
53
 
54
  @tool
55
  def get_current_time_in_timezone(timezone: str) -> str: