agenttemplate / app.py
ianeksdi's picture
Update app.py
1655dcc verified
raw
history blame
2.78 kB
import yaml
from smolagents import CodeAgent, HfApiModel
from tools.final_answer import FinalAnswerTool
from Gradio_UI import GradioUI
# Simple system prompt without any code-related instructions
system_prompt = (
"You are a health and lifestyle advisor specializing in the early detection and prevention of hypertension. "
"Provide direct and concise lifestyle advice based on the user's details. "
"Output a clear answer as plain text with no extra commentary."
)
class SimpleAgent(CodeAgent):
def run(self, user_input, **kwargs):
"""
Override the run method to bypass code parsing completely
"""
messages = [
{"role": "system", "content": self.description},
{"role": "user", "content": user_input}
]
response = self.model.complete(messages=messages, max_tokens=1024)
return self.clean_response(response)
def clean_response(self, text):
"""
Clean up the response text without any code parsing
"""
# Remove potential error messages
if "Error in code parsing" in text:
text = text.split("Error in code parsing")[0]
# Remove code block markers and step information
text = text.replace("```", "")
text = text.replace("<end_code>", "")
# Remove step information
lines = []
for line in text.split('\n'):
if not any(x in line.lower() for x in ["step", "duration:", "input-tokens:", "output-tokens:"]):
lines.append(line)
# Clean up and join
cleaned_lines = [line.strip() for line in lines if line.strip()]
return '\n\n'.join(cleaned_lines)
# Load prompt templates from YAML
with open("prompts.yaml", 'r') as stream:
prompt_templates = yaml.safe_load(stream)
# Remove any code-related sections
for key in list(prompt_templates.keys()):
if 'code' in key.lower():
prompt_templates.pop(key)
# Initialize the model with simplified settings
model = HfApiModel(
max_tokens=1024,
temperature=0.5,
model_id='deepseek-ai/DeepSeek-R1-Distill-Qwen-32B'
)
# Initialize our custom agent instead of CodeAgent
agent = SimpleAgent(
model=model,
tools=[FinalAnswerTool()],
max_steps=1,
verbosity_level=0,
name="Health Advisor",
description=system_prompt,
prompt_templates=prompt_templates
)
def run_agent(user_input):
"""
Runs the agent and returns a clean, formatted response.
"""
try:
response = agent.run(user_input)
return response
except Exception as e:
return f"I apologize, but I couldn't process your request. Please try again."
# Launch the Gradio UI
GradioUI(agent).launch()