Ensarioglu commited on
Commit
cc96503
·
verified ·
1 Parent(s): 0b450ff

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -18
app.py CHANGED
@@ -1,46 +1,64 @@
1
  from flask import Flask, request, jsonify
2
  import requests
3
  import os
 
4
 
5
  app = Flask(__name__)
6
 
7
  HF_TOKEN = os.getenv("HF_TOKEN")
8
  API_URL = "https://router.huggingface.co/v1/chat/completions"
9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
  @app.route("/", methods=["POST"])
11
  def handle_request():
 
 
 
 
12
  try:
13
  data = request.get_json()
14
  istatistikler = data.get("istatistikler", "")
15
 
16
  headers = {"Authorization": f"Bearer {HF_TOKEN}", "Content-Type": "application/json"}
17
-
18
  payload = {
19
- "model": "meta-llama/Llama-3.3-70B-Instruct", # Limit testi için en kararlı model
20
- "messages": [{"role": "user", "content": istatistikler}],
21
- "max_tokens": 100
 
22
  }
23
 
24
- response = requests.post(API_URL, headers=headers, json=payload, timeout=30)
25
 
26
- # --- LİMİT SORGULAMA MANTIĞI ---
27
- # HF başlıklarından limit bilgilerini çekiyoruz
28
- limit_bilgisi = {
29
- "ratelimit_limit": response.headers.get("x-ratelimit-limit-requests", "Belirsiz"),
30
- "ratelimit_remaining": response.headers.get("x-ratelimit-remaining-requests", "Belirsiz"),
31
- "ratelimit_reset": response.headers.get("x-ratelimit-reset", "0")
32
- }
33
 
34
  if response.status_code == 200:
35
- analiz = response.json()['choices'][0]['message']['content']
36
  return jsonify({
37
- "puanlar": analiz,
38
- "kalan_hak": limit_bilgisi["ratelimit_remaining"],
39
- "toplam_limit": limit_bilgisi["ratelimit_limit"],
40
- "sifirlanma_suresi": limit_bilgisi["ratelimit_reset"]
 
41
  })
42
  else:
43
- return jsonify({"error": "Limit hatası veya API kapalı", "kod": response.status_code}), response.status_code
44
 
45
  except Exception as e:
46
  return jsonify({"error": str(e)}), 500
 
1
  from flask import Flask, request, jsonify
2
  import requests
3
  import os
4
+ import re
5
 
6
  app = Flask(__name__)
7
 
8
  HF_TOKEN = os.getenv("HF_TOKEN")
9
  API_URL = "https://router.huggingface.co/v1/chat/completions"
10
 
11
+ MODELLER = [
12
+ "meta-llama/Llama-4-Maverick-17B-128E-Instruct-FP8",
13
+ "meta-llama/Llama-3.3-70B-Instruct",
14
+ "deepseek-ai/DeepSeek-V3",
15
+ "meta-llama/Llama-3.1-70B-Instruct",
16
+ "Qwen/Qwen2.5-72B-Instruct-AWQ",
17
+ "google/gemma-2-27b-it",
18
+ "mistralai/Mistral-Small-Instruct-2409",
19
+ "microsoft/Phi-3-medium-4k-instruct",
20
+ "01-ai/Yi-1.5-34B-Chat",
21
+ "meta-llama/Meta-Llama-3-70B-Instruct"
22
+ ]
23
+
24
+ model_sayaci = 0
25
+
26
  @app.route("/", methods=["POST"])
27
  def handle_request():
28
+ global model_sayaci
29
+ secilen_model = MODELLER[model_sayaci % len(MODELLER)]
30
+ model_sayaci += 1
31
+
32
  try:
33
  data = request.get_json()
34
  istatistikler = data.get("istatistikler", "")
35
 
36
  headers = {"Authorization": f"Bearer {HF_TOKEN}", "Content-Type": "application/json"}
 
37
  payload = {
38
+ "model": secilen_model,
39
+ "messages": [{"role": "system", "content": "Sadece 'İsim: Puan' yaz."}, {"role": "user", "content": istatistikler}],
40
+ "max_tokens": 150,
41
+ "temperature": 0.1
42
  }
43
 
44
+ response = requests.post(API_URL, headers=headers, json=payload, timeout=40)
45
 
46
+ # --- BURASI ÖNEMLİ: LİMİT BİLGİSİNİ ÇEKİYORUZ ---
47
+ kalan = response.headers.get("x-ratelimit-remaining-requests", "Gizli")
48
+ toplam = response.headers.get("x-ratelimit-limit-requests", "Gizli")
49
+ sifirlanma = response.headers.get("x-ratelimit-reset", "0")
 
 
 
50
 
51
  if response.status_code == 200:
52
+ cevap = response.json()['choices'][0]['message']['content']
53
  return jsonify({
54
+ "model": secilen_model,
55
+ "puanlar": cevap.replace(".", ","),
56
+ "kalan_hak": kalan,
57
+ "toplam_limit": toplam,
58
+ "reset": sifirlanma
59
  })
60
  else:
61
+ return jsonify({"error": "Limit Doldu", "kod": response.status_code, "kalan_hak": "0"}), response.status_code
62
 
63
  except Exception as e:
64
  return jsonify({"error": str(e)}), 500