Spaces:
Runtime error
Runtime error
smellslikeml
commited on
Commit
·
af3df09
1
Parent(s):
a290131
initial commit
Browse files- README.md +11 -4
- app.py +4 -0
- integer_programming.py +135 -0
- requirements.txt +6 -0
- tool_config.json +6 -0
README.md
CHANGED
|
@@ -1,8 +1,8 @@
|
|
| 1 |
---
|
| 2 |
-
title:
|
| 3 |
-
emoji:
|
| 4 |
-
colorFrom:
|
| 5 |
-
colorTo:
|
| 6 |
sdk: gradio
|
| 7 |
sdk_version: 3.43.2
|
| 8 |
app_file: app.py
|
|
@@ -10,4 +10,11 @@ pinned: false
|
|
| 10 |
license: mit
|
| 11 |
---
|
| 12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
|
| 1 |
---
|
| 2 |
+
title: Google Sheet Extractor
|
| 3 |
+
emoji: 👀
|
| 4 |
+
colorFrom: blue
|
| 5 |
+
colorTo: yellow
|
| 6 |
sdk: gradio
|
| 7 |
sdk_version: 3.43.2
|
| 8 |
app_file: app.py
|
|
|
|
| 10 |
license: mit
|
| 11 |
---
|
| 12 |
|
| 13 |
+
## Getting Started
|
| 14 |
+
|
| 15 |
+
Please configure your OpenAI API key as an environment variable:
|
| 16 |
+
```
|
| 17 |
+
export OPENAI_API_KEY="yout-key-here"
|
| 18 |
+
```
|
| 19 |
+
|
| 20 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
app.py
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers.tools.base import launch_gradio_demo
|
| 2 |
+
from integer_programming import IntegerProgrammingTool
|
| 3 |
+
|
| 4 |
+
launch_gradio_demo(IntegerProgrammingTool)
|
integer_programming.py
ADDED
|
@@ -0,0 +1,135 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import math
|
| 2 |
+
import json
|
| 3 |
+
import regex
|
| 4 |
+
import inspect
|
| 5 |
+
import guidance
|
| 6 |
+
from ast import literal_eval
|
| 7 |
+
from transformers import Tool
|
| 8 |
+
from ortools.linear_solver import pywraplp
|
| 9 |
+
|
| 10 |
+
guidance.llm = guidance.llms.OpenAI("gpt-4")
|
| 11 |
+
|
| 12 |
+
structure_program = guidance(
|
| 13 |
+
'''
|
| 14 |
+
{{#user~}}
|
| 15 |
+
{{description}}
|
| 16 |
+
Help me extract args from the data blob to apply the following algorithm:
|
| 17 |
+
{{code}}
|
| 18 |
+
|
| 19 |
+
----
|
| 20 |
+
|
| 21 |
+
{{~#each examples}}
|
| 22 |
+
Data Blob: {{this.input}}
|
| 23 |
+
Result: {{this.output}}
|
| 24 |
+
---
|
| 25 |
+
{{~/each}}
|
| 26 |
+
|
| 27 |
+
Please help me extract the input values from a given data blob into a JSON.
|
| 28 |
+
Data Blob: {{data_blob}}
|
| 29 |
+
Result:
|
| 30 |
+
{{~/user}}
|
| 31 |
+
|
| 32 |
+
{{#assistant~}}
|
| 33 |
+
{{gen 'output'}}
|
| 34 |
+
{{~/assistant}}
|
| 35 |
+
''')
|
| 36 |
+
|
| 37 |
+
class IntegerProgrammingTool(Tool):
|
| 38 |
+
name = "integer_programming_tool"
|
| 39 |
+
description = """
|
| 40 |
+
This tool solves an integer programming problem.
|
| 41 |
+
Input is data_blob
|
| 42 |
+
Output is the optimal solution as a string.
|
| 43 |
+
"""
|
| 44 |
+
inputs = ["text"]
|
| 45 |
+
outputs = ["text"]
|
| 46 |
+
examples = [
|
| 47 |
+
{'input': '''
|
| 48 |
+
,Space,Price
|
| 49 |
+
Puzzle,1,2
|
| 50 |
+
BoardGame,5,12
|
| 51 |
+
|
| 52 |
+
Constraint,100
|
| 53 |
+
''',
|
| 54 |
+
'output': {
|
| 55 |
+
"objective_coeffs": [
|
| 56 |
+
[1, 2],
|
| 57 |
+
[5, 15],
|
| 58 |
+
],
|
| 59 |
+
"constraints": [100],
|
| 60 |
+
"bounds": [
|
| 61 |
+
[0, None],
|
| 62 |
+
[0, None]
|
| 63 |
+
],
|
| 64 |
+
"goal": "max"
|
| 65 |
+
},
|
| 66 |
+
},
|
| 67 |
+
{'input': '''
|
| 68 |
+
,Space,Price
|
| 69 |
+
Puzzle,3,12
|
| 70 |
+
BoardGame,15,120
|
| 71 |
+
|
| 72 |
+
Constraint,300
|
| 73 |
+
''',
|
| 74 |
+
'output': {
|
| 75 |
+
"objective_coeffs": [
|
| 76 |
+
[3, 2],
|
| 77 |
+
[15, 120],
|
| 78 |
+
],
|
| 79 |
+
"constraints": [300],
|
| 80 |
+
"bounds": [
|
| 81 |
+
[0, None],
|
| 82 |
+
[0, None]
|
| 83 |
+
],
|
| 84 |
+
"goal": "max"
|
| 85 |
+
},
|
| 86 |
+
},
|
| 87 |
+
]
|
| 88 |
+
def __call__(self, data_blob):
|
| 89 |
+
code = inspect.getsourcelines(self.__call__)
|
| 90 |
+
args = structure_program(
|
| 91 |
+
description=self.description,
|
| 92 |
+
code=code,
|
| 93 |
+
examples=self.examples,
|
| 94 |
+
data_blob=data_blob,
|
| 95 |
+
)['output']
|
| 96 |
+
pattern = regex.compile(r"\{(?:[^{}]|(?R))*\}")
|
| 97 |
+
matches = pattern.findall(args)[0]
|
| 98 |
+
args = literal_eval(matches)
|
| 99 |
+
print(args)
|
| 100 |
+
objective_coeffs = args['objective_coeffs']
|
| 101 |
+
constraints = args['constraints']
|
| 102 |
+
bounds = args['bounds']
|
| 103 |
+
goal = args['goal']
|
| 104 |
+
|
| 105 |
+
objective_coeffs = list(zip(*objective_coeffs))
|
| 106 |
+
solver = pywraplp.Solver.CreateSolver("CBC")
|
| 107 |
+
variables = [solver.IntVar(
|
| 108 |
+
int(bounds[i][0]) if bounds[i][0] is not None else -math.inf,
|
| 109 |
+
int(bounds[i][1]) if bounds[i][1] is not None else math.inf,
|
| 110 |
+
f"x{i}") for i in range(len(objective_coeffs))]
|
| 111 |
+
|
| 112 |
+
# Set objective function
|
| 113 |
+
objective = solver.Objective()
|
| 114 |
+
for i, coeff_list in enumerate(objective_coeffs):
|
| 115 |
+
for j, coeff_value in enumerate(coeff_list):
|
| 116 |
+
objective.SetCoefficient(variables[j], coeff_value)
|
| 117 |
+
if goal == 'max':
|
| 118 |
+
objective.SetMaximization()
|
| 119 |
+
else:
|
| 120 |
+
objective.SetMinimization()
|
| 121 |
+
|
| 122 |
+
# Add constraints
|
| 123 |
+
for i, constraint_value in enumerate(constraints):
|
| 124 |
+
if constraint_value:
|
| 125 |
+
constraint = solver.RowConstraint(0, constraint_value)
|
| 126 |
+
for j, coeff in enumerate(objective_coeffs[i]):
|
| 127 |
+
constraint.SetCoefficient(variables[j], coeff)
|
| 128 |
+
|
| 129 |
+
solver.Solve()
|
| 130 |
+
|
| 131 |
+
solution = {"Objective value": objective.Value()}
|
| 132 |
+
for i, variable in enumerate(variables):
|
| 133 |
+
solution[f"x{i}"] = variable.solution_value()
|
| 134 |
+
|
| 135 |
+
return json.dumps(solution)
|
requirements.txt
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
openai
|
| 2 |
+
ortools
|
| 3 |
+
requests
|
| 4 |
+
guidance
|
| 5 |
+
huggingface_hub
|
| 6 |
+
transformers
|
tool_config.json
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"description": "This tool solves an integer programming problem. Input is data_blob. Output is the optimal solution.",
|
| 3 |
+
"name": "integer_programming_tool",
|
| 4 |
+
"tool_class": "integer_programming.IntegerProgrammingTool"
|
| 5 |
+
}
|
| 6 |
+
|