Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,106 +1,57 @@
|
|
1 |
import yaml
|
2 |
-
import re
|
3 |
from smolagents import CodeAgent, HfApiModel
|
4 |
from tools.final_answer import FinalAnswerTool
|
5 |
from Gradio_UI import GradioUI
|
6 |
|
7 |
-
#
|
8 |
system_prompt = (
|
9 |
"You are a health and lifestyle advisor specializing in the early detection and prevention of hypertension. "
|
10 |
-
"Provide
|
11 |
-
"
|
12 |
-
"Output exactly one final answer as plain text with no extra commentary."
|
13 |
)
|
14 |
|
15 |
-
def
|
16 |
"""
|
17 |
-
|
18 |
"""
|
19 |
-
# Remove
|
20 |
-
text =
|
21 |
-
#
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
lines = text.splitlines()
|
30 |
-
filtered_lines = []
|
31 |
-
for line in lines:
|
32 |
-
# Skip lines that are clearly debug or error messages.
|
33 |
-
if re.search(r"Step \d+", line):
|
34 |
-
continue
|
35 |
-
if "Duration:" in line:
|
36 |
-
continue
|
37 |
-
if "Error in code parsing:" in line:
|
38 |
-
continue
|
39 |
-
if "💥 Error" in line:
|
40 |
-
continue
|
41 |
-
if "Reached max steps" in line:
|
42 |
-
continue
|
43 |
-
if "Make sure to provide correct code blobs" in line:
|
44 |
-
continue
|
45 |
-
if "Here is your code snippet:" in line:
|
46 |
-
continue
|
47 |
-
if "Code:" in line:
|
48 |
-
continue
|
49 |
-
filtered_lines.append(line)
|
50 |
-
|
51 |
-
cleaned_text = "\n".join(filtered_lines)
|
52 |
-
return cleaned_text.strip()
|
53 |
-
|
54 |
-
# Use only the final_answer tool.
|
55 |
-
final_answer = FinalAnswerTool()
|
56 |
|
57 |
-
#
|
58 |
model = HfApiModel(
|
59 |
max_tokens=1024,
|
60 |
temperature=0.5,
|
61 |
-
model_id='deepseek-ai/DeepSeek-R1-Distill-Qwen-32B'
|
62 |
-
custom_role_conversions=None,
|
63 |
)
|
64 |
|
65 |
-
#
|
66 |
-
with open("prompts.yaml", 'r') as stream:
|
67 |
-
prompt_templates = yaml.safe_load(stream)
|
68 |
-
|
69 |
-
# Ensure the final_answer key exists in prompt_templates to prevent KeyError.
|
70 |
-
if "final_answer" not in prompt_templates:
|
71 |
-
prompt_templates["final_answer"] = {"pre_messages": "", "post_messages": ""}
|
72 |
-
|
73 |
-
# Initialize CodeAgent with a low verbosity level to reduce extra debug output.
|
74 |
agent = CodeAgent(
|
75 |
model=model,
|
76 |
-
tools=[
|
77 |
max_steps=1,
|
78 |
verbosity_level=0,
|
79 |
-
|
80 |
-
planning_interval=None,
|
81 |
-
name="Hypertension Prevention Advisor",
|
82 |
description=system_prompt,
|
83 |
prompt_templates=prompt_templates
|
84 |
)
|
85 |
|
86 |
def run_agent(user_input):
|
87 |
"""
|
88 |
-
Runs the agent and
|
89 |
-
debug logs, and code snippet sections before returning the final plain-text answer.
|
90 |
"""
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
return "I
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
clean_response = remove_code_snippets(raw_response)
|
100 |
-
words = clean_response.split()
|
101 |
-
if len(set(words)) < 5:
|
102 |
-
return "I'm unable to generate a meaningful response. Please refine your query."
|
103 |
-
return clean_response
|
104 |
-
|
105 |
-
# Launch the Gradio UI.
|
106 |
-
GradioUI(agent).launch()
|
|
|
1 |
import yaml
|
|
|
2 |
from smolagents import CodeAgent, HfApiModel
|
3 |
from tools.final_answer import FinalAnswerTool
|
4 |
from Gradio_UI import GradioUI
|
5 |
|
6 |
+
# Simplified system prompt without any code-related instructions
|
7 |
system_prompt = (
|
8 |
"You are a health and lifestyle advisor specializing in the early detection and prevention of hypertension. "
|
9 |
+
"Provide direct and concise lifestyle advice based on the user's details. "
|
10 |
+
"Output a clear answer as plain text with no extra commentary."
|
|
|
11 |
)
|
12 |
|
13 |
+
def clean_response(text):
|
14 |
"""
|
15 |
+
Simple cleanup function that removes extra whitespace and ensures proper formatting.
|
16 |
"""
|
17 |
+
# Remove extra whitespace
|
18 |
+
text = ' '.join(text.split())
|
19 |
+
# Split into paragraphs for readability
|
20 |
+
paragraphs = text.split('\n\n')
|
21 |
+
cleaned_paragraphs = [p.strip() for p in paragraphs if p.strip()]
|
22 |
+
return '\n\n'.join(cleaned_paragraphs)
|
23 |
+
|
24 |
+
# Load prompt templates from YAML
|
25 |
+
with open("prompts.yaml", 'r') as stream:
|
26 |
+
prompt_templates = yaml.safe_load(stream)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
|
28 |
+
# Initialize the model with simplified settings
|
29 |
model = HfApiModel(
|
30 |
max_tokens=1024,
|
31 |
temperature=0.5,
|
32 |
+
model_id='deepseek-ai/DeepSeek-R1-Distill-Qwen-32B'
|
|
|
33 |
)
|
34 |
|
35 |
+
# Initialize CodeAgent with minimal configuration
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
36 |
agent = CodeAgent(
|
37 |
model=model,
|
38 |
+
tools=[FinalAnswerTool()],
|
39 |
max_steps=1,
|
40 |
verbosity_level=0,
|
41 |
+
name="Health Advisor",
|
|
|
|
|
42 |
description=system_prompt,
|
43 |
prompt_templates=prompt_templates
|
44 |
)
|
45 |
|
46 |
def run_agent(user_input):
|
47 |
"""
|
48 |
+
Runs the agent and returns a clean, formatted response.
|
|
|
49 |
"""
|
50 |
+
try:
|
51 |
+
response = agent.run(user_input)
|
52 |
+
return clean_response(response)
|
53 |
+
except Exception as e:
|
54 |
+
return f"I apologize, but I couldn't process your request. Please try again."
|
55 |
+
|
56 |
+
# Launch the Gradio UI
|
57 |
+
GradioUI(agent).launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|