Ensarioglu commited on
Commit
3620f04
·
verified ·
1 Parent(s): 65f48f1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -19
app.py CHANGED
@@ -4,39 +4,34 @@ import os
4
 
5
  app = Flask(__name__)
6
 
7
- # Token'ın doğruluğundan emin ol (Settings -> Secrets kısmında HF_TOKEN olmalı)
8
  HF_TOKEN = os.getenv("HF_TOKEN")
9
-
10
- # En stabil model ve doğru router adresi
11
  API_URL = "https://router.huggingface.co/models/meta-llama/Meta-Llama-3-8B-Instruct"
12
 
13
  @app.route("/", methods=["POST"])
14
  def handle_request():
15
  try:
16
  data = request.get_json()
17
- soru = data.get("soru", "Merhaba")
18
-
19
- headers = {
20
- "Authorization": f"Bearer {HF_TOKEN}",
21
- "Content-Type": "application/json"
22
- }
23
 
24
- payload = {
25
- "inputs": soru,
26
- "parameters": {"max_new_tokens": 150, "wait_for_model": True}
27
- }
28
 
29
- # Hugging Face'e istek at
30
- response = requests.post(API_URL, headers=headers, json=payload, timeout=25)
31
 
32
- # Boş yanıt kontrolü
33
- if not response.text or response.text.strip() == "":
34
- return jsonify({"error": "Hugging Face bos yanit dondu, tekrar dene."}), 502
 
 
 
 
35
 
36
  return jsonify(response.json())
37
 
38
  except Exception as e:
39
- return jsonify({"error": "Sunucu Hatasi", "detay": str(e)}), 500
40
 
41
  if __name__ == "__main__":
42
  app.run(host="0.0.0.0", port=7860)
 
4
 
5
  app = Flask(__name__)
6
 
7
+ # Settings -> Secrets kısmındaki ismiyle birebir aynı olmalı
8
  HF_TOKEN = os.getenv("HF_TOKEN")
 
 
9
  API_URL = "https://router.huggingface.co/models/meta-llama/Meta-Llama-3-8B-Instruct"
10
 
11
  @app.route("/", methods=["POST"])
12
  def handle_request():
13
  try:
14
  data = request.get_json()
15
+ soru = data.get("soru", "Messi vs Ronaldo")
 
 
 
 
 
16
 
17
+ headers = {"Authorization": f"Bearer {HF_TOKEN}", "Content-Type": "application/json"}
18
+ payload = {"inputs": soru, "parameters": {"max_new_tokens": 100}}
 
 
19
 
20
+ # İSTEĞİ GÖNDER
21
+ response = requests.post(API_URL, headers=headers, json=payload, timeout=20)
22
 
23
+ # EĞER HUGGING FACE HATA VERİRSE DETAYI GODOT'YA YOLLA
24
+ if response.status_code != 200:
25
+ return jsonify({
26
+ "error": "HF_HATASI",
27
+ "detay": response.text,
28
+ "kod": response.status_code
29
+ }), response.status_code
30
 
31
  return jsonify(response.json())
32
 
33
  except Exception as e:
34
+ return jsonify({"error": "FLASK_HATASI", "detay": str(e)}), 500
35
 
36
  if __name__ == "__main__":
37
  app.run(host="0.0.0.0", port=7860)