-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
99 lines (81 loc) · 3.9 KB
/
Copy pathmain.py
File metadata and controls
99 lines (81 loc) · 3.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
from flask import Flask, request, jsonify, render_template
from flask_cors import CORS
from dotenv import load_dotenv
import os, requests
import os
from dotenv import load_dotenv
# Only load the local .env file when we're NOT on Render
if not os.getenv("RENDER"):
load_dotenv() # for local development only
api_key = os.getenv("GROQ_API_KEY")
print("Loaded GROQ_API_KEY:", api_key[:8] + "…" if api_key else None)
load_dotenv() # ⬅️ picks up GROQ_API_KEY, etc.
app = Flask(__name__)
CORS(app)
print("Loaded GROQ_API_KEY:", os.getenv("GROQ_API_KEY"))
@app.route("/")
def index():
return render_template("index.html")
@app.route("/chat", methods=["POST"])
def chat():
try:
data = request.get_json(force=True)
prompt = data.get("prompt", "").strip()
preferences = data.get("preferences", {})
if not prompt:
return jsonify({"response": "No spell detected. Try again."}), 400
headers = {
"Authorization": f"Bearer {os.getenv('GROQ_API_KEY')}",
"Content-Type": "application/json"
}
# Build dynamic system prompt based on preferences
daw = preferences.get("daw", "ableton")
plugins = preferences.get("plugins", [])
ableton_devices = preferences.get("abletonDevices", ["wavetable", "operator"])
complexity = preferences.get("complexity", "Intermediate")
# Create device lists
if ableton_devices:
device_list = ", ".join([d.title() for d in ableton_devices])
else:
device_list = "Wavetable, Operator, Bass, Analog, Impulse, Simpler"
plugin_text = ""
if plugins:
plugin_text = f" You can also suggest these third-party plugins: {', '.join([p.title() for p in plugins])}."
complexity_instructions = {
"Minimal": "Keep it very simple - 1-2 devices max, basic parameter changes only.",
"Beginner": "Use 2-3 devices, explain each step clearly with beginner-friendly language.",
"Intermediate": "Use 3-4 devices, moderate complexity, assume basic DAW knowledge.",
"Advanced": "Use 4-6 devices, complex routing, advanced techniques welcome.",
"Expert": "Full complexity, advanced modulation, intricate routing, assume expert knowledge."
}
system_prompt = (f"You are ArcSyn, a {daw.title()} expert. Give EXACT, ACTIONABLE instructions. "
f"Complexity level: {complexity} - {complexity_instructions.get(complexity, '')} "
f"Focus on these {daw.title()} devices: {device_list}.{plugin_text} "
f"Format: Device > Parameter: Value. Example: 'Wavetable > Osc 1 Position: 45%, Filter: Low Pass 24dB, Cutoff: 800Hz'. "
f"Always specify exact routing and be copy-paste ready for {daw.title()}.")
payload = {
"model": "llama3-70b-8192",
"messages": [
{"role": "system", "content": system_prompt},
{"role": "user", "content": prompt}
]
}
res = requests.post(
"https://api.groq.com/openai/v1/chat/completions",
headers=headers,
json=payload,
timeout=30
)
res.raise_for_status()
message = res.json()["choices"][0]["message"]["content"]
return jsonify({"response": message})
except Exception as e:
# log the exception and return 500
print("Error in /chat:", e)
return jsonify({"response": "💥 The arcane circuit fizzled."}), 500
# ---------- local dev only ----------
if __name__ == "__main__":
# Render (and most PaaS) sets $PORT. Default to 5000 for local runs.
port = int(os.environ.get("PORT", 5000))
# host 0.0.0.0 so Docker/Render can reach it if you test in containers
app.run(host="0.0.0.0", port=port, debug=False)