aliceblue11 commited on
Commit
ff7bc08
·
verified ·
1 Parent(s): 3bdf777

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +85 -64
app.py CHANGED
@@ -61,13 +61,11 @@ class SajuCalculator:
61
 
62
  def parse_date(self, date_str):
63
  """날짜 파싱"""
64
- # 숫자만 추출
65
  numbers = re.findall(r'\d+', str(date_str))
66
 
67
  if not numbers:
68
  raise ValueError("날짜에서 숫자를 찾을 수 없습니다")
69
 
70
- # 하나의 긴 숫자인 경우
71
  if len(numbers) == 1:
72
  num_str = numbers[0]
73
  if len(num_str) == 8: # YYYYMMDD
@@ -81,8 +79,6 @@ class SajuCalculator:
81
  day = int(num_str[4:6])
82
  else:
83
  raise ValueError(f"날짜 형식을 인식할 수 없습니다: {num_str}")
84
-
85
- # 여러 숫자로 분리된 경우
86
  elif len(numbers) >= 3:
87
  year = int(numbers[0])
88
  month = int(numbers[1])
@@ -120,14 +116,11 @@ class SajuCalculator:
120
  hour = int(numbers[0])
121
  minute = int(numbers[1]) if len(numbers) > 1 else 0
122
 
123
- # 24시간 형식 조정
124
  hour = hour % 24
125
-
126
  return hour, minute
127
 
128
  def get_ganzhi(self, year, month, day, hour):
129
  """간지 계산"""
130
- # 기준일: 1900년 1월 1일
131
  base_date = datetime.datetime(1900, 1, 1)
132
  target_date = datetime.datetime(year, month, day)
133
 
@@ -215,54 +208,7 @@ def get_element_balance_advice(elements):
215
 
216
  return advice
217
 
218
- def calculate_saju(birth_date, birth_time, gender, birth_place):
219
- """사주 계산 메인 함수"""
220
- # SajuCalculator 인스턴스 생성
221
- calculator = SajuCalculator()
222
-
223
- try:
224
- # 입력 검증
225
- if not birth_date:
226
- return "❌ 생년월일을 입력해주세요."
227
-
228
- # 날짜 파싱
229
- year, month, day = calculator.parse_date(birth_date)
230
-
231
- # 시간 파싱
232
- hour, minute = calculator.parse_time(birth_time)
233
-
234
- # 날짜 유효성 검사
235
- if year < 1900 or year > 2100:
236
- return f"❌ 연도는 1900~2100 사이여야 합니다. 입력된 연도: {year}"
237
- if month < 1 or month > 12:
238
- return f"❌ 월은 1~12 사이여야 합니다. 입력된 월: {month}"
239
- if day < 1 or day > 31:
240
- return f"❌ 일은 1~31 사이여야 합니다. 입력된 일: {day}"
241
-
242
- # datetime 객체 생성
243
- birth_datetime = datetime.datetime(year, month, day, hour, minute)
244
-
245
- # 간지 계산
246
- ganzhi = calculator.get_ganzhi(year, month, day, hour)
247
-
248
- # 오행 분석
249
- elements = calculator.analyze_elements(ganzhi)
250
-
251
- # 십신 분석
252
- ten_gods = calculator.get_ten_gods_analysis(ganzhi)
253
-
254
- # 결과 포맷팅
255
- result = format_saju_result(ganzhi, elements, ten_gods, birth_datetime, gender, birth_place, calculator)
256
-
257
- return result
258
-
259
- except ValueError as ve:
260
- return f"❌ 입력 오류: {str(ve)}\n\n💡 입력 예시:\n- 생년월일: 19900207, 1990-2-7\n- 시간: 0815, 8:15"
261
-
262
- except Exception as e:
263
- return f"❌ 계산 중 오류가 발생했습니다: {str(e)}"
264
-
265
- def format_saju_result(ganzhi, elements, ten_gods, birth_datetime, gender, birth_place, calculator, original_time, corrected_time, location_offset):
266
  """사주 결과 포맷팅 - 표 형태로 출력"""
267
 
268
  # 오행별 색상 설정
