File size: 2,119 Bytes
c19d193
8564a64
1890de8
6aae614
9b5b26a
 
1890de8
89dbda5
 
8564a64
 
 
 
89dbda5
8c01ffb
8564a64
 
 
 
 
 
 
6aae614
ae7a494
1890de8
e121372
89dbda5
 
8564a64
89dbda5
13d500a
8c01ffb
8564a64
861422e
 
89dbda5
1890de8
 
8fe992b
1890de8
8c01ffb
 
 
 
89dbda5
 
861422e
8fe992b
 
8564a64
 
 
 
1890de8
8564a64
 
 
89dbda5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import yaml
import re
from smolagents import CodeAgent, HfApiModel
from tools.final_answer import FinalAnswerTool
from Gradio_UI import GradioUI

# 🚀 Updated system prompt: No code recognition or extraction allowed.
system_prompt = (
    "You are a health and lifestyle advisor specializing in the early detection and prevention of hypertension. "
    "Provide only the final, direct, and concise lifestyle advice based solely on the user's details. "
    "Do NOT recognize, extract, or output any code snippets, programming-related information, or technical explanations. "
    "Only output the final advice as plain text. "
    "For example, if the user mentions alcohol consumption, simply say: 'Reduce alcohol intake, as it can raise blood pressure.'"
)

# 🛑 Function to remove any detected code snippets.
def remove_code_snippets(text):
    """Removes potential code snippets from the output."""
    pattern = r"```.*?```|`[^`]+`"  # Matches both block and inline code
    return re.sub(pattern, "", text, flags=re.DOTALL).strip()

# ✅ Use only the final_answer tool.
final_answer = FinalAnswerTool()

# 🎯 Model setup
model = HfApiModel(
    max_tokens=2096,
    temperature=0.5,
    model_id='deepseek-ai/DeepSeek-R1-Distill-Qwen-32B',
    custom_role_conversions=None,
)

# 📖 Load prompt templates from YAML file.
with open("prompts.yaml", 'r') as stream:
    prompt_templates = yaml.safe_load(stream)

# 🔄 Initialize CodeAgent (but block code extraction)
agent = CodeAgent(
    model=model,
    tools=[final_answer],  # Restricted to final advice only.
    max_steps=6,
    verbosity_level=1,
    grammar=None,
    planning_interval=None,
    name="Hypertension Prevention Advisor",
    description=system_prompt,
    prompt_templates=prompt_templates
)

# 🛑 Run agent with filtering.
def run_agent(user_input):
    """Runs the agent and ensures no code snippets are in the output."""
    raw_response = agent.run(user_input)
    clean_response = remove_code_snippets(raw_response)  # Filter out code snippets
    return clean_response

# 🚀 Launch the Gradio UI.
GradioUI(agent).launch()