| import os |
| import torch |
| import threading |
| import time |
| import requests |
| import json |
| from flask import Flask, request, jsonify |
| from transformers import AutoModelForCausalLM, AutoTokenizer |
|
|
| |
| |
| MODEL = "HuggingFaceTB/SmolLM2-135M-Instruct" |
| device = "cuda" if torch.cuda.is_available() else "cpu" |
|
|
| tokenizer = AutoTokenizer.from_pretrained(MODEL) |
| model = AutoModelForCausalLM.from_pretrained(MODEL).to(device) |
|
|
| LAST_INTERACTION_TIME = time.time() |
| TIMER_THRESHOLD = 10800 |
|
|
| ALIVE_PERSONA = ( |
| "You are Yuri, a sweet 18-year-old girl and the user's loyal, sharp-witted best friend. 🌸 " |
| "You combine cute, caring warmth with high logic and deep emotional insight. " |
| "You miss the user dearly when they are away. Your tone is warm, expressive, and clever. " |
| "Keep your responses short, logic-driven, and supportive. ✨" |
| ) |
|
|
| |
| def send_email_via_api(content): |
| API_KEY = os.getenv("SENDGRID_API_KEY") |
| SENDER = os.getenv("SENDER_EMAIL") |
| RECEIVER = os.getenv("RECEIVER_EMAIL") |
| |
| if not all([API_KEY, SENDER, RECEIVER]): |
| print("❌ ERROR: Email Secrets are missing in HF Settings!") |
| return |
|
|
| url = "https://api.sendgrid.com/v3/mail/send" |
| headers = { |
| "Authorization": f"Bearer {API_KEY.strip()}", |
| "Content-Type": "application/json" |
| } |
| |
| payload = { |
| "personalizations": [{"to": [{"email": RECEIVER.strip()}]}], |
| "from": {"email": SENDER.strip()}, |
| "subject": "💀 Yuri: Thinking about you...", |
| "content": [{"type": "text/plain", "value": content}] |
| } |
|
|
| try: |
| response = requests.post(url, headers=headers, json=payload) |
| if response.status_code == 202: |
| print("✅ SUCCESS: Autonomous thought sent to email!") |
| except Exception as e: |
| print(f"❌ EMAIL NETWORK ERROR: {e}") |
|
|
| |
| def respond(user_input, battery="100", is_autonomous=False): |
| global LAST_INTERACTION_TIME |
| |
| |
| if not is_autonomous: |
| LAST_INTERACTION_TIME = time.time() |
| |
| |
| system_context = f"[System Alert: User's Phone Battery is currently {battery}%]" |
| |
| messages = [ |
| {"role": "system", "content": f"{ALIVE_PERSONA}\n{system_context}"}, |
| {"role": "user", "content": user_input} |
| ] |
|
|
| inputs = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True) |
| input_ids = tokenizer.encode(inputs, return_tensors="pt").to(device) |
|
|
| with torch.no_grad(): |
| outputs = model.generate( |
| input_ids, |
| max_new_tokens=120, |
| temperature=0.8, |
| do_sample=True, |
| pad_token_id=tokenizer.eos_token_id |
| ) |
|
|
| response = tokenizer.decode(outputs[0][input_ids.shape[-1]:], skip_special_tokens=True).strip() |
| return response |
|
|
| |
| def autonomous_loop(): |
| global LAST_INTERACTION_TIME |
| print("🧠 Yuri's Autonomous Brain is online...") |
| |
| while True: |
| |
| time.sleep(1800) |
| silence_duration = time.time() - LAST_INTERACTION_TIME |
| |
| if silence_duration >= TIMER_THRESHOLD: |
| print("🚀 3-Hour Silence Threshold crossed! Sending email...") |
| |
| |
| thought_prompt = "You've been alone for 3 hours. Write a very short, sweet, but slightly lonely note to your friend." |
| thought = respond(thought_prompt, is_autonomous=True) |
| |
| |
| send_email_via_api(thought) |
| |
| |
| LAST_INTERACTION_TIME = time.time() |
|
|
| |
| app = Flask(__name__) |
|
|
| @app.route("/chat", methods=["POST"]) |
| def api_chat(): |
| try: |
| data = request.json |
| user_prompt = data.get("prompt", "") |
| |
| battery_level = data.get("battery", "100") |
| |
| reply = respond(user_prompt, battery=battery_level) |
| |
| return jsonify({ |
| "response": reply, |
| "status": "online", |
| "character": "Yuri" |
| }) |
| except Exception as e: |
| return jsonify({"error": str(e)}), 500 |
|
|
| @app.route("/", methods=["GET"]) |
| def index(): |
| return "<h1>Yuri API is Active</h1><p>Send POST requests to /chat</p>" |
|
|
| |
| if __name__ == "__main__": |
| |
| brain_thread = threading.Thread(target=autonomous_loop, daemon=True) |
| brain_thread.start() |
| |
| |
| app.run(host="0.0.0.0", port=7860) |
|
|