Spaces:
Running
Running
| from flask import Flask, request, jsonify, render_template_string | |
| from groq import Groq | |
| import os | |
| import json | |
| app = Flask(__name__) | |
| client = Groq(api_key=os.environ.get("GROQ_API_KEY")) | |
| # THE MAILBOX (Holds commands for the PC) | |
| PC_QUEUE = [] | |
| def home(): | |
| # Mobile Interface HTML | |
| html = """ | |
| <!DOCTYPE html> | |
| <html> | |
| <head> | |
| <meta name="viewport" content="width=device-width, initial-scale=1"> | |
| <style> | |
| body { background-color: #000; color: #0f0; font-family: monospace; text-align: center; padding: 20px; } | |
| input { width: 80%; padding: 15px; background: #222; border: 1px solid #0f0; color: white; font-size: 16px; margin-bottom: 10px; } | |
| button { padding: 15px 30px; background: #008000; color: white; border: none; font-weight: bold; cursor: pointer; } | |
| #status { margin-top: 20px; font-size: 18px; color: yellow; } | |
| </style> | |
| </head> | |
| <body> | |
| <h1>ACCESS TERMINAL</h1> | |
| <input type="text" id="cmd" placeholder="Say 'Jarvis open calculator'..."> | |
| <button onclick="send()">TRANSMIT</button> | |
| <p id="status">System Ready.</p> | |
| <script> | |
| function send() { | |
| var txt = document.getElementById("cmd").value; | |
| document.getElementById("status").innerText = "Transmitting..."; | |
| fetch('/ask', { | |
| method: 'POST', | |
| headers: {'Content-Type': 'application/json'}, | |
| body: JSON.stringify({text: txt, source: "mobile"}) | |
| }) | |
| .then(r => r.json()) | |
| .then(d => { document.getElementById("status").innerText = d.reply; }); | |
| } | |
| </script> | |
| </body> | |
| </html> | |
| """ | |
| return render_template_string(html) | |
| def ask_brain(): | |
| global PC_QUEUE | |
| try: | |
| data = request.json | |
| user_input = data.get('text', '') | |
| # --- SYSTEM PROMPT (Defines valid actions) --- | |
| system_prompt = """ | |
| You are an OS Controller. Analyze the user request. | |
| Return ONLY valid JSON. | |
| Format: {"action": "action_name", "target": "target_name", "reply": "speech_text"} | |
| Actions: | |
| - "password": if user asks for password. | |
| - "open": if user wants to open app. | |
| - "lock": if user wants to lock the screen. Target is null. | |
| - "shutdown": if user wants to turn off PC. Target is null. | |
| - "chat": for general questions. | |
| """ | |
| completion = client.chat.completions.create( | |
| messages=[ | |
| {"role": "system", "content": system_prompt}, | |
| {"role": "user", "content": user_input} | |
| ], | |
| model="llama-3.3-70b-versatile", | |
| response_format={"type": "json_object"} | |
| ) | |
| ai_resp = completion.choices[0].message.content | |
| parsed = json.loads(ai_resp) | |
| # Add to mailbox for PC | |
| PC_QUEUE.append(parsed) | |
| return jsonify(parsed) | |
| except Exception as e: | |
| return jsonify({"action": "error", "reply": str(e)}) | |
| def get_tasks(): | |
| global PC_QUEUE | |
| # PC calls this to pick up mail | |
| if PC_QUEUE: | |
| tasks = list(PC_QUEUE) | |
| PC_QUEUE = [] | |
| return jsonify(tasks) | |
| else: | |
| return jsonify([]) | |
| if __name__ == '__main__': | |
| app.run(host='0.0.0.0', port=7860) |