Spaces:
Sleeping
Sleeping
Commit
·
a9293d4
1
Parent(s):
5b70291
debug
Browse files- app/main.py +42 -32
app/main.py
CHANGED
|
@@ -4,7 +4,6 @@ from fastapi.middleware.cors import CORSMiddleware
|
|
| 4 |
import json
|
| 5 |
from datetime import datetime
|
| 6 |
import os
|
| 7 |
-
import requests
|
| 8 |
from typing import Dict
|
| 9 |
|
| 10 |
app = FastAPI()
|
|
@@ -21,6 +20,25 @@ app.add_middleware(
|
|
| 21 |
# Cache dosya yolu
|
| 22 |
CACHE_FILE = "menu_cache.json"
|
| 23 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
def get_current_date() -> str:
|
| 26 |
"""Günün tarihini YYYY-MM-DD formatında döndürür"""
|
|
@@ -33,47 +51,30 @@ def load_cache() -> Dict:
|
|
| 33 |
if os.path.exists(CACHE_FILE):
|
| 34 |
with open(CACHE_FILE, "r", encoding="utf-8") as f:
|
| 35 |
return json.load(f)
|
| 36 |
-
except Exception:
|
| 37 |
-
|
|
|
|
| 38 |
return {}
|
| 39 |
|
| 40 |
|
| 41 |
def save_cache(cache_data: Dict) -> None:
|
| 42 |
"""Cache'i dosyaya kaydeder"""
|
| 43 |
-
|
| 44 |
-
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
|
| 47 |
async def generate_menu() -> Dict:
|
| 48 |
-
"""
|
| 49 |
-
API_URL = "https://free.churchless.tech/v1/chat/completions"
|
| 50 |
-
headers = {
|
| 51 |
-
"Content-Type": "application/json"
|
| 52 |
-
}
|
| 53 |
-
|
| 54 |
-
data = {
|
| 55 |
-
"model": "gpt-3.5-turbo",
|
| 56 |
-
"messages": [{
|
| 57 |
-
"role": "user",
|
| 58 |
-
"content": """Türk mutfağına uygun günlük bir menü oluştur. Şu şekilde olmalı:
|
| 59 |
-
- Kahvaltı: Tipik bir Türk kahvaltısı
|
| 60 |
-
- Öğle: Ana yemek ve yanında
|
| 61 |
-
- Akşam: Ana yemek ve yanında
|
| 62 |
-
|
| 63 |
-
Lütfen mevsime uygun ve sağlıklı seçimler yap. Sadece menüyü liste olarak ver."""
|
| 64 |
-
}]
|
| 65 |
-
}
|
| 66 |
-
|
| 67 |
try:
|
| 68 |
-
response = requests.post(API_URL, json=data, headers=headers)
|
| 69 |
-
response.raise_for_status() # HTTP hatalarını yakala
|
| 70 |
-
|
| 71 |
-
menu_text = response.json()['choices'][0]['message']['content']
|
| 72 |
return {
|
| 73 |
"date": get_current_date(),
|
| 74 |
-
"menu":
|
| 75 |
}
|
| 76 |
except Exception as e:
|
|
|
|
| 77 |
raise HTTPException(status_code=500, detail=f"Menü oluşturulamadı: {str(e)}")
|
| 78 |
|
| 79 |
|
|
@@ -90,12 +91,20 @@ async def get_menu():
|
|
| 90 |
|
| 91 |
# Yeni menü oluştur
|
| 92 |
menu_data = await generate_menu()
|
| 93 |
-
|
| 94 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 95 |
|
| 96 |
return menu_data
|
| 97 |
except Exception as e:
|
| 98 |
-
|
|
|
|
| 99 |
|
| 100 |
|
| 101 |
@app.post("/reset")
|
|
@@ -106,6 +115,7 @@ async def reset_cache():
|
|
| 106 |
os.remove(CACHE_FILE)
|
| 107 |
return {"message": "Cache temizlendi"}
|
| 108 |
except Exception as e:
|
|
|
|
| 109 |
raise HTTPException(status_code=500, detail=f"Cache temizlenemedi: {str(e)}")
|
| 110 |
|
| 111 |
|
|
|
|
| 4 |
import json
|
| 5 |
from datetime import datetime
|
| 6 |
import os
|
|
|
|
| 7 |
from typing import Dict
|
| 8 |
|
| 9 |
app = FastAPI()
|
|
|
|
| 20 |
# Cache dosya yolu
|
| 21 |
CACHE_FILE = "menu_cache.json"
|
| 22 |
|
| 23 |
+
# Sabit örnek menü - API çalışana kadar bunu kullanacağız
|
| 24 |
+
SAMPLE_MENU = """Kahvaltı:
|
| 25 |
+
- Karışık kahvaltı tabağı (zeytin, peynir, domates, salatalık, bal, tereyağı)
|
| 26 |
+
- Haşlanmış yumurta
|
| 27 |
+
- Simit
|
| 28 |
+
- Çay
|
| 29 |
+
|
| 30 |
+
Öğle:
|
| 31 |
+
- Mercimek çorbası
|
| 32 |
+
- Izgara köfte
|
| 33 |
+
- Pilav
|
| 34 |
+
- Mevsim salata
|
| 35 |
+
|
| 36 |
+
Akşam:
|
| 37 |
+
- Sebzeli tavuk sote
|
| 38 |
+
- Bulgur pilavı
|
| 39 |
+
- Cacık
|
| 40 |
+
- Revani tatlısı"""
|
| 41 |
+
|
| 42 |
|
| 43 |
def get_current_date() -> str:
|
| 44 |
"""Günün tarihini YYYY-MM-DD formatında döndürür"""
|
|
|
|
| 51 |
if os.path.exists(CACHE_FILE):
|
| 52 |
with open(CACHE_FILE, "r", encoding="utf-8") as f:
|
| 53 |
return json.load(f)
|
| 54 |
+
except Exception as e:
|
| 55 |
+
print(f"Cache okuma hatası: {str(e)}")
|
| 56 |
+
return {}
|
| 57 |
return {}
|
| 58 |
|
| 59 |
|
| 60 |
def save_cache(cache_data: Dict) -> None:
|
| 61 |
"""Cache'i dosyaya kaydeder"""
|
| 62 |
+
try:
|
| 63 |
+
with open(CACHE_FILE, "w", encoding="utf-8") as f:
|
| 64 |
+
json.dump(cache_data, f, ensure_ascii=False, indent=2)
|
| 65 |
+
except Exception as e:
|
| 66 |
+
print(f"Cache yazma hatası: {str(e)}")
|
| 67 |
|
| 68 |
|
| 69 |
async def generate_menu() -> Dict:
|
| 70 |
+
"""Menü oluşturur - şimdilik sabit menü döndürüyoruz"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
return {
|
| 73 |
"date": get_current_date(),
|
| 74 |
+
"menu": SAMPLE_MENU
|
| 75 |
}
|
| 76 |
except Exception as e:
|
| 77 |
+
print(f"Menü oluşturma hatası: {str(e)}")
|
| 78 |
raise HTTPException(status_code=500, detail=f"Menü oluşturulamadı: {str(e)}")
|
| 79 |
|
| 80 |
|
|
|
|
| 91 |
|
| 92 |
# Yeni menü oluştur
|
| 93 |
menu_data = await generate_menu()
|
| 94 |
+
|
| 95 |
+
# Cache'e kaydet
|
| 96 |
+
try:
|
| 97 |
+
cache[current_date] = menu_data
|
| 98 |
+
save_cache(cache)
|
| 99 |
+
except Exception as e:
|
| 100 |
+
print(f"Cache kaydetme hatası: {str(e)}")
|
| 101 |
+
# Cache hatası olsa bile menüyü döndür
|
| 102 |
+
pass
|
| 103 |
|
| 104 |
return menu_data
|
| 105 |
except Exception as e:
|
| 106 |
+
print(f"Genel hata: {str(e)}")
|
| 107 |
+
raise HTTPException(status_code=500, detail=f"Beklenmeyen bir hata oluştu: {str(e)}")
|
| 108 |
|
| 109 |
|
| 110 |
@app.post("/reset")
|
|
|
|
| 115 |
os.remove(CACHE_FILE)
|
| 116 |
return {"message": "Cache temizlendi"}
|
| 117 |
except Exception as e:
|
| 118 |
+
print(f"Cache temizleme hatası: {str(e)}")
|
| 119 |
raise HTTPException(status_code=500, detail=f"Cache temizlenemedi: {str(e)}")
|
| 120 |
|
| 121 |
|