Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,151 +1,142 @@
|
|
1 |
import concurrent.futures
|
2 |
import gradio as gr
|
3 |
-
from huggingface_hub import InferenceClient
|
4 |
import json
|
5 |
-
import re
|
6 |
import traceback
|
7 |
|
8 |
-
class
|
9 |
def __init__(self):
|
10 |
-
self.client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
11 |
self.executor = concurrent.futures.ThreadPoolExecutor(max_workers=1)
|
12 |
-
self.
|
13 |
-
1. Analyze the task and break it down into specific sub-tasks
|
14 |
-
2. Create a structured plan with numbered steps
|
15 |
-
3. Include any relevant considerations or potential challenges
|
16 |
-
4. Format the response as a JSON with the following structure:
|
17 |
-
{
|
18 |
-
"task_analysis": "Brief analysis of the main task",
|
19 |
-
"steps": [
|
20 |
-
{
|
21 |
-
"step_number": 1,
|
22 |
-
"description": "Step description",
|
23 |
-
"estimated_time": "Time estimate",
|
24 |
-
"considerations": ["List of considerations"]
|
25 |
-
}
|
26 |
-
],
|
27 |
-
"potential_challenges": ["List of potential challenges"],
|
28 |
-
"resources_needed": ["List of required resources"]
|
29 |
-
}
|
30 |
|
31 |
-
|
32 |
-
|
33 |
-
def parse_json_response(self, response_text):
|
34 |
-
"""Extract JSON from the response text."""
|
35 |
try:
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
return
|
40 |
-
|
41 |
-
|
42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
43 |
|
44 |
-
def
|
45 |
-
"""
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
output.append(f"- Estimated time: {step.get('estimated_time')}")
|
57 |
-
if step.get('considerations'):
|
58 |
-
output.append("\nConsiderations:")
|
59 |
-
for consideration in step['considerations']:
|
60 |
-
output.append(f"- {consideration}")
|
61 |
-
|
62 |
-
if plan_json.get("potential_challenges"):
|
63 |
-
output.append("\n## Potential Challenges")
|
64 |
-
for challenge in plan_json["potential_challenges"]:
|
65 |
-
output.append(f"- {challenge}")
|
66 |
-
|
67 |
-
if plan_json.get("resources_needed"):
|
68 |
-
output.append("\n## Required Resources")
|
69 |
-
for resource in plan_json["resources_needed"]:
|
70 |
-
output.append(f"- {resource}")
|
71 |
-
|
72 |
-
return "\n".join(output)
|
73 |
|
74 |
-
def
|
|
|
75 |
try:
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
80 |
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
if plan_json:
|
95 |
-
formatted_response = self.format_plan(plan_json)
|
96 |
-
yield formatted_response
|
97 |
-
else:
|
98 |
-
yield response
|
99 |
except Exception as e:
|
100 |
-
|
101 |
|
102 |
def __del__(self):
|
103 |
self.executor.shutdown(wait=False)
|
104 |
|
105 |
def create_interface():
|
106 |
-
|
107 |
|
108 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
109 |
-
gr.Markdown("#
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
value=0.7,
|
130 |
-
step=0.1,
|
131 |
-
label="Temperature"
|
132 |
)
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
)
|
140 |
|
141 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
142 |
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
149 |
|
150 |
return demo
|
151 |
|
|
|
1 |
import concurrent.futures
|
2 |
import gradio as gr
|
|
|
3 |
import json
|
|
|
4 |
import traceback
|
5 |
|
6 |
+
class FunctionManager:
|
7 |
def __init__(self):
|
|
|
8 |
self.executor = concurrent.futures.ThreadPoolExecutor(max_workers=1)
|
9 |
+
self.functions = {}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
|
11 |
+
def validate_function_json(self, json_str):
|
12 |
+
"""Validate function JSON format."""
|
|
|
|
|
13 |
try:
|
14 |
+
function_data = json.loads(json_str)
|
15 |
+
required_fields = ['name', 'description', 'parameters']
|
16 |
+
if not all(field in function_data for field in required_fields):
|
17 |
+
return False, "Missing required fields: name, description, or parameters"
|
18 |
+
|
19 |
+
if not isinstance(function_data['parameters'], dict):
|
20 |
+
return False, "Parameters must be a JSON object"
|
21 |
+
|
22 |
+
return True, function_data
|
23 |
+
except json.JSONDecodeError as e:
|
24 |
+
return False, f"Invalid JSON format: {str(e)}"
|
25 |
+
except Exception as e:
|
26 |
+
return False, f"Validation error: {str(e)}"
|
27 |
|
28 |
+
def add_function(self, function_json):
|
29 |
+
"""Add a new function definition."""
|
30 |
+
try:
|
31 |
+
is_valid, result = self.validate_function_json(function_json)
|
32 |
+
if not is_valid:
|
33 |
+
return f"Error: {result}"
|
34 |
+
|
35 |
+
function_name = result['name']
|
36 |
+
self.functions[function_name] = result
|
37 |
+
return f"Successfully added function: {function_name}"
|
38 |
+
except Exception as e:
|
39 |
+
return f"Error adding function: {str(e)}\n{traceback.format_exc()}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
40 |
|
41 |
+
def list_functions(self):
|
42 |
+
"""List all registered functions."""
|
43 |
try:
|
44 |
+
if not self.functions:
|
45 |
+
return "No functions registered."
|
46 |
+
|
47 |
+
output = ["# Registered Functions"]
|
48 |
+
for name, func in self.functions.items():
|
49 |
+
output.append(f"\n## {name}")
|
50 |
+
output.append(f"Description: {func['description']}")
|
51 |
+
output.append("\nParameters:")
|
52 |
+
params = func['parameters']
|
53 |
+
if 'properties' in params:
|
54 |
+
for param_name, param_info in params['properties'].items():
|
55 |
+
output.append(f"- {param_name}: {param_info.get('description', 'No description')}")
|
56 |
+
if param_info.get('type'):
|
57 |
+
output.append(f" Type: {param_info['type']}")
|
58 |
+
if param_info.get('required', False):
|
59 |
+
output.append(" Required: Yes")
|
60 |
+
|
61 |
+
return "\n".join(output)
|
62 |
+
except Exception as e:
|
63 |
+
return f"Error listing functions: {str(e)}\n{traceback.format_exc()}"
|
64 |
|
65 |
+
def format_function_call(self, function_name, parameters):
|
66 |
+
"""Format a function call into the HuggingChat format."""
|
67 |
+
try:
|
68 |
+
if function_name not in self.functions:
|
69 |
+
return False, f"Function '{function_name}' not found"
|
70 |
+
|
71 |
+
function_def = self.functions[function_name]
|
72 |
+
function_call = {
|
73 |
+
"name": function_name,
|
74 |
+
"arguments": parameters
|
75 |
+
}
|
76 |
+
|
77 |
+
return True, json.dumps(function_call, indent=2)
|
|
|
|
|
|
|
|
|
|
|
78 |
except Exception as e:
|
79 |
+
return False, f"Error formatting function call: {str(e)}"
|
80 |
|
81 |
def __del__(self):
|
82 |
self.executor.shutdown(wait=False)
|
83 |
|
84 |
def create_interface():
|
85 |
+
manager = FunctionManager()
|
86 |
|
87 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
88 |
+
gr.Markdown("# HuggingChat Function Manager")
|
89 |
+
|
90 |
+
with gr.Tab("Add Function"):
|
91 |
+
function_json = gr.Textbox(
|
92 |
+
label="Function Definition (JSON)",
|
93 |
+
placeholder='''{
|
94 |
+
"name": "example_function",
|
95 |
+
"description": "An example function",
|
96 |
+
"parameters": {
|
97 |
+
"type": "object",
|
98 |
+
"properties": {
|
99 |
+
"param1": {
|
100 |
+
"type": "string",
|
101 |
+
"description": "First parameter"
|
102 |
+
}
|
103 |
+
},
|
104 |
+
"required": ["param1"]
|
105 |
+
}
|
106 |
+
}''',
|
107 |
+
lines=10
|
|
|
|
|
|
|
108 |
)
|
109 |
+
add_output = gr.Textbox(label="Result", lines=2)
|
110 |
+
add_btn = gr.Button("Add Function", variant="primary")
|
111 |
+
add_btn.click(
|
112 |
+
manager.add_function,
|
113 |
+
inputs=[function_json],
|
114 |
+
outputs=[add_output]
|
115 |
)
|
116 |
|
117 |
+
with gr.Tab("List Functions"):
|
118 |
+
list_output = gr.Markdown(label="Registered Functions")
|
119 |
+
list_btn = gr.Button("List Functions")
|
120 |
+
list_btn.click(
|
121 |
+
manager.list_functions,
|
122 |
+
outputs=[list_output]
|
123 |
+
)
|
124 |
|
125 |
+
with gr.Tab("Format Function Call"):
|
126 |
+
with gr.Row():
|
127 |
+
call_name = gr.Textbox(label="Function Name", lines=1)
|
128 |
+
call_params = gr.Textbox(
|
129 |
+
label="Parameters (JSON)",
|
130 |
+
placeholder='{"param1": "value1"}',
|
131 |
+
lines=5
|
132 |
+
)
|
133 |
+
format_output = gr.Textbox(label="Formatted Call", lines=5)
|
134 |
+
format_btn = gr.Button("Format Call")
|
135 |
+
format_btn.click(
|
136 |
+
lambda n, p: manager.format_function_call(n, json.loads(p))[1],
|
137 |
+
inputs=[call_name, call_params],
|
138 |
+
outputs=[format_output]
|
139 |
+
)
|
140 |
|
141 |
return demo
|
142 |
|