Devices & Components
Robu's Arduino UNO Q
USB C
Software & Tools
Arduino App Lab
Project description
Code
Offline LLM chatbot code
python
nano.py file
1from flask import Flask, request, jsonify, render_template_string 2import requests 3 4app = Flask(__name__) 5 6LLM_URL = "http://localhost:8080/v1/chat/completions" 7 8HTML = """ 9<!DOCTYPE html> 10<html> 11<head> 12 <title>SmolLM Chat</title> 13 <style> 14 body { font-family: Arial; background:#111; color:#eee; } 15 #chat { width:80%; margin:auto; margin-top:30px; } 16 .msg { padding:10px; margin:10px 0; border-radius:10px; } 17 .user { background:#2a2a2a; text-align:right; } 18 .bot { background:#1e3a5f; } 19 input { width:80%; padding:10px; } 20 button { padding:10px; } 21 </style> 22</head> 23<body> 24<div id="chat"> 25 <h2>SmolLM2 Web Chat</h2> 26 <div id="messages"></div> 27 <input id="input" placeholder="Type message..." /> 28 <button onclick="send()">Send</button> 29</div> 30 31<script> 32async function send() { 33 let input = document.getElementById("input"); 34 let msg = input.value; 35 if (!msg) return; 36 37 let messages = document.getElementById("messages"); 38 messages.innerHTML += `<div class='msg user'>${msg}</div>`; 39 input.value = ""; 40 41 let response = await fetch("/chat", { 42 method: "POST", 43 headers: {"Content-Type": "application/json"}, 44 body: JSON.stringify({message: msg}) 45 }); 46 47 let data = await response.json(); 48 messages.innerHTML += `<div class='msg bot'>${data.reply}</div>`; 49} 50</script> 51</body> 52</html> 53""" 54 55def ask_llm(prompt): 56 payload = { 57 "model": "smollm2", 58 "messages": [ 59 {"role": "user", "content": prompt} 60 ], 61 "max_tokens": 200 62 } 63 r = requests.post(LLM_URL, json=payload) 64 return r.json()["choices"][0]["message"]["content"] 65 66@app.route("/") 67def index(): 68 return render_template_string(HTML) 69 70@app.route("/chat", methods=["POST"]) 71def chat(): 72 user_msg = request.json["message"] 73 reply = ask_llm(user_msg) 74 return jsonify({"reply": reply}) 75 76if __name__ == "__main__": 77 app.run(host="0.0.0.0", port=7000)
Comments
Only logged in users can leave comments