desiree commited on
Commit
e44659b
·
verified ·
1 Parent(s): f583115

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +86 -22
app.py CHANGED
@@ -1,32 +1,92 @@
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
 
 
3
 
4
- """
5
- For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
6
- """
7
  client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
 
10
  def respond(
11
  message,
12
  history: list[tuple[str, str]],
13
- system_message,
14
- max_tokens,
15
- temperature,
16
- top_p,
17
  ):
18
  messages = [{"role": "system", "content": system_message}]
19
 
20
- for val in history:
21
- if val[0]:
22
- messages.append({"role": "user", "content": val[0]})
23
- if val[1]:
24
- messages.append({"role": "assistant", "content": val[1]})
25
 
26
  messages.append({"role": "user", "content": message})
27
 
28
  response = ""
29
-
30
  for message in client.chat_completion(
31
  messages,
32
  max_tokens=max_tokens,
@@ -35,19 +95,21 @@ def respond(
35
  top_p=top_p,
36
  ):
37
  token = message.choices[0].delta.content
38
-
39
  response += token
40
- yield response
 
 
 
 
 
 
 
41
 
42
-
43
- """
44
- For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
45
- """
46
  demo = gr.ChatInterface(
47
  respond,
48
  additional_inputs=[
49
- gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
50
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
51
  gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
52
  gr.Slider(
53
  minimum=0.1,
@@ -57,8 +119,10 @@ demo = gr.ChatInterface(
57
  label="Top-p (nucleus sampling)",
58
  ),
59
  ],
 
 
 
60
  )
61
 
62
-
63
  if __name__ == "__main__":
64
  demo.launch()
 
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
3
+ import json
4
+ import re
5
 
 
 
 
6
  client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
7
 
8
+ SYSTEM_PROMPT = """You are an AI agent planner that helps break down tasks into clear, actionable steps. For each task, you will:
9
+ 1. Analyze the task and break it down into specific sub-tasks
10
+ 2. Create a structured plan with numbered steps
11
+ 3. Include any relevant considerations or potential challenges
12
+ 4. Format the response as a JSON with the following structure:
13
+ {
14
+ "task_analysis": "Brief analysis of the main task",
15
+ "steps": [
16
+ {
17
+ "step_number": 1,
18
+ "description": "Step description",
19
+ "estimated_time": "Time estimate",
20
+ "considerations": ["List of considerations"]
21
+ }
22
+ ],
23
+ "potential_challenges": ["List of potential challenges"],
24
+ "resources_needed": ["List of required resources"]
25
+ }
26
+
27
+ Keep your responses focused and practical."""
28
+
29
+ def parse_json_response(response_text):
30
+ """Extract JSON from the response text."""
31
+ try:
32
+ # Find JSON pattern in the text
33
+ json_match = re.search(r'\{.*\}', response_text, re.DOTALL)
34
+ if json_match:
35
+ json_str = json_match.group()
36
+ return json.loads(json_str)
37
+ return None
38
+ except json.JSONDecodeError:
39
+ return None
40
+
41
+ def format_plan(plan_json):
42
+ """Format the JSON plan into a readable markdown string."""
43
+ if not plan_json:
44
+ return "Error: Could not parse the plan. Please try again."
45
+
46
+ output = []
47
+ output.append("# Task Analysis")
48
+ output.append(plan_json.get("task_analysis", ""))
49
+ output.append("\n## Detailed Steps")
50
+
51
+ for step in plan_json.get("steps", []):
52
+ output.append(f"\n### Step {step.get('step_number')}: {step.get('description')}")
53
+ output.append(f"- Estimated time: {step.get('estimated_time')}")
54
+ if step.get('considerations'):
55
+ output.append("\nConsiderations:")
56
+ for consideration in step['considerations']:
57
+ output.append(f"- {consideration}")
58
+
59
+ if plan_json.get("potential_challenges"):
60
+ output.append("\n## Potential Challenges")
61
+ for challenge in plan_json["potential_challenges"]:
62
+ output.append(f"- {challenge}")
63
+
64
+ if plan_json.get("resources_needed"):
65
+ output.append("\n## Required Resources")
66
+ for resource in plan_json["resources_needed"]:
67
+ output.append(f"- {resource}")
68
+
69
+ return "\n".join(output)
70
 
71
  def respond(
72
  message,
73
  history: list[tuple[str, str]],
74
+ system_message=SYSTEM_PROMPT,
75
+ max_tokens=1024,
76
+ temperature=0.7,
77
+ top_p=0.95,
78
  ):
79
  messages = [{"role": "system", "content": system_message}]
80
 
81
+ for user_msg, assistant_msg in history:
82
+ if user_msg:
83
+ messages.append({"role": "user", "content": user_msg})
84
+ if assistant_msg:
85
+ messages.append({"role": "assistant", "content": assistant_msg})
86
 
87
  messages.append({"role": "user", "content": message})
88
 
89
  response = ""
 
90
  for message in client.chat_completion(
91
  messages,
92
  max_tokens=max_tokens,
 
95
  top_p=top_p,
96
  ):
97
  token = message.choices[0].delta.content
 
98
  response += token
99
+
100
+ # Try to parse and format JSON as it comes in
101
+ plan_json = parse_json_response(response)
102
+ if plan_json:
103
+ formatted_response = format_plan(plan_json)
104
+ yield formatted_response
105
+ else:
106
+ yield response
107
 
 
 
 
 
108
  demo = gr.ChatInterface(
109
  respond,
110
  additional_inputs=[
111
+ gr.Textbox(value=SYSTEM_PROMPT, label="System message", lines=5),
112
+ gr.Slider(minimum=1, maximum=2048, value=1024, step=1, label="Max new tokens"),
113
  gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
114
  gr.Slider(
115
  minimum=0.1,
 
119
  label="Top-p (nucleus sampling)",
120
  ),
121
  ],
122
+ title="AI Agent Planner",
123
+ description="I help break down tasks into clear, actionable steps. Describe your task, and I'll create a detailed plan.",
124
+ theme=gr.themes.Soft(),
125
  )
126
 
 
127
  if __name__ == "__main__":
128
  demo.launch()