CihanYakar commited on
Commit
5b70291
·
1 Parent(s): 37ef80b

switch to freegpt

Browse files
Files changed (2) hide show
  1. .idea/misc.xml +3 -0
  2. app/main.py +58 -35
.idea/misc.xml CHANGED
@@ -1,4 +1,7 @@
1
  <?xml version="1.0" encoding="UTF-8"?>
2
  <project version="4">
 
 
 
3
  <component name="ProjectRootManager" version="2" project-jdk-name="Python 3.9" project-jdk-type="Python SDK" />
4
  </project>
 
1
  <?xml version="1.0" encoding="UTF-8"?>
2
  <project version="4">
3
+ <component name="Black">
4
+ <option name="sdkName" value="Python 3.9" />
5
+ </component>
6
  <component name="ProjectRootManager" version="2" project-jdk-name="Python 3.9" project-jdk-type="Python SDK" />
7
  </project>
app/main.py CHANGED
@@ -21,10 +21,12 @@ app.add_middleware(
21
  # Cache dosya yolu
22
  CACHE_FILE = "menu_cache.json"
23
 
 
24
  def get_current_date() -> str:
25
  """Günün tarihini YYYY-MM-DD formatında döndürür"""
26
  return datetime.now().strftime("%Y-%m-%d")
27
 
 
28
  def load_cache() -> Dict:
29
  """Cache dosyasını okur"""
30
  try:
@@ -35,56 +37,77 @@ def load_cache() -> Dict:
35
  pass
36
  return {}
37
 
 
38
  def save_cache(cache_data: Dict) -> None:
39
  """Cache'i dosyaya kaydeder"""
40
  with open(CACHE_FILE, "w", encoding="utf-8") as f:
41
  json.dump(cache_data, f, ensure_ascii=False, indent=2)
42
 
 
43
  async def generate_menu() -> Dict:
44
- """HuggingFace API kullanarak menü oluşturur"""
45
- API_URL = "https://api-inference.huggingface.co/models/HuggingFaceH4/zephyr-7b-beta"
46
-
47
- prompt = """
48
- Türk mutfağına uygun günlük bir menü oluştur. Şu şekilde olmalı:
49
- - Kahvaltı: Tipik bir Türk kahvaltısı
50
- - Öğle: Ana yemek ve yanında
51
- - Akşam: Ana yemek ve yanında
52
-
53
- Lütfen mevsime uygun ve sağlıklı seçimler yap. Sadece menüyü liste olarak ver.
54
- """
55
-
56
- response = requests.post(API_URL, json={"inputs": prompt})
57
- if response.status_code != 200:
58
- raise HTTPException(status_code=500, detail="Menü oluşturulamadı")
59
-
60
- return {
61
- "date": get_current_date(),
62
- "menu": response.json()[0]["generated_text"]
63
  }
64
 
 
 
 
 
 
 
 
 
 
 
 
 
 
65
  @app.get("/menu")
66
  async def get_menu():
67
  """Günün menüsünü getirir, yoksa oluşturur"""
68
- cache = load_cache()
69
- current_date = get_current_date()
70
-
71
- # Cache kontrolü
72
- if current_date in cache:
73
- return cache[current_date]
74
-
75
- # Yeni menü oluştur
76
- menu_data = await generate_menu()
77
- cache[current_date] = menu_data
78
- save_cache(cache)
79
-
80
- return menu_data
 
 
 
 
81
 
82
  @app.post("/reset")
83
  async def reset_cache():
84
  """Cache'i temizler"""
85
- if os.path.exists(CACHE_FILE):
86
- os.remove(CACHE_FILE)
87
- return {"message": "Cache temizlendi"}
 
 
 
 
88
 
89
  @app.get("/health")
90
  async def health_check():
 
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"""
27
  return datetime.now().strftime("%Y-%m-%d")
28
 
29
+
30
  def load_cache() -> Dict:
31
  """Cache dosyasını okur"""
32
  try:
 
37
  pass
38
  return {}
39
 
40
+
41
  def save_cache(cache_data: Dict) -> None:
42
  """Cache'i dosyaya kaydeder"""
43
  with open(CACHE_FILE, "w", encoding="utf-8") as f:
44
  json.dump(cache_data, f, ensure_ascii=False, indent=2)
45
 
46
+
47
  async def generate_menu() -> Dict:
48
+ """FreeGPT API kullanarak menü oluşturur"""
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": menu_text
75
+ }
76
+ except Exception as e:
77
+ raise HTTPException(status_code=500, detail=f"Menü oluşturulamadı: {str(e)}")
78
+
79
+
80
  @app.get("/menu")
81
  async def get_menu():
82
  """Günün menüsünü getirir, yoksa oluşturur"""
83
+ try:
84
+ cache = load_cache()
85
+ current_date = get_current_date()
86
+
87
+ # Cache kontrolü
88
+ if current_date in cache:
89
+ return cache[current_date]
90
+
91
+ # Yeni menü oluştur
92
+ menu_data = await generate_menu()
93
+ cache[current_date] = menu_data
94
+ save_cache(cache)
95
+
96
+ return menu_data
97
+ except Exception as e:
98
+ raise HTTPException(status_code=500, detail=f"Menü alınamadı: {str(e)}")
99
+
100
 
101
  @app.post("/reset")
102
  async def reset_cache():
103
  """Cache'i temizler"""
104
+ try:
105
+ if os.path.exists(CACHE_FILE):
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
 
112
  @app.get("/health")
113
  async def health_check():