Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -3,55 +3,27 @@ from smolagents import CodeAgent, HfApiModel
|
|
3 |
from tools.final_answer import FinalAnswerTool
|
4 |
from Gradio_UI import GradioUI
|
5 |
|
6 |
-
#
|
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 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
response = self.model.complete(messages=messages, max_tokens=1024)
|
24 |
-
return self.clean_response(response)
|
25 |
-
|
26 |
-
def clean_response(self, text):
|
27 |
-
"""
|
28 |
-
Clean up the response text without any code parsing
|
29 |
-
"""
|
30 |
-
# Remove potential error messages
|
31 |
-
if "Error in code parsing" in text:
|
32 |
-
text = text.split("Error in code parsing")[0]
|
33 |
-
|
34 |
-
# Remove code block markers and step information
|
35 |
-
text = text.replace("```", "")
|
36 |
-
text = text.replace("<end_code>", "")
|
37 |
-
|
38 |
-
# Remove step information
|
39 |
-
lines = []
|
40 |
-
for line in text.split('\n'):
|
41 |
-
if not any(x in line.lower() for x in ["step", "duration:", "input-tokens:", "output-tokens:"]):
|
42 |
-
lines.append(line)
|
43 |
-
|
44 |
-
# Clean up and join
|
45 |
-
cleaned_lines = [line.strip() for line in lines if line.strip()]
|
46 |
-
return '\n\n'.join(cleaned_lines)
|
47 |
|
48 |
# Load prompt templates from YAML
|
49 |
with open("prompts.yaml", 'r') as stream:
|
50 |
prompt_templates = yaml.safe_load(stream)
|
51 |
-
# Remove any code-related sections
|
52 |
-
for key in list(prompt_templates.keys()):
|
53 |
-
if 'code' in key.lower():
|
54 |
-
prompt_templates.pop(key)
|
55 |
|
56 |
# Initialize the model with simplified settings
|
57 |
model = HfApiModel(
|
@@ -60,8 +32,8 @@ model = HfApiModel(
|
|
60 |
model_id='deepseek-ai/DeepSeek-R1-Distill-Qwen-32B'
|
61 |
)
|
62 |
|
63 |
-
# Initialize
|
64 |
-
agent =
|
65 |
model=model,
|
66 |
tools=[FinalAnswerTool()],
|
67 |
max_steps=1,
|
@@ -77,9 +49,9 @@ def run_agent(user_input):
|
|
77 |
"""
|
78 |
try:
|
79 |
response = agent.run(user_input)
|
80 |
-
return response
|
81 |
except Exception as e:
|
82 |
return f"I apologize, but I couldn't process your request. Please try again."
|
83 |
|
84 |
# Launch the Gradio UI
|
85 |
-
GradioUI(agent).launch()
|
|
|
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(
|
|
|
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,
|
|
|
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()
|