desiree commited on
Commit
a05579f
·
verified ·
1 Parent(s): 28fff71

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +114 -123
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 AgentPlanner:
9
  def __init__(self):
10
- self.client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
11
  self.executor = concurrent.futures.ThreadPoolExecutor(max_workers=1)
12
- self.system_prompt = """You are an AI agent planner that helps break down tasks into clear, actionable steps. For each task, you will:
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
- Keep your responses focused and practical."""
32
-
33
- def parse_json_response(self, response_text):
34
- """Extract JSON from the response text."""
35
  try:
36
- json_match = re.search(r'\{.*\}', response_text, re.DOTALL)
37
- if json_match:
38
- json_str = json_match.group()
39
- return json.loads(json_str)
40
- return None
41
- except json.JSONDecodeError:
42
- return None
 
 
 
 
 
 
43
 
44
- def format_plan(self, plan_json):
45
- """Format the JSON plan into a readable markdown string."""
46
- if not plan_json:
47
- return "Error: Could not parse the plan. Please try again."
48
-
49
- output = []
50
- output.append("# Task Analysis")
51
- output.append(plan_json.get("task_analysis", ""))
52
- output.append("\n## Detailed Steps")
53
-
54
- for step in plan_json.get("steps", []):
55
- output.append(f"\n### Step {step.get('step_number')}: {step.get('description')}")
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 generate_plan(self, task, max_tokens=1024, temperature=0.7, top_p=0.95):
 
75
  try:
76
- messages = [
77
- {"role": "system", "content": self.system_prompt},
78
- {"role": "user", "content": task}
79
- ]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
80
 
81
- response = ""
82
- for message in self.client.chat_completion(
83
- messages,
84
- max_tokens=max_tokens,
85
- stream=True,
86
- temperature=temperature,
87
- top_p=top_p,
88
- ):
89
- token = message.choices[0].delta.content
90
- response += token
91
-
92
- # Try to parse and format JSON as it comes in
93
- plan_json = self.parse_json_response(response)
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
- yield f"Error generating plan: {str(e)}\n{traceback.format_exc()}"
101
 
102
  def __del__(self):
103
  self.executor.shutdown(wait=False)
104
 
105
  def create_interface():
106
- planner = AgentPlanner()
107
 
108
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
109
- gr.Markdown("# AI Agent Planner")
110
- gr.Markdown("Get organized, structured plans for any task")
111
-
112
- task_input = gr.Textbox(
113
- label="Task Description",
114
- placeholder="Describe the task you need help planning...",
115
- lines=3
116
- )
117
-
118
- with gr.Row():
119
- max_tokens = gr.Slider(
120
- minimum=1,
121
- maximum=2048,
122
- value=1024,
123
- step=1,
124
- label="Max Tokens"
125
- )
126
- temperature = gr.Slider(
127
- minimum=0.1,
128
- maximum=4.0,
129
- value=0.7,
130
- step=0.1,
131
- label="Temperature"
132
  )
133
- top_p = gr.Slider(
134
- minimum=0.1,
135
- maximum=1.0,
136
- value=0.95,
137
- step=0.05,
138
- label="Top-p"
139
  )
140
 
141
- output = gr.Markdown(label="Generated Plan")
 
 
 
 
 
 
142
 
143
- generate_btn = gr.Button("Generate Plan", variant="primary")
144
- generate_btn.click(
145
- planner.generate_plan,
146
- inputs=[task_input, max_tokens, temperature, top_p],
147
- outputs=output
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