Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,7 @@
|
|
| 1 |
from flask import Flask, request, jsonify, render_template_string
|
| 2 |
from groq import Groq
|
| 3 |
import os
|
|
|
|
| 4 |
|
| 5 |
app = Flask(__name__)
|
| 6 |
client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
|
|
@@ -10,7 +11,7 @@ PC_QUEUE = []
|
|
| 10 |
|
| 11 |
@app.route('/')
|
| 12 |
def home():
|
| 13 |
-
# Mobile Interface
|
| 14 |
html = """
|
| 15 |
<!DOCTYPE html>
|
| 16 |
<html>
|
|
@@ -47,35 +48,39 @@ def home():
|
|
| 47 |
return render_template_string(html)
|
| 48 |
|
| 49 |
@app.route('/ask', methods=['POST'])
|
| 50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
system_prompt = """
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
"""
|
| 63 |
-
|
| 64 |
-
# 1. Ask AI to define the action
|
| 65 |
-
try:
|
| 66 |
completion = client.chat.completions.create(
|
| 67 |
messages=[
|
| 68 |
-
{"role": "system", "content":
|
| 69 |
{"role": "user", "content": user_input}
|
| 70 |
],
|
| 71 |
model="llama-3.3-70b-versatile",
|
| 72 |
response_format={"type": "json_object"}
|
| 73 |
)
|
|
|
|
| 74 |
ai_resp = completion.choices[0].message.content
|
| 75 |
-
|
| 76 |
-
parsed = json.loads(ai_resp) # Ensure valid JSON
|
| 77 |
|
| 78 |
-
#
|
| 79 |
PC_QUEUE.append(parsed)
|
| 80 |
|
| 81 |
return jsonify(parsed)
|
|
@@ -86,8 +91,7 @@ def home():
|
|
| 86 |
@app.route('/get_tasks', methods=['GET'])
|
| 87 |
def get_tasks():
|
| 88 |
global PC_QUEUE
|
| 89 |
-
# PC calls this to pick up mail
|
| 90 |
-
# We return the list and then CLEAR it (so we don't do tasks twice).
|
| 91 |
if PC_QUEUE:
|
| 92 |
tasks = list(PC_QUEUE)
|
| 93 |
PC_QUEUE = []
|
|
|
|
| 1 |
from flask import Flask, request, jsonify, render_template_string
|
| 2 |
from groq import Groq
|
| 3 |
import os
|
| 4 |
+
import json
|
| 5 |
|
| 6 |
app = Flask(__name__)
|
| 7 |
client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
|
|
|
|
| 11 |
|
| 12 |
@app.route('/')
|
| 13 |
def home():
|
| 14 |
+
# Mobile Interface HTML
|
| 15 |
html = """
|
| 16 |
<!DOCTYPE html>
|
| 17 |
<html>
|
|
|
|
| 48 |
return render_template_string(html)
|
| 49 |
|
| 50 |
@app.route('/ask', methods=['POST'])
|
| 51 |
+
def ask_brain():
|
| 52 |
+
global PC_QUEUE
|
| 53 |
+
try:
|
| 54 |
+
data = request.json
|
| 55 |
+
user_input = data.get('text', '')
|
| 56 |
+
|
| 57 |
+
# --- SYSTEM PROMPT (Defines valid actions) ---
|
| 58 |
system_prompt = """
|
| 59 |
+
You are an OS Controller. Analyze the user request.
|
| 60 |
+
Return ONLY valid JSON.
|
| 61 |
+
Format: {"action": "action_name", "target": "target_name", "reply": "speech_text"}
|
| 62 |
+
|
| 63 |
+
Actions:
|
| 64 |
+
- "password": if user asks for password.
|
| 65 |
+
- "open": if user wants to open app.
|
| 66 |
+
- "lock": if user wants to lock the screen. Target is null.
|
| 67 |
+
- "shutdown": if user wants to turn off PC. Target is null.
|
| 68 |
+
- "chat": for general questions.
|
| 69 |
"""
|
| 70 |
+
|
|
|
|
|
|
|
| 71 |
completion = client.chat.completions.create(
|
| 72 |
messages=[
|
| 73 |
+
{"role": "system", "content": system_prompt},
|
| 74 |
{"role": "user", "content": user_input}
|
| 75 |
],
|
| 76 |
model="llama-3.3-70b-versatile",
|
| 77 |
response_format={"type": "json_object"}
|
| 78 |
)
|
| 79 |
+
|
| 80 |
ai_resp = completion.choices[0].message.content
|
| 81 |
+
parsed = json.loads(ai_resp)
|
|
|
|
| 82 |
|
| 83 |
+
# Add to mailbox for PC
|
| 84 |
PC_QUEUE.append(parsed)
|
| 85 |
|
| 86 |
return jsonify(parsed)
|
|
|
|
| 91 |
@app.route('/get_tasks', methods=['GET'])
|
| 92 |
def get_tasks():
|
| 93 |
global PC_QUEUE
|
| 94 |
+
# PC calls this to pick up mail
|
|
|
|
| 95 |
if PC_QUEUE:
|
| 96 |
tasks = list(PC_QUEUE)
|
| 97 |
PC_QUEUE = []
|