@@ -287,21 +233,21 @@ def format_saju_result(ganzhi, elements, ten_gods, birth_datetime, gender, birth
287
 
288
  # 시간 보정 정보
289
  time_correction_info = ""
290
- if location_offset != 0:
291
- sign = "+" if location_offset > 0 else ""
292
  time_correction_info = f"""
293
  ### ⏰ 출생지 시간 보정
294
- - **입력 시간**: {original_time}
295
- - **보정 시간**: {corrected_time} ({birth_place} 기준 {sign}{location_offset}분 보정)
296
  """
297
 
298
  result = f"""
299
  # 🔮 사주명리 만세력 분석결과
300
 
301
  ## 📋 기본정보
302
- - **생년월일**: {birth_datetime.strftime('%Y년 %m월 %d일')}
303
- - **출생시간**: {corrected_time} ({birth_place})
304
- - **성별**: {gender}
305
  {time_correction_info}
306
  ## 🏛️ 사주(四柱) 만세력표
307
 
@@ -372,6 +318,83 @@ def format_saju_result(ganzhi, elements, ten_gods, birth_datetime, gender, birth
372
 
373
  return result
374
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
375
  def create_interface():
376
  """Gradio 인터페이스 생성"""
377
  with gr.Blocks(title="🔮 사주명리 만세력 시스템") as demo:
@@ -429,8 +452,6 @@ def create_interface():
429
  outputs=result_output
430
  )
431
 
432
- # 버튼 클릭 시에만 작동하도록 변경 (자동 로드 제거)
433
-
434
  gr.HTML("""
435
  <div style="text-align: center; padding: 20px; margin-top: 30px; border-top: 1px solid #eee;">
436
  <p><small>※ 본 시스템은 전통 사주명리학을 기반으로 하며, 참고용으로만 활용해주시기 바랍니다.</small></p>
 
61
 
62
  def parse_date(self, date_str):
63
  """날짜 파싱"""
 
64
  numbers = re.findall(r'\d+', str(date_str))
65
 
66
  if not numbers:
67
  raise ValueError("날짜에서 숫자를 찾을 수 없습니다")
68
 
 
69
  if len(numbers) == 1:
70
  num_str = numbers[0]
71
  if len(num_str) == 8: # YYYYMMDD
 
79
  day = int(num_str[4:6])
80
  else:
81
  raise ValueError(f"날짜 형식을 인식할 수 없습니다: {num_str}")
 
 
82
  elif len(numbers) >= 3:
83
  year = int(numbers[0])
84
  month = int(numbers[1])
 
116
  hour = int(numbers[0])
117
  minute = int(numbers[1]) if len(numbers) > 1 else 0
118
 
 
119
  hour = hour % 24
 
120
  return hour, minute
121
 
122
  def get_ganzhi(self, year, month, day, hour):
123
  """간지 계산"""
 
124
  base_date = datetime.datetime(1900, 1, 1)
125
  target_date = datetime.datetime(year, month, day)
126
 
 
208
 
209
  return advice
210
 
211
+ def format_saju_result(calculator, ganzhi, elements, ten_gods, birth_info):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
212
  """사주 결과 포맷팅 - 표 형태로 출력"""
213
 
214
  # 오행별 색상 설정
 
233
 
234
  # 시간 보정 정보
235
  time_correction_info = ""
236
+ if birth_info['location_offset'] != 0:
237
+ sign = "+" if birth_info['location_offset'] > 0 else ""
238
  time_correction_info = f"""
239
  ### ⏰ 출생지 시간 보정
240
+ - **입력 시간**: {birth_info['original_time']}
241
+ - **보정 시간**: {birth_info['corrected_time']} ({birth_info['birth_place']} 기준 {sign}{birth_info['location_offset']}분 보정)
242
  """
243
 
244
  result = f"""
245
  # 🔮 사주명리 만세력 분석결과
246
 
247
  ## 📋 기본정보
248
+ - **생년월일**: {birth_info['birth_datetime'].strftime('%Y년 %m월 %d일')}
249
+ - **출생시간**: {birth_info['corrected_time']} ({birth_info['birth_place']})
250
+ - **성별**: {birth_info['gender']}
251
  {time_correction_info}
252
  ## 🏛️ 사주(四柱) 만세력표
253
 
 
318
 
319
  return result
320
 
321
+ def calculate_saju(birth_date, birth_time, gender, birth_place):
322
+ """사주 계산 메인 함수"""
323
+ # SajuCalculator 인스턴스 생성
324
+ calculator = SajuCalculator()
325
+
326
+ try:
327
+ # 입력 검증
328
+ if not birth_date:
329
+ return "❌ 생년월일을 입력해주세요."
330
+
331
+ # 날짜 파싱
332
+ year, month, day = calculator.parse_date(birth_date)
333
+
334
+ # 시간 파싱
335
+ hour, minute = calculator.parse_time(birth_time)
336
+
337
+ # 출생지에 따른 시간 보정
338
+ location_offset = calculator.get_location_offset(birth_place)
339
+
340
+ # 보정된 시간 계산
341
+ corrected_minute = minute + location_offset
342
+ corrected_hour = hour
343
+
344
+ # 분이 60을 넘거나 0 미만인 경우 시간 조정
345
+ if corrected_minute >= 60:
346
+ corrected_hour += corrected_minute // 60
347
+ corrected_minute = corrected_minute % 60
348
+ elif corrected_minute < 0:
349
+ corrected_hour -= (-corrected_minute - 1) // 60 + 1
350
+ corrected_minute = 60 + (corrected_minute % 60)
351
+
352
+ # 시간이 24를 넘거나 0 미만인 경우 조정
353
+ corrected_hour = corrected_hour % 24
354
+
355
+ # 날짜 유효성 검사
356
+ if year < 1900 or year > 2100:
357
+ return f"❌ 연도는 1900~2100 사이여야 합니다. 입력된 연도: {year}"
358
+ if month < 1 or month > 12:
359
+ return f"❌ 월은 1~12 사이여야 합니다. 입력된 월: {month}"
360
+ if day < 1 or day > 31:
361
+ return f"❌ 일은 1~31 사이여야 합니다. 입력된 일: {day}"
362
+
363
+ # datetime 객체 생성 (보정된 시간 사용)
364
+ birth_datetime = datetime.datetime(year, month, day, corrected_hour, corrected_minute)
365
+ original_time = f"{hour:02d}:{minute:02d}"
366
+ corrected_time = f"{corrected_hour:02d}:{corrected_minute:02d}"
367
+
368
+ # 출생 정보 딕셔너리
369
+ birth_info = {
370
+ 'birth_datetime': birth_datetime,
371
+ 'gender': gender,
372
+ 'birth_place': birth_place,
373
+ 'original_time': original_time,
374
+ 'corrected_time': corrected_time,
375
+ 'location_offset': location_offset
376
+ }
377
+
378
+ # 간지 계산
379
+ ganzhi = calculator.get_ganzhi(year, month, day, corrected_hour)
380
+
381
+ # 오행 분석
382
+ elements = calculator.analyze_elements(ganzhi)
383
+
384
+ # 십신 분석
385
+ ten_gods = calculator.get_ten_gods_analysis(ganzhi)
386
+
387
+ # 결과 포맷팅
388
+ result = format_saju_result(calculator, ganzhi, elements, ten_gods, birth_info)
389
+
390
+ return result
391
+
392
+ except ValueError as ve:
393
+ return f"❌ 입력 오류: {str(ve)}\n\n💡 입력 예시:\n- 생년월일: 19851015, 1985-10-15\n- 시간: 1430, 14:30"
394
+
395
+ except Exception as e:
396
+ return f"❌ 계산 중 오류가 발생했습니다: {str(e)}"
397
+
398
  def create_interface():
399
  """Gradio 인터페이스 생성"""
400
  with gr.Blocks(title="🔮 사주명리 만세력 시스템") as demo:
 
452
  outputs=result_output
453
  )
454
 
 
 
455
  gr.HTML("""
456
  <div style="text-align: center; padding: 20px; margin-top: 30px; border-top: 1px solid #eee;">
457
  <p><small>※ 본 시스템은 전통 사주명리학을 기반으로 하며, 참고용으로만 활용해주시기 바랍니다.</small></p>