fudii0921 commited on
Commit
0b861df
·
verified ·
1 Parent(s): f0f70c6

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +400 -0
app.py ADDED
@@ -0,0 +1,400 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import numpy as np
3
+
4
+ setsumei = '''
5
+ <!DOCTYPE html>
6
+ <html lang="ja">
7
+ <head>
8
+ <meta charset="UTF-8">
9
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
10
+ <title>需要と供給の均衡点</title>
11
+ <style>
12
+ body {
13
+ font-family: Arial, sans-serif;
14
+ line-height: 1.6;
15
+ margin: 20px;
16
+ }
17
+ h1, h2 {
18
+ color: #2c3e50;
19
+ }
20
+ p {
21
+ margin-bottom: 10px;
22
+ }
23
+ ul {
24
+ margin-bottom: 20px;
25
+ }
26
+ .highlight {
27
+ background-color: #f9f9f9;
28
+ border-left: 5px solid #3498db;
29
+ padding: 10px;
30
+ margin-bottom: 20px;
31
+ }
32
+ </style>
33
+ </head>
34
+ <body>
35
+ <h1>需要と供給の均衡点</h1>
36
+ <p>最も基本的な方法は、需要曲線と供給曲線の交点を見つけることです。この交点が市場の均衡価格を示します。</p>
37
+ <div class="highlight">
38
+ <p>需要曲線 = Qd = a - bP</p>
39
+ <p>供給曲線 = Qs = c + dP</p>
40
+ </div>
41
+ <p>ここで、P は価格、a, b, c, d は定数です。均衡点では Qd = Qs となるため、次の方程式を解きます:</p>
42
+ <div class="highlight">
43
+ <p>a - bP = c + dP</p>
44
+ <p>P = (a - c) / (b + d)</p>
45
+ </div>
46
+ <p>この P が均衡価格です。</p>
47
+
48
+ <h2>利潤最大化</h2>
49
+ <p>企業が利潤を最大化するための価格設定も重要です。利潤 π は次のように表されます:</p>
50
+ <div class="highlight">
51
+ <p>π = TR − TC</p>
52
+ <p>TC = FC + VC</p>
53
+ </div>
54
+ <p>ここで、TR は総収入、TC は総費用です。総収入 TR は価格 P と販売量 Q の積で表されます:</p>
55
+ <div class="highlight">
56
+ <p>TR = P ⋅ Q</p>
57
+ </div>
58
+ <p>総費用 TC は固定費用 FC と変動費用 VC の和です:</p>
59
+ <div class="highlight">
60
+ <p>TC = FC + VC</p>
61
+ </div>
62
+ <p>利潤を最大化するためには、次の条件を満たす価格 P を見つけます:</p>
63
+ <div class="highlight">
64
+ <p>dπ / dP = 0</p>
65
+ </div>
66
+
67
+ <h2>価格弾力性を考慮した価格設定</h2>
68
+ <p>価格弾力性を考慮することで、価格変更が需要に与える影響を評価できます。価格弾力性 ϵ は次のように定義されます:</p>
69
+ <div class="highlight">
70
+ <p>ϵ = (ΔQ / Q) / (ΔP / P)</p>
71
+ </div>
72
+ <p>ΔQ は需要の変化量、ΔP は価格の変化量です。価格弾力性が高い場合、価格を下げることで需要が大幅に増加し、総収入が増える可能性があります。</p>
73
+ <p>これらの方法を組み合わせることで、より精度の高い最適価格を導出することができます。具体的な状況に応じて適切な手法を選択することが重要です。</p>
74
+
75
+ <h2>価格弾力性が高い場合</h2>
76
+ <ul>
77
+ <li><strong>メリット:</strong>
78
+ <ul>
79
+ <li>価格変更の影響: 価格を下げると需要が大幅に増加するため、売上を増やすことができます。</li>
80
+ <li>競争力: 価格を柔軟に調整することで、競合他社に対して優位に立つことができます。</li>
81
+ </ul>
82
+ </li>
83
+ <li><strong>デメリット:</strong>
84
+ <ul>
85
+ <li>収益の不安定性: 価格を上げると需要が大幅に減少するため、収益が不安定になる可能性があります。</li>
86
+ <li>価格競争: 価格弾力性が高い市場では、価格競争が激化し、利益率が低下することがあります。</li>
87
+ </ul>
88
+ </li>
89
+ </ul>
90
+
91
+ <h2>価格弾力性が低い場合</h2>
92
+ <ul>
93
+ <li><strong>メリット:</strong>
94
+ <ul>
95
+ <li>収益の安定性: 価格変更が需要に与える影響が小さいため、収益が安定しやすいです。</li>
96
+ <li>価格設定の自由度: 価格を上げても需要が大きく減少しないため、利益率を維持しやすいです。</li>
97
+ </ul>
98
+ </li>
99
+ <li><strong>デメリット:</strong>
100
+ <ul>
101
+ <li>需要の増加: 価格を下げても需要が大幅に増加しないため、売上を増やすのが難しいです。</li>
102
+ <li>競争力: 価格を柔軟に調整することで競争力を高めるのが難しいです。</li>
103
+ </ul>
104
+ </li>
105
+ </ul>
106
+
107
+ <p>企業の戦略や市場の特性に応じて、価格弾力性の高低がどちらが良いかを判断することが重要です。具体的な状況に応じて、適切な価格戦略を選択することが求められます。</p>
108
+ </body>
109
+ </html>
110
+ '''
111
+
112
+ js = """
113
+ function createGradioAnimation() {
114
+ var container = document.createElement('div');
115
+ container.id = 'gradio-animation';
116
+ container.style.fontSize = '2em';
117
+ container.style.fontWeight = 'bold';
118
+ container.style.textAlign = 'center';
119
+ container.style.marginBottom = '20px';
120
+
121
+ var text = 'ダイナミック・プライシング';
122
+ for (var i = 0; i < text.length; i++) {
123
+ (function(i){
124
+ setTimeout(function(){
125
+ var letter = document.createElement('span');
126
+ var randomColor = "#" + Math.floor(Math.random() * 16777215).toString(16);
127
+ letter.style.color = randomColor;
128
+ letter.style.opacity = '0';
129
+ letter.style.transition = 'opacity 0.5s';
130
+ letter.innerText = text[i];
131
+
132
+ container.appendChild(letter);
133
+
134
+ setTimeout(function() {
135
+ letter.style.opacity = '1';
136
+ }, 50);
137
+ }, i * 250);
138
+ })(i);
139
+ }
140
+
141
+ var gradioContainer = document.querySelector('.gradio-container');
142
+ gradioContainer.insertBefore(container, gradioContainer.firstChild);
143
+
144
+ return 'Animation created';
145
+ }
146
+ """
147
+
148
+
149
+ def dyn_chart():
150
+ # 需要曲線と供給曲線のパラメータ
151
+ a = 100 # 需要曲線の切片
152
+ b = 2 # 需要曲線の傾き
153
+ c = 20 # 供給曲線の切片
154
+ d = 1 # 供給曲線の傾き
155
+
156
+ # 価格の範囲
157
+ P = np.linspace(0, 100, 100)
158
+
159
+ # 需要曲線と供給曲線の計算
160
+ Q_d = a - b * P
161
+ Q_s = c + d * P
162
+
163
+ # 均衡点の計算
164
+ equilibrium_price = (a - c) / (b + d)
165
+ equilibrium_quantity = a - b * equilibrium_price
166
+
167
+ print(f"均衡価格: {equilibrium_price}")
168
+ print(f"均衡数量: {equilibrium_quantity}")
169
+
170
+ # グラフのプロット
171
+ pplot = f'''
172
+ import numpy as np<br />
173
+ import matplotlib.pyplot as plt<br />
174
+ import japanize_matplotlib<br />
175
+ <br />
176
+ equilibrium_price = {equilibrium_price}<br />
177
+ equilibrium_quantity = {equilibrium_quantity}<br />
178
+ plt.figure(figsize=(10, 6))<br />
179
+ plt.plot(P, Q_d, label='需要曲線')<br />
180
+ plt.plot(P, Q_s, label='供給曲線')<br />
181
+ plt.axvline(x=equilibrium_price, color='r', linestyle='--', label='均衡価格')<br />
182
+ plt.axhline(y=equilibrium_quantity, color='r', linestyle='--', label='均衡数量')<br />
183
+ plt.xlabel('価格')<br />
184
+ plt.ylabel('数量')<br />
185
+ plt.title('需要と供給の均衡点')<br />
186
+ plt.legend()<br />
187
+ plt.grid(True)<br />
188
+
189
+ plt.savefig('plot.png')<br />
190
+ plt.show()'''
191
+
192
+ return pplot
193
+
194
+
195
+ def load_css():
196
+ with open('theme.css', 'r') as file:
197
+ css_content = file.read()
198
+ return css_content
199
+
200
+
201
+ # 価格弾力性を計算する関数
202
+ def calculate_price_elasticity(prices, quantities):
203
+ # 価格と需要の変化率を計算
204
+ delta_p = np.diff(prices) / prices[:-1]
205
+ delta_q = np.diff(quantities) / quantities[:-1]
206
+
207
+ # 価格弾力性を計算
208
+ elasticity = delta_q / delta_p
209
+ return elasticity
210
+
211
+ # Gradioインターフェース用の関数
212
+ def gradio_calculate_price_elasticity(prices_str, quantities_str):
213
+ # 文字列を数値の配列に変換
214
+ prices = np.array([float(p) for p in prices_str.split(',')])
215
+ quantities = np.array([float(q) for q in quantities_str.split(',')])
216
+
217
+ # 価格弾力性を計算
218
+ price_elasticity = calculate_price_elasticity(prices, quantities)
219
+
220
+ # 結果を表示
221
+ results = []
222
+ for i, e in enumerate(price_elasticity):
223
+ results.append(f"価格 {prices[i]} から {prices[i+1]} への変化に対する価格弾力性: {e}")
224
+
225
+ # 平均価格弾力性を計算
226
+ average_elasticity = np.mean(price_elasticity)
227
+ results.append(f"平均価格弾力性: {average_elasticity}")
228
+
229
+ return "\n".join(results)
230
+
231
+
232
+ # 利潤を計算する関数
233
+ def calculate_profit(price, quantity, fixed_cost, variable_cost_per_unit):
234
+ # 総収入 (TR) を計算
235
+ total_revenue = price * quantity
236
+ print("Total revenue: ",total_revenue)
237
+
238
+ # 総費用 (TC) を計算
239
+ total_cost = (fixed_cost * quantity) + (variable_cost_per_unit * quantity)
240
+ print("Total cost: ",total_cost)
241
+ # 利潤 (π) を計算
242
+ profit = total_revenue - total_cost
243
+ print("Profit: ",profit)
244
+
245
+ return profit
246
+
247
+ # Gradioインターフェース用の関数
248
+ def gradio_calculate_profit(price, quantity, fixed_cost, variable_cost_per_unit):
249
+ profit = calculate_profit(int(price), int(quantity), int(fixed_cost), int(variable_cost_per_unit))
250
+ return f"利潤 (π): {profit}"
251
+
252
+
253
+
254
+ # 価格弾力性を用いて適正な価格を計算する関数
255
+ def calculate_optimal_price(current_price, current_quantity, price_elasticity, target_quantity):
256
+ # 価格弾力性の公式を利用して新しい価格を計算
257
+ optimal_price = current_price * (1 + (target_quantity - current_quantity) / (price_elasticity * current_quantity))
258
+ return optimal_price
259
+
260
+ # Gradioインターフェース用の関数
261
+ def gradio_calculate_optimal_price(current_price, current_quantity, price_elasticity, target_quantity):
262
+ optimal_price = calculate_optimal_price(current_price, current_quantity, price_elasticity, target_quantity)
263
+ return f"適正な価格: {optimal_price}"
264
+
265
+
266
+
267
+ # Custom CSS for the Groq badge and color scheme (feel free to edit however you wish)
268
+ custom_css = """
269
+ .gradio-container {
270
+ background-color: #f5f5f5;
271
+ }
272
+ .gr-button-primary {
273
+ background-color: #f55036 !important;
274
+ border-color: #f55036 !important;
275
+ }
276
+ .gr-button-secondary {
277
+ color: #f55036 !important;
278
+ border-color: #f55036 !important;
279
+ }
280
+ #groq-badge {
281
+ position: fixed;
282
+ bottom: 20px;
283
+ right: 20px;
284
+ z-index: 1000;
285
+ }
286
+ """
287
+
288
+ #with gr.Blocks(theme=gr.themes.Default()) as llm:
289
+ with gr.Blocks(css=load_css(),js=js) as llm:
290
+ with gr.Tab("価格弾力性"):
291
+ gr.Markdown("# 🗞️ 価格弾力性")
292
+
293
+ with gr.Row():
294
+ price_input = gr.Textbox(label="価格 (カンマ区切り)",value="100000, 120000, 140000, 160000, 180000")
295
+ demand_input = gr.Textbox(label="需要量 (カンマ区切り)",value="1000, 900, 800, 700, 600")
296
+
297
+ with gr.Row():
298
+ response_output = gr.Textbox(label="AIアシスタントの応答")
299
+
300
+ submit_button = gr.Button("価格弾力性プロセス", variant="primary")
301
+
302
+ # Add the Groq badge
303
+ gr.HTML("""
304
+ <div id="groq-badge">
305
+ <div style="color: #f55036; font-weight: bold;">POWERED BY EPRAG</div>
306
+ </div>
307
+ """)
308
+
309
+ submit_button.click(
310
+ gradio_calculate_price_elasticity,
311
+ inputs=[price_input, demand_input],
312
+ outputs=[response_output]
313
+ )
314
+
315
+ with gr.Tab("利潤"):
316
+ gr.Markdown("# 🗞️ 利潤")
317
+ with gr.Row():
318
+ price_input = gr.Number(label="価格",value="150000")
319
+ quantity_input = gr.Number(label="販売量",value="150")
320
+ fixed_input = gr.Number(label="固定費用",value="80000")
321
+ var_unit = gr.Number(label="単位あたりの変動費用",value="1000")
322
+
323
+ with gr.Row():
324
+ dynamic_output = gr.Textbox(label="AIアシスタントの応答")
325
+
326
+ submit_button = gr.Button("利潤プロセス", variant="primary")
327
+ submit_button.click(
328
+ gradio_calculate_profit,
329
+ inputs=[price_input,quantity_input,fixed_input,var_unit],
330
+ outputs=[dynamic_output]
331
+ )
332
+
333
+ # Add the Groq badge
334
+ gr.HTML("""
335
+ <div id="groq-badge">
336
+ <div style="color: #f55036; font-weight: bold;">POWERED BY EPRAG</div>
337
+ </div>
338
+ """)
339
+
340
+ with gr.Tab("適正価格"):
341
+
342
+ gr.Markdown("# 🗞️ 適正価格")
343
+
344
+ with gr.Row():
345
+ current_price_input = gr.Number(label="現在の価格",value=150000)
346
+ current_demand_input = gr.Number(label="現在の需要量",value=1000)
347
+ elasticity_input = gr.Number(label="価格弾力性",value=-1.5)
348
+ target_demand_input = gr.Number(label="目標とする需要量",value=1200)
349
+
350
+ with gr.Row():
351
+ price_output = gr.Textbox(label="AIアシスタントの応答")
352
+
353
+ submit_button = gr.Button("適正価格プロセス", variant="primary")
354
+ submit_button.click(
355
+ gradio_calculate_optimal_price,
356
+ inputs=[current_price_input,current_demand_input,elasticity_input,target_demand_input],
357
+ outputs=[price_output]
358
+ )
359
+
360
+ # Add the Groq badge
361
+ gr.HTML("""
362
+ <div id="groq-badge">
363
+ <div style="color: #f55036; font-weight: bold;">POWERED BY EPRAG</div>
364
+ </div>
365
+ """)
366
+
367
+ with gr.Tab("ダイナミック・プライシング"):
368
+ gr.Markdown("# 🗞️ ダイナミック・プライシング")
369
+
370
+ with gr.Row():
371
+ #logout_output = gr.Textbox(label="AIアシスタントの応答")
372
+ logout_output = gr.HTML(label="AIアシスタントの応答")
373
+
374
+ submit_button = gr.Button("ダイナミック・プライシング", variant="primary")
375
+ submit_button.click(
376
+ dyn_chart,
377
+ inputs=[],
378
+ outputs=[logout_output]
379
+ )
380
+
381
+ gr.Markdown("""
382
+ ## 使い方:
383
+ 上記のPythonコードをコピーして、ファイル保存して実行してください。
384
+ """)
385
+
386
+ # Add the Groq badge
387
+ gr.HTML("""
388
+ <div id="groq-badge">
389
+ <div style="color: #f55036; font-weight: bold;">POWERED BY EPRAG</div>
390
+ </div>
391
+ """)
392
+
393
+ with gr.Tab("解説"):
394
+ gr.Markdown("# 📄 解説")
395
+
396
+ with gr.Row():
397
+ #logout_output = gr.Textbox(label="AIアシスタントの応答")
398
+ gr.HTML(label="解説",value=setsumei)
399
+
400
+ llm.launch(share=True,pwa=True)