Spaces:
Sleeping
Sleeping
Commit
·
2e3e6dd
1
Parent(s):
18a2288
debug
Browse files- app/main.py +30 -31
app/main.py
CHANGED
|
@@ -6,6 +6,7 @@ from datetime import datetime
|
|
| 6 |
import os
|
| 7 |
import requests
|
| 8 |
from typing import Dict
|
|
|
|
| 9 |
|
| 10 |
app = FastAPI()
|
| 11 |
|
|
@@ -34,7 +35,8 @@ def load_cache() -> Dict:
|
|
| 34 |
with open(CACHE_FILE, "r", encoding="utf-8") as f:
|
| 35 |
return json.load(f)
|
| 36 |
except Exception as e:
|
| 37 |
-
|
|
|
|
| 38 |
return {}
|
| 39 |
return {}
|
| 40 |
|
|
@@ -45,7 +47,9 @@ def save_cache(cache_data: Dict) -> None:
|
|
| 45 |
with open(CACHE_FILE, "w", encoding="utf-8") as f:
|
| 46 |
json.dump(cache_data, f, ensure_ascii=False, indent=2)
|
| 47 |
except Exception as e:
|
| 48 |
-
|
|
|
|
|
|
|
| 49 |
|
| 50 |
|
| 51 |
async def generate_menu() -> Dict:
|
|
@@ -69,8 +73,10 @@ async def generate_menu() -> Dict:
|
|
| 69 |
}]
|
| 70 |
}
|
| 71 |
|
|
|
|
| 72 |
response = requests.post(API_URL, json=data, headers=headers, timeout=10)
|
| 73 |
print(f"API yanıtı status: {response.status_code}")
|
|
|
|
| 74 |
|
| 75 |
if response.status_code == 200:
|
| 76 |
menu_text = response.json()['choices'][0]['message']['content']
|
|
@@ -79,32 +85,14 @@ async def generate_menu() -> Dict:
|
|
| 79 |
"menu": menu_text
|
| 80 |
}
|
| 81 |
else:
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
"date": get_current_date(),
|
| 86 |
-
"menu": """Kahvaltı:
|
| 87 |
-
- Karışık kahvaltı tabağı (zeytin, peynir, domates, salatalık, bal, tereyağı)
|
| 88 |
-
- Haşlanmış yumurta
|
| 89 |
-
- Simit
|
| 90 |
-
- Çay
|
| 91 |
-
|
| 92 |
-
Öğle:
|
| 93 |
-
- Mercimek çorbası
|
| 94 |
-
- Izgara köfte
|
| 95 |
-
- Pilav
|
| 96 |
-
- Mevsim salata
|
| 97 |
-
|
| 98 |
-
Akşam:
|
| 99 |
-
- Sebzeli tavuk sote
|
| 100 |
-
- Bulgur pilavı
|
| 101 |
-
- Cacık
|
| 102 |
-
- Revani tatlısı"""
|
| 103 |
-
}
|
| 104 |
|
| 105 |
except Exception as e:
|
| 106 |
-
|
| 107 |
-
|
|
|
|
| 108 |
|
| 109 |
|
| 110 |
@app.get("/menu")
|
|
@@ -114,26 +102,35 @@ async def get_menu():
|
|
| 114 |
cache = load_cache()
|
| 115 |
current_date = get_current_date()
|
| 116 |
|
|
|
|
|
|
|
|
|
|
| 117 |
# Cache kontrolü
|
| 118 |
if current_date in cache:
|
|
|
|
| 119 |
return cache[current_date]
|
| 120 |
|
|
|
|
| 121 |
# Yeni menü oluştur
|
| 122 |
menu_data = await generate_menu()
|
| 123 |
|
| 124 |
# Cache'e kaydet
|
| 125 |
try:
|
|
|
|
| 126 |
cache[current_date] = menu_data
|
| 127 |
save_cache(cache)
|
|
|
|
| 128 |
except Exception as e:
|
| 129 |
-
|
|
|
|
| 130 |
# Cache hatası olsa bile menüyü döndür
|
| 131 |
pass
|
| 132 |
|
| 133 |
return menu_data
|
| 134 |
except Exception as e:
|
| 135 |
-
|
| 136 |
-
|
|
|
|
| 137 |
|
| 138 |
|
| 139 |
@app.post("/reset")
|
|
@@ -142,10 +139,12 @@ async def reset_cache():
|
|
| 142 |
try:
|
| 143 |
if os.path.exists(CACHE_FILE):
|
| 144 |
os.remove(CACHE_FILE)
|
|
|
|
| 145 |
return {"message": "Cache temizlendi"}
|
| 146 |
except Exception as e:
|
| 147 |
-
|
| 148 |
-
|
|
|
|
| 149 |
|
| 150 |
|
| 151 |
@app.get("/health")
|
|
|
|
| 6 |
import os
|
| 7 |
import requests
|
| 8 |
from typing import Dict
|
| 9 |
+
import traceback
|
| 10 |
|
| 11 |
app = FastAPI()
|
| 12 |
|
|
|
|
| 35 |
with open(CACHE_FILE, "r", encoding="utf-8") as f:
|
| 36 |
return json.load(f)
|
| 37 |
except Exception as e:
|
| 38 |
+
error_detail = f"Cache okuma hatası: {str(e)}\nTrace: {traceback.format_exc()}"
|
| 39 |
+
print(error_detail)
|
| 40 |
return {}
|
| 41 |
return {}
|
| 42 |
|
|
|
|
| 47 |
with open(CACHE_FILE, "w", encoding="utf-8") as f:
|
| 48 |
json.dump(cache_data, f, ensure_ascii=False, indent=2)
|
| 49 |
except Exception as e:
|
| 50 |
+
error_detail = f"Cache yazma hatası: {str(e)}\nTrace: {traceback.format_exc()}"
|
| 51 |
+
print(error_detail)
|
| 52 |
+
raise Exception(error_detail)
|
| 53 |
|
| 54 |
|
| 55 |
async def generate_menu() -> Dict:
|
|
|
|
| 73 |
}]
|
| 74 |
}
|
| 75 |
|
| 76 |
+
print("API isteği gönderiliyor...")
|
| 77 |
response = requests.post(API_URL, json=data, headers=headers, timeout=10)
|
| 78 |
print(f"API yanıtı status: {response.status_code}")
|
| 79 |
+
print(f"API yanıtı: {response.text}")
|
| 80 |
|
| 81 |
if response.status_code == 200:
|
| 82 |
menu_text = response.json()['choices'][0]['message']['content']
|
|
|
|
| 85 |
"menu": menu_text
|
| 86 |
}
|
| 87 |
else:
|
| 88 |
+
error_detail = f"API hatası: Status Code {response.status_code}, Response: {response.text}"
|
| 89 |
+
print(error_detail)
|
| 90 |
+
raise Exception(error_detail)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
|
| 92 |
except Exception as e:
|
| 93 |
+
error_detail = f"Menü oluşturma hatası: {str(e)}\nTrace: {traceback.format_exc()}"
|
| 94 |
+
print(error_detail)
|
| 95 |
+
raise Exception(error_detail)
|
| 96 |
|
| 97 |
|
| 98 |
@app.get("/menu")
|
|
|
|
| 102 |
cache = load_cache()
|
| 103 |
current_date = get_current_date()
|
| 104 |
|
| 105 |
+
print(f"Cache kontrolü yapılıyor... Tarih: {current_date}")
|
| 106 |
+
print(f"Mevcut cache: {json.dumps(cache, ensure_ascii=False)}")
|
| 107 |
+
|
| 108 |
# Cache kontrolü
|
| 109 |
if current_date in cache:
|
| 110 |
+
print("Cache'den menü döndürülüyor")
|
| 111 |
return cache[current_date]
|
| 112 |
|
| 113 |
+
print("Yeni menü oluşturuluyor...")
|
| 114 |
# Yeni menü oluştur
|
| 115 |
menu_data = await generate_menu()
|
| 116 |
|
| 117 |
# Cache'e kaydet
|
| 118 |
try:
|
| 119 |
+
print("Cache'e kaydediliyor...")
|
| 120 |
cache[current_date] = menu_data
|
| 121 |
save_cache(cache)
|
| 122 |
+
print("Cache güncellendi")
|
| 123 |
except Exception as e:
|
| 124 |
+
error_detail = f"Cache kaydetme hatası: {str(e)}\nTrace: {traceback.format_exc()}"
|
| 125 |
+
print(error_detail)
|
| 126 |
# Cache hatası olsa bile menüyü döndür
|
| 127 |
pass
|
| 128 |
|
| 129 |
return menu_data
|
| 130 |
except Exception as e:
|
| 131 |
+
error_detail = f"Genel hata: {str(e)}\nTrace: {traceback.format_exc()}"
|
| 132 |
+
print(error_detail)
|
| 133 |
+
raise HTTPException(status_code=500, detail=error_detail)
|
| 134 |
|
| 135 |
|
| 136 |
@app.post("/reset")
|
|
|
|
| 139 |
try:
|
| 140 |
if os.path.exists(CACHE_FILE):
|
| 141 |
os.remove(CACHE_FILE)
|
| 142 |
+
print("Cache dosyası silindi")
|
| 143 |
return {"message": "Cache temizlendi"}
|
| 144 |
except Exception as e:
|
| 145 |
+
error_detail = f"Cache temizleme hatası: {str(e)}\nTrace: {traceback.format_exc()}"
|
| 146 |
+
print(error_detail)
|
| 147 |
+
raise HTTPException(status_code=500, detail=error_detail)
|
| 148 |
|
| 149 |
|
| 150 |
@app.get("/health")
|