Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,29 +1,40 @@
|
|
| 1 |
from flask import Flask, request, jsonify, render_template
|
| 2 |
-
from
|
|
|
|
| 3 |
|
| 4 |
app = Flask(__name__)
|
| 5 |
-
client =
|
| 6 |
|
| 7 |
@app.route('/')
|
| 8 |
def index():
|
| 9 |
-
return render_template('
|
| 10 |
|
| 11 |
-
@app.route(
|
| 12 |
def run_code():
|
| 13 |
-
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
try:
|
|
|
|
| 16 |
response = client.chat.completions.create(
|
| 17 |
-
model="
|
| 18 |
-
messages=[{"role": "user", "content":
|
|
|
|
|
|
|
| 19 |
)
|
| 20 |
-
|
| 21 |
-
|
|
|
|
|
|
|
| 22 |
except Exception as e:
|
| 23 |
-
#
|
| 24 |
-
|
| 25 |
-
return jsonify({"output": f"Traceback (most recent call last):\n File \"main.py\", line 1, in <module>\nRunTimeError: AI server connection failed: {str(e)}"}), 500
|
| 26 |
|
| 27 |
if __name__ == '__main__':
|
| 28 |
-
#
|
| 29 |
app.run(host='0.0.0.0', port=7860)
|
|
|
|
| 1 |
from flask import Flask, request, jsonify, render_template
|
| 2 |
+
from flask_cors import CORS
|
| 3 |
+
from huggingface_hub import InferenceClient
|
| 4 |
|
| 5 |
app = Flask(__name__)
|
| 6 |
+
client = InferenceClient(api_key='')
|
| 7 |
|
| 8 |
@app.route('/')
|
| 9 |
def index():
|
| 10 |
+
return render_template('index2.html')
|
| 11 |
|
| 12 |
+
@app.route("/run", methods=["POST"])
|
| 13 |
def run_code():
|
| 14 |
+
"""Синхронизировано с фронтендом: принимаем 'code', отдаем 'output'"""
|
| 15 |
+
data = request.get_json()
|
| 16 |
+
# Берем содержимое редактора
|
| 17 |
+
user_code = data.get("code", "").strip()
|
| 18 |
+
|
| 19 |
+
if not user_code:
|
| 20 |
+
return jsonify({"output": "Error: Empty request"}), 400
|
| 21 |
+
|
| 22 |
try:
|
| 23 |
+
# Модель лучше указать реальную, например Qwen 2.5 Coder
|
| 24 |
response = client.chat.completions.create(
|
| 25 |
+
model="deepseek-ai/DeepSeek-V3.2",
|
| 26 |
+
messages=[{"role": "user", "content": user_code}],
|
| 27 |
+
max_tokens=2000,
|
| 28 |
+
temperature=0.5
|
| 29 |
)
|
| 30 |
+
|
| 31 |
+
answer = response.choices[0].message.content
|
| 32 |
+
return jsonify({"output": answer})
|
| 33 |
+
|
| 34 |
except Exception as e:
|
| 35 |
+
# Если API отвалилось — это трында, возвращаем ошибку как вывод терминала
|
| 36 |
+
return jsonify({"output": f"Traceback (most recent call last):\n File \"main.py\", line 1, in <module>\nRuntimeError: {str(e)}"}), 500
|
|
|
|
| 37 |
|
| 38 |
if __name__ == '__main__':
|
| 39 |
+
# Порт 7860 обязателен для Hugging Face Spaces
|
| 40 |
app.run(host='0.0.0.0', port=7860)
|