CSB261 commited on
Commit
d348e0c
ยท
verified ยท
1 Parent(s): a3f9e77

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -11
app.py CHANGED
@@ -1,4 +1,5 @@
1
  import gradio as gr
 
2
 
3
  class MBTIAnalyzer:
4
  def __init__(self):
@@ -35,10 +36,13 @@ class MBTIAnalyzer:
35
  self.scores = {'E': 0, 'I': 0, 'S': 0, 'N': 0, 'T': 0, 'F': 0, 'J': 0, 'P': 0}
36
 
37
  def ask_question(self, category, answer):
38
- if answer == '1':
39
- self.scores[category[0]] += 1
40
- elif answer == '2':
41
- self.scores[category[1]] += 1
 
 
 
42
 
43
  def analyze(self, answers):
44
  idx = 0
@@ -67,25 +71,36 @@ class MBTIAnalyzer:
67
  result += 'T' if percentages['T'] >= 50 else 'F'
68
  result += 'J' if percentages['J'] >= 50 else 'P'
69
 
70
- output = f"MBTI ์œ ํ˜•: {result}\n\n์„ธ๋ถ€ ์ง€์ˆ˜:\n"
71
- for trait, percentage in percentages.items():
72
- output += f"{trait}: {percentage}%\n"
73
- return output
 
 
 
 
 
 
 
 
 
74
 
75
  def mbti_interface(*answers):
76
  analyzer = MBTIAnalyzer()
77
- return analyzer.analyze(answers)
 
 
78
 
79
  # Gradio ์ธํ„ฐํŽ˜์ด์Šค ์„ค์ •
80
  questions = []
81
  for key in ['EI', 'SN', 'TF', 'JP']:
82
  for q in MBTIAnalyzer().questions[key]:
83
- questions.append(gr.Radio(["1", "2"], label=q))
84
 
85
  iface = gr.Interface(
86
  fn=mbti_interface,
87
  inputs=questions,
88
- outputs="text",
89
  title="MBTI ๋ถ„์„๊ธฐ",
90
  description="20๊ฐœ ์ด์ƒ์˜ ์งˆ๋ฌธ์„ ํ†ตํ•ด ๋‹น์‹ ์˜ MBTI ์œ ํ˜•์„ ๋ถ„์„ํ•ฉ๋‹ˆ๋‹ค."
91
  )
 
1
  import gradio as gr
2
+ import matplotlib.pyplot as plt
3
 
4
  class MBTIAnalyzer:
5
  def __init__(self):
 
36
  self.scores = {'E': 0, 'I': 0, 'S': 0, 'N': 0, 'T': 0, 'F': 0, 'J': 0, 'P': 0}
37
 
38
  def ask_question(self, category, answer):
39
+ # ์ ์ˆ˜๋ฅผ 5๋‹จ๊ณ„๋กœ ๊ณ„์‚ฐํ•ฉ๋‹ˆ๋‹ค. ๋งค์šฐ ๊ทธ๋ ‡๋‹ค: 2, ๊ทธ๋ ‡๋‹ค: 1, ๋ณดํ†ต์ด๋‹ค: 0, ์•„๋‹ˆ๋‹ค: -1, ๋งค์šฐ ์•„๋‹ˆ๋‹ค: -2
40
+ scale = {"๋งค์šฐ ๊ทธ๋ ‡๋‹ค": 2, "๊ทธ๋ ‡๋‹ค": 1, "๋ณดํ†ต์ด๋‹ค": 0, "์•„๋‹ˆ๋‹ค": -1, "๋งค์šฐ ์•„๋‹ˆ๋‹ค": -2}
41
+ score = scale[answer]
42
+ if score > 0:
43
+ self.scores[category[0]] += score
44
+ elif score < 0:
45
+ self.scores[category[1]] -= score
46
 
47
  def analyze(self, answers):
48
  idx = 0
 
71
  result += 'T' if percentages['T'] >= 50 else 'F'
72
  result += 'J' if percentages['J'] >= 50 else 'P'
73
 
74
+ return result, percentages
75
+
76
+ def plot_result(self, percentages):
77
+ labels = list(percentages.keys())
78
+ sizes = list(percentages.values())
79
+
80
+ fig, ax = plt.subplots()
81
+ ax.barh(labels, sizes, color='skyblue')
82
+ ax.set_xlim(0, 100)
83
+ ax.set_xlabel('Percentage (%)')
84
+ ax.set_title('MBTI Result Distribution')
85
+
86
+ return fig
87
 
88
  def mbti_interface(*answers):
89
  analyzer = MBTIAnalyzer()
90
+ result, percentages = analyzer.analyze(answers)
91
+ fig = analyzer.plot_result(percentages)
92
+ return result, fig
93
 
94
  # Gradio ์ธํ„ฐํŽ˜์ด์Šค ์„ค์ •
95
  questions = []
96
  for key in ['EI', 'SN', 'TF', 'JP']:
97
  for q in MBTIAnalyzer().questions[key]:
98
+ questions.append(gr.Radio(["๋งค์šฐ ๊ทธ๋ ‡๋‹ค", "๊ทธ๋ ‡๋‹ค", "๋ณดํ†ต์ด๋‹ค", "์•„๋‹ˆ๋‹ค", "๋งค์šฐ ์•„๋‹ˆ๋‹ค"], label=q))
99
 
100
  iface = gr.Interface(
101
  fn=mbti_interface,
102
  inputs=questions,
103
+ outputs=[gr.Textbox(label="MBTI ์œ ํ˜• ๊ฒฐ๊ณผ"), gr.Plot(label="๊ฒฐ๊ณผ ์ฐจํŠธ")],
104
  title="MBTI ๋ถ„์„๊ธฐ",
105
  description="20๊ฐœ ์ด์ƒ์˜ ์งˆ๋ฌธ์„ ํ†ตํ•ด ๋‹น์‹ ์˜ MBTI ์œ ํ˜•์„ ๋ถ„์„ํ•ฉ๋‹ˆ๋‹ค."
106
  )