pythononlinegcc commited on
Commit
89e328e
·
verified ·
1 Parent(s): 75e8c76

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -14
app.py CHANGED
@@ -1,29 +1,40 @@
1
  from flask import Flask, request, jsonify, render_template
2
- from g4f.client import ClientFactory
 
3
 
4
  app = Flask(__name__)
5
- client = ClientFactory.create_client("gemini")
6
 
7
  @app.route('/')
8
  def index():
9
- return render_template('index.html')
10
 
11
- @app.route('/run', methods=['POST'])
12
  def run_code():
13
- data = request.json
14
- prompt = data.get('code', '')
 
 
 
 
 
 
15
  try:
 
16
  response = client.chat.completions.create(
17
- model="models/gemini-flash-latest",
18
- messages=[{"role": "user", "content": prompt}],
 
 
19
  )
20
- print(response.choices[0].message.content)
21
- return jsonify({"output": response.choices[0].message.content})
 
 
22
  except Exception as e:
23
- # Имитируем ошибку
24
- print(e)
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
- # Слушаем на всех интерфейсах, порт 5000.
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)