import os
import subprocess
import tempfile
from flask import Flask, render_template_string, request, jsonify
from groq import Groq
app = Flask(__name__)
# Exypos AI üçün Groq API
GROQ_API_KEY = os.getenv("GROQ_API_KEY")
client_groq = Groq(api_key=GROQ_API_KEY) if GROQ_API_KEY else None
HTML_TEMPLATE = """
Twenty IDE | Premium Workspace
"""
@app.route('/')
def index():
return render_template_string(HTML_TEMPLATE)
@app.route('/run', methods=['POST'])
def run_code():
data = request.json
code = data.get('code', '')
is_shell = data.get('is_shell', False)
if not code.strip():
return jsonify({"status": "error", "output": "Boş kod göndərildi."})
if is_shell:
shell_cmd = code.strip()[1:]
try:
result = subprocess.run(shell_cmd, shell=True, capture_output=True, text=True, timeout=60)
output = result.stdout if result.returncode == 0 else result.stderr
status = "success" if result.returncode == 0 else "error"
return jsonify({"status": status, "output": output})
except Exception as e:
return jsonify({"status": "error", "output": str(e)})
with tempfile.NamedTemporaryFile(mode='w', suffix='.py', delete=False, encoding='utf-8') as temp_file:
temp_file.write(code)
temp_filepath = temp_file.name
try:
result = subprocess.run(['python', temp_filepath], capture_output=True, text=True, timeout=15)
if result.returncode == 0:
return jsonify({"status": "success", "output": result.stdout})
else:
return jsonify({"status": "error", "output": result.stderr if result.stderr else result.stdout})
except subprocess.TimeoutExpired:
return jsonify({"status": "error", "output": "Vaxt bitdi (15 saniyə limit). Sonsuz dövrə (while True) düşmüş ola bilərsiniz."})
except Exception as e:
return jsonify({"status": "error", "output": str(e)})
finally:
if os.path.exists(temp_filepath):
os.remove(temp_filepath)
@app.route('/ask_ai', methods=['POST'])
def ask_ai():
if not client_groq:
return jsonify({"reply": "Xəta: Groq API Key (GROQ_API_KEY) tapılmadı."})
data = request.json
prompt = data.get('prompt', '')
context_code = data.get('code', '')
system_msg = "Sən Exypos AI-san, Twenty OS daxilində çalışan güclü kod köməkçisisən. Qısa, dəqiq və azərbaycanca cavab ver. Əgər kod düzəldirsənsə, mütləq mətndə yalnız python kodunu ```python ... ``` blokunun içinə yaz."
user_msg = f"Sual: {prompt}\n\nHazırki kodum:\n```python\n{context_code}\n```"
try:
chat_completion = client_groq.chat.completions.create(
messages=[
{"role": "system", "content": system_msg},
{"role": "user", "content": user_msg}
],
model="llama-3.1-8b-instant",
temperature=0.5,
max_tokens=1500
)
return jsonify({"reply": chat_completion.choices[0].message.content})
except Exception as e:
return jsonify({"reply": f"AI Bağlantı Xətası: {str(e)}"})
if __name__ == "__main__":
app.run(host="0.0.0.0", port=7860, debug=False)