Ensarioglu commited on
Commit
0dfa461
·
verified ·
1 Parent(s): fa5244c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -24
app.py CHANGED
@@ -5,40 +5,42 @@ import os
5
  app = Flask(__name__)
6
 
7
  HF_TOKEN = os.getenv("HF_TOKEN")
 
 
8
 
9
  @app.route("/", methods=["POST"])
10
  def handle_request():
11
  try:
12
- # 1. Önce anahtarın (token) çalışıp çalışmadığını Hugging Face'e soralım
13
- whoami_url = "https://huggingface.co/api/whoami-v2"
14
- headers = {"Authorization": f"Bearer {HF_TOKEN}"}
15
- user_info = requests.get(whoami_url, headers=headers)
16
-
17
- if user_info.status_code != 200:
18
- return jsonify({"error": "TOKEN_GECERSIZ", "detay": user_info.text}), 401
19
-
20
- # 2. Godot'dan gelen soruyu al
21
  data = request.get_json()
22
- soru = data.get("soru", "")
23
-
24
- # 3. Model Denemesi (En garanti model olan GPT-2 veya Mistral)
25
- # Eğer Llama 3.2 yoksa, sistem otomatik olarak 'google/gemma-2-2b-it' deneyecek
26
- API_URL = "https://router.huggingface.co/models/google/gemma-2-2b-it"
 
 
 
 
 
 
 
 
 
 
 
 
27
 
28
- payload = {"inputs": soru, "parameters": {"max_new_tokens": 100, "wait_for_model": True}}
29
- response = requests.post(API_URL, headers=headers, json=payload, timeout=20)
30
-
31
  if response.status_code == 200:
32
- return jsonify({"durum": "BASARILI", "cevap": response.json()})
 
 
33
  else:
34
  return jsonify({
35
- "error": "MODEL_ERISIM_HATASI",
36
- "mesaj": "Bu model anahtarına kapalı veya adres yanlış.",
37
- "hf_kod": response.status_code,
38
- "hf_detay": response.text,
39
- "anahtar_sahibi": user_info.json().get("name", "Bilinmiyor")
40
  }), response.status_code
41
-
42
  except Exception as e:
43
  return jsonify({"error": "SUNUCU_HATASI", "detay": str(e)}), 500
44
 
 
5
  app = Flask(__name__)
6
 
7
  HF_TOKEN = os.getenv("HF_TOKEN")
8
+ # 2026 GÜNCEL ROUTER ADRESİ
9
+ API_URL = "https://router.huggingface.co/v1/chat/completions"
10
 
11
  @app.route("/", methods=["POST"])
12
  def handle_request():
13
  try:
 
 
 
 
 
 
 
 
 
14
  data = request.get_json()
15
+ soru = data.get("soru", "NoxBall nedir?")
16
+
17
+ headers = {
18
+ "Authorization": f"Bearer {HF_TOKEN}",
19
+ "Content-Type": "application/json"
20
+ }
21
+
22
+ # 2026 Yeni Mesaj Formatı (OpenAI uyumlu)
23
+ payload = {
24
+ "model": "meta-llama/Llama-3.1-8B-Instruct", # Aktif ve ücretsiz model
25
+ "messages": [
26
+ {"role": "user", "content": soru}
27
+ ],
28
+ "max_tokens": 150
29
+ }
30
+
31
+ response = requests.post(API_URL, headers=headers, json=payload, timeout=30)
32
 
 
 
 
33
  if response.status_code == 200:
34
+ # Gelen cevaptan sadece metni ayıklayıp Godot'ya yolluyoruz
35
+ cevap_metni = response.json()['choices'][0]['message']['content']
36
+ return jsonify({"analiz": cevap_metni})
37
  else:
38
  return jsonify({
39
+ "error": "HF_HATASI",
40
+ "kod": response.status_code,
41
+ "detay": response.text
 
 
42
  }), response.status_code
43
+
44
  except Exception as e:
45
  return jsonify({"error": "SUNUCU_HATASI", "detay": str(e)}), 500
46