ianeksdi commited on
Commit
5c1340d
·
verified ·
1 Parent(s): 09155aa

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -35
app.py CHANGED
@@ -1,57 +1,58 @@
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='Qwen/Qwen2.5-Coder-32B-Instruct'
 
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()
 
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
+ # 🚀 Updated system prompt: No code recognition or extraction allowed.
8
  system_prompt = (
9
  "You are a health and lifestyle advisor specializing in the early detection and prevention of hypertension. "
10
+ "Provide only the final, direct, and concise lifestyle advice based solely on the user's details. "
11
+ "Do NOT recognize, extract, or output any code snippets, programming-related information, or technical explanations. "
12
+ "Only output the final advice as plain text. "
13
+ "For example, if the user mentions alcohol consumption, simply say: 'Reduce alcohol intake, as it can raise blood pressure.'"
14
  )
15
 
16
+ # 🛑 Function to remove any detected code snippets.
17
+ def remove_code_snippets(text):
18
+ """Removes potential code snippets from the output."""
19
+ pattern = r"```.*?```|`[^`]+`" # Matches both block and inline code
20
+ return re.sub(pattern, "", text, flags=re.DOTALL).strip()
 
 
 
 
 
 
 
 
 
21
 
22
+ # Use only the final_answer tool.
23
+ final_answer = FinalAnswerTool()
24
+
25
+ # 🎯 Model setup
26
  model = HfApiModel(
27
+ max_tokens=2096,
28
  temperature=0.5,
29
+ model_id='deepseek-ai/DeepSeek-R1-Distill-Qwen-32B',
30
+ custom_role_conversions=None,
31
  )
32
 
33
+ # 📖 Load prompt templates from YAML file.
34
+ with open("prompts.yaml", 'r') as stream:
35
+ prompt_templates = yaml.safe_load(stream)
36
+
37
+ # 🔄 Initialize CodeAgent (but block code extraction)
38
  agent = CodeAgent(
39
  model=model,
40
+ tools=[final_answer], # Restricted to final advice only.
41
+ max_steps=6,
42
+ verbosity_level=1,
43
+ grammar=None,
44
+ planning_interval=None,
45
+ name="Hypertension Prevention Advisor",
46
  description=system_prompt,
47
  prompt_templates=prompt_templates
48
  )
49
 
50
+ # 🛑 Run agent with filtering.
51
  def run_agent(user_input):
52
+ """Runs the agent and ensures no code snippets are in the output."""
53
+ raw_response = agent.run(user_input)
54
+ clean_response = remove_code_snippets(raw_response) # Filter out code snippets
55
+ return clean_response
56
+
57
+ # 🚀 Launch the Gradio UI.
 
 
 
 
58
  GradioUI(agent).launch()