seawolf2357 commited on
Commit
d98fe87
β€’
1 Parent(s): 57d8237

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -21
app.py CHANGED
@@ -126,37 +126,31 @@ def get_lunar_info(solYear, solMonth, solDay, solHour):
126
  return {"Error": "API ν˜ΈμΆœμ— μ‹€νŒ¨ν–ˆμŠ΅λ‹ˆλ‹€."}, "정보 μ—†μŒ", "<p>API ν˜ΈμΆœμ— μ‹€νŒ¨ν–ˆμŠ΅λ‹ˆλ‹€.</p>"
127
 
128
 
129
-
130
  def analyze_character(lunIljin, OPENAI_API_KEY):
131
- API_URL = "https://api.openai.com/v1/chat/completions" # API URL이 μ •ν™•ν•œμ§€ 확인
132
  payload = {
133
- "model": "gpt-4-0125-preview", # 'gpt-4-0125-preview' λͺ¨λΈ 이름이 μ •ν™•ν•œμ§€, ν˜Ήμ€ μ‚¬μš© κ°€λŠ₯ν•œμ§€ 확인
134
- "prompt": f"일주 '{lunIljin}'에 λŒ€ν•œ 성격과 νŠΉμ§•μ„ μ„€λͺ…ν•΄μ£Όμ„Έμš”.", # prompt ꡬ성이 μ μ ˆν•œμ§€ 확인
135
- "temperature": 0.7,
136
- "max_tokens": 150,
137
- "top_p": 1.0,
138
- "frequency_penalty": 0.0,
139
- "presence_penalty": 0.0
 
140
  }
141
  headers = {
142
  "Content-Type": "application/json",
143
- "Authorization": f"Bearer {OPENAI_API_KEY}" # API ν‚€κ°€ μ •ν™•νžˆ μž…λ ₯λ˜μ—ˆλŠ”μ§€ 확인
144
  }
145
-
146
  response = requests.post(API_URL, headers=headers, json=payload)
147
-
148
  if response.status_code == 200:
149
  data = response.json()
150
- return data["choices"][0]["text"]
 
151
  else:
152
- # 였λ₯˜ λ©”μ‹œμ§€ λ°˜ν™˜ 방식 κ°œμ„ 
153
- try:
154
- error_data = response.json() # 였λ₯˜ 응닡이 JSON ν˜•νƒœμΈ 경우
155
- error_message = error_data.get("error", {}).get("message", "API ν˜ΈμΆœμ— μ‹€νŒ¨ν–ˆμŠ΅λ‹ˆλ‹€.")
156
- except ValueError:
157
- error_message = "API 호좜 μ‹€νŒ¨: 응닡을 JSON으둜 νŒŒμ‹±ν•  수 μ—†μŠ΅λ‹ˆλ‹€."
158
-
159
- return f"였λ₯˜κ°€ λ°œμƒν–ˆμŠ΅λ‹ˆλ‹€. μƒνƒœ μ½”λ“œ: {response.status_code}, λ©”μ‹œμ§€: {error_message}"
160
 
161
 
162
  # 사주 뢄석 데이터 μ˜ˆμ‹œ (κ°„μ†Œν™”λœ 버전)
 
126
  return {"Error": "API ν˜ΈμΆœμ— μ‹€νŒ¨ν–ˆμŠ΅λ‹ˆλ‹€."}, "정보 μ—†μŒ", "<p>API ν˜ΈμΆœμ— μ‹€νŒ¨ν–ˆμŠ΅λ‹ˆλ‹€.</p>"
127
 
128
 
 
129
  def analyze_character(lunIljin, OPENAI_API_KEY):
130
+ API_URL = "https://api.openai.com/v1/chat/completions"
131
  payload = {
132
+ "model": "gpt-4-0125-preview", # λͺ¨λΈ 이름 확인 ν•„μš”
133
+ "messages": [{
134
+ "role": "system",
135
+ "content": "당신은 사주 νŒ”μžμ— λŒ€ν•΄ μ„€λͺ…ν•˜λŠ” μ±—λ΄‡μž…λ‹ˆλ‹€."
136
+ }, {
137
+ "role": "user",
138
+ "content": f"일주 '{lunIljin}'에 λŒ€ν•œ 성격과 νŠΉμ§•μ„ μ„€λͺ…ν•΄μ£Όμ„Έμš”."
139
+ }],
140
  }
141
  headers = {
142
  "Content-Type": "application/json",
143
+ "Authorization": f"Bearer {OPENAI_API_KEY}"
144
  }
 
145
  response = requests.post(API_URL, headers=headers, json=payload)
 
146
  if response.status_code == 200:
147
  data = response.json()
148
+ # λŒ€ν™”ν˜• API μ‘λ‹΅μ—μ„œ μ μ ˆν•œ λ©”μ‹œμ§€ μΆ”μΆœ
149
+ return data["choices"][0]["message"]["content"]
150
  else:
151
+ # 였λ₯˜ λ©”μ‹œμ§€ κ°œμ„ 
152
+ return f"였λ₯˜κ°€ λ°œμƒν–ˆμŠ΅λ‹ˆλ‹€. μƒνƒœ μ½”λ“œ: {response.status_code}, λ©”μ‹œμ§€: {response.json().get('error', {}).get('message', 'Unknown error')}"
153
+
 
 
 
 
 
154
 
155
 
156
  # 사주 뢄석 데이터 μ˜ˆμ‹œ (κ°„μ†Œν™”λœ 버전)