Spaces:
Build error
Build error
File size: 2,777 Bytes
ebbad26 9b5b26a 1655dcc ebbad26 9fb943a 89dbda5 8c01ffb 1655dcc 9fb943a 1655dcc f1f3641 9fb943a ebbad26 9fb943a ebbad26 f1f3641 1655dcc ebbad26 9fb943a cf9d0e5 ebbad26 9fb943a ebbad26 0172db2 ebbad26 9fb943a ebbad26 9fb943a 1655dcc 9fb943a |
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 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
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() |