there is a python code that asks the AI to generate a wish for the day, wanted to receive it, transfer it to home assistant and insert lovelace to see this wish every day
here is the python code
from flask import Flask, jsonify
import requests
app = Flask(__name__)
API_TOKEN = 'MY_TOKEN'
API_URL = 'https://api-inference.huggingface.co/models/mistralai/Mistral-Nemo-Instruct-2407'
headers = {
"Authorization": f"Bearer {API_TOKEN}"
}
@app.route('/generate_wish', methods=['GET'])
def generate_wish():
prompt = "Write a creative and unusual wish for a good day in prose."
payload = {
"inputs": prompt,
"parameters": {
"temperature": 0.3
}
}
response = requests.post(API_URL, headers=headers, json=payload)
if response.status_code == 200:
generated_text = response.json()[0]['generated_text']
unwanted_text = "Write a creative and unusual wish for a good day in prose"
wish = generated_text.replace(unwanted_text, "").strip()
return jsonify({"wish": wish})
else:
return jsonify({"error": response.text}), response.status_code
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000)
I receive data in the form of text (for example, let’s take a simple option - have a nice day)
I wanted to use node red to get this data and then insert it into my lovelace, but something doesn’t work, node red gets the data, but I don’t see this message in home assistant
please help
[{"id":"7afc7ba787cdd45f","type":"tab","label":"Поток 6","disabled":false,"info":"","env":[]},{"id":"0724c3a7da171eaa","type":"inject","z":"7afc7ba787cdd45f","name":"Запросить пожелание","props":[],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payloadType":"date","x":140,"y":60,"wires":[["987c718704bba886"]]},{"id":"987c718704bba886","type":"http request","z":"7afc7ba787cdd45f","name":"Запрос к Flask API","method":"GET","ret":"txt","paytoqs":false,"url":"http://localhost:5000/generate_wish","tls":"","persist":false,"proxy":"","authType":"","senderr":false,"x":170,"y":180,"wires":[["21399478bf30dee2"]]},{"id":"21399478bf30dee2","type":"function","z":"7afc7ba787cdd45f","name":"Обработать ответ","func":"const response = JSON.parse(msg.payload);\nif (response.wish) {\n msg.payload = response.wish.replace(/\\n/g, \" \"); // Удаляем символы новой строки\n} else {\n msg.payload = \"Ошибка получения пожелания\";\n}\nreturn msg;\n","outputs":1,"timeout":"","noerr":0,"initialize":"","finalize":"","libs":[],"x":430,"y":40,"wires":[["75cbf1f62cb5c503"]]},{"id":"75cbf1f62cb5c503","type":"template","z":"7afc7ba787cdd45f","name":"Создать сообщение","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"{{payload}}","output":"str","x":460,"y":160,"wires":[["2ac88ce4e7b298f2"]]},{"id":"2ac88ce4e7b298f2","type":"api-current-state","z":"7afc7ba787cdd45f","name":"Получить текущее состояние","server":"10cea322.6322ad","version":3,"outputs":1,"entity_id":"input_text.wish_message","x":740,"y":60,"wires":[["8cb628e63ccb1ee4"]]},{"id":"8cb628e63ccb1ee4","type":"debug","z":"7afc7ba787cdd45f","name":"debug 5","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":820,"y":160,"wires":[]},{"id":"10cea322.6322ad","type":"server","name":"Home Assistant","addon":true}]