Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files- app.py +129 -0
- requirements.txt +3 -0
app.py
ADDED
|
@@ -0,0 +1,129 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import re
|
| 3 |
+
import numpy as np
|
| 4 |
+
import matplotlib.pyplot as plt
|
| 5 |
+
|
| 6 |
+
# Store equation values
|
| 7 |
+
stored_equations = {}
|
| 8 |
+
|
| 9 |
+
# Function to extract numbers from text
|
| 10 |
+
def extract_numbers(phrase):
|
| 11 |
+
return [float(num) for num in re.findall(r'-?\d+\.?\d*', phrase)]
|
| 12 |
+
|
| 13 |
+
# Function to calculate equilibrium
|
| 14 |
+
def calculate_equilibrium(demand_intercept, slope_demand, supply_intercept, slope_supply):
|
| 15 |
+
Q_eq = (demand_intercept - supply_intercept) / (slope_supply - slope_demand)
|
| 16 |
+
P_eq = demand_intercept + slope_demand * Q_eq
|
| 17 |
+
return round(Q_eq, 2), round(P_eq, 2)
|
| 18 |
+
|
| 19 |
+
def plot_equilibrium(demand_intercept, slope_demand, supply_intercept, slope_supply):
|
| 20 |
+
Q_range = np.linspace(0, 20, 100)
|
| 21 |
+
demand_curve = demand_intercept + (slope_demand * Q_range)
|
| 22 |
+
supply_curve = supply_intercept + (slope_supply * Q_range)
|
| 23 |
+
|
| 24 |
+
plt.figure(figsize=(6, 4))
|
| 25 |
+
plt.plot(Q_range, demand_curve, label="Demand Curve", color="blue")
|
| 26 |
+
plt.plot(Q_range, supply_curve, label="Supply Curve", color="green")
|
| 27 |
+
|
| 28 |
+
# Calculate equilibrium
|
| 29 |
+
Q_eq, P_eq = calculate_equilibrium(demand_intercept, slope_demand, supply_intercept, slope_supply)
|
| 30 |
+
|
| 31 |
+
# Calculate CS & PS
|
| 32 |
+
CS_top = demand_intercept # Max price consumers would pay (demand intercept)
|
| 33 |
+
PS_bottom = supply_intercept # Min price suppliers accept (supply intercept)
|
| 34 |
+
|
| 35 |
+
CS = 0.5 * (CS_top - P_eq) * Q_eq # Triangle area for CS
|
| 36 |
+
PS = 0.5 * (P_eq - PS_bottom) * Q_eq # Triangle area for PS
|
| 37 |
+
|
| 38 |
+
# ✅ Print CS & PS values
|
| 39 |
+
print(f"Consumer Surplus (CS): {CS:.2f}")
|
| 40 |
+
print(f"Producer Surplus (PS): {PS:.2f}")
|
| 41 |
+
|
| 42 |
+
# Highlight CS (shaded area above equilibrium price)
|
| 43 |
+
plt.fill_between([0, Q_eq], [CS_top, P_eq], P_eq, color="blue", alpha=0.3, label="Consumer Surplus")
|
| 44 |
+
|
| 45 |
+
# Highlight PS (shaded area below equilibrium price)
|
| 46 |
+
plt.fill_between([0, Q_eq], P_eq, [PS_bottom, P_eq], color="green", alpha=0.3, label="Producer Surplus")
|
| 47 |
+
|
| 48 |
+
# Mark equilibrium point
|
| 49 |
+
plt.scatter(Q_eq, P_eq, color="red", label=f"Equilibrium ({Q_eq}, {P_eq})")
|
| 50 |
+
|
| 51 |
+
plt.xlabel("Quantity")
|
| 52 |
+
plt.ylabel("Price")
|
| 53 |
+
plt.legend()
|
| 54 |
+
plt.title("Supply & Demand Equilibrium with CS & PS")
|
| 55 |
+
plt.grid()
|
| 56 |
+
|
| 57 |
+
plot_filename = "equilibrium_plot.png"
|
| 58 |
+
plt.savefig(plot_filename)
|
| 59 |
+
plt.close()
|
| 60 |
+
return plot_filename, CS, PS
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
# Function to process user input
|
| 64 |
+
def chatbot(user_input):
|
| 65 |
+
global stored_equations
|
| 66 |
+
user_input = user_input.strip().lower()
|
| 67 |
+
|
| 68 |
+
if not user_input:
|
| 69 |
+
return "Please enter a valid question.", None
|
| 70 |
+
|
| 71 |
+
# Step 1: Detect if user wants to find equilibrium
|
| 72 |
+
if "equilibrium" in user_input:
|
| 73 |
+
return "Enter demand and supply equations in this format: Demand: P = a - bQ, Supply: P = c + dQ.", None
|
| 74 |
+
|
| 75 |
+
# Step 2: Detect if user entered equations
|
| 76 |
+
if "p =" in user_input:
|
| 77 |
+
numbers = extract_numbers(user_input)
|
| 78 |
+
if len(numbers) == 4:
|
| 79 |
+
stored_equations = {
|
| 80 |
+
"demand_intercept": numbers[0],
|
| 81 |
+
"demand_slope": -abs(numbers[1]), # Ensure demand slope is negative
|
| 82 |
+
"supply_intercept": numbers[2],
|
| 83 |
+
"supply_slope": abs(numbers[3]) # Ensure supply slope is positive
|
| 84 |
+
}
|
| 85 |
+
return (f"Stored values:\n"
|
| 86 |
+
f" Demand: P = {numbers[0]} - {abs(numbers[1])}Q\n"
|
| 87 |
+
f" Supply: P = {numbers[2]} + {numbers[3]}Q\n"
|
| 88 |
+
f"Type 'solve' to calculate equilibrium."), None
|
| 89 |
+
|
| 90 |
+
else:
|
| 91 |
+
return "Invalid format. Please enter equations correctly.", None
|
| 92 |
+
|
| 93 |
+
# Step 3: Compute equilibrium when user types "solve"
|
| 94 |
+
if "solve" in user_input:
|
| 95 |
+
if not stored_equations:
|
| 96 |
+
return "Error: Missing equations. Please enter demand and supply equations first.", None
|
| 97 |
+
|
| 98 |
+
Q_eq, P_eq = calculate_equilibrium(
|
| 99 |
+
stored_equations["demand_intercept"],
|
| 100 |
+
stored_equations["demand_slope"],
|
| 101 |
+
stored_equations["supply_intercept"],
|
| 102 |
+
stored_equations["supply_slope"]
|
| 103 |
+
)
|
| 104 |
+
plot_path, CS, PS = plot_equilibrium(
|
| 105 |
+
stored_equations["demand_intercept"],
|
| 106 |
+
stored_equations["demand_slope"],
|
| 107 |
+
stored_equations["supply_intercept"],
|
| 108 |
+
stored_equations["supply_slope"]
|
| 109 |
+
)
|
| 110 |
+
return f"Equilibrium found at Quantity = {Q_eq}, Price = {P_eq}, CS = {CS}, PS = {PS}.", plot_path
|
| 111 |
+
|
| 112 |
+
return "I can help with equilibrium calculations. Try typing 'I want to find equilibrium.'", None
|
| 113 |
+
|
| 114 |
+
# Launch Gradio chatbot
|
| 115 |
+
#interface = gr.Interface(fn=chatbot, inputs="text", outputs=["text", "image"])
|
| 116 |
+
|
| 117 |
+
# Launch Gradio chatbot
|
| 118 |
+
interface = gr.Interface(
|
| 119 |
+
fn=chatbot,
|
| 120 |
+
inputs="text",
|
| 121 |
+
outputs=[
|
| 122 |
+
gr.Textbox(lines=4, interactive=False), # Output box with flexible content, always 4 lines tall
|
| 123 |
+
gr.Image()
|
| 124 |
+
],
|
| 125 |
+
title="AGEC 3503 HW Bot",
|
| 126 |
+
description="Hi, Welcome to the AGEC 3503 HW Bot! Enter your question to solve a problem like Competitive Equilibrium."
|
| 127 |
+
)
|
| 128 |
+
|
| 129 |
+
interface.launch(share=True)
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
numpy
|
| 3 |
+
matplotlib
|