Spaces:
Running
Running
File size: 7,848 Bytes
6639e76 ab031a3 6639e76 b076596 6639e76 b076596 6639e76 b076596 6639e76 5faa1d3 b076596 5faa1d3 6639e76 b076596 6639e76 b076596 6639e76 b076596 6639e76 27294fd 6639e76 5faa1d3 b076596 6639e76 b076596 6639e76 ab031a3 b076596 ab031a3 6639e76 ab031a3 6639e76 ab031a3 6639e76 ab031a3 6639e76 ab031a3 6639e76 ab031a3 6639e76 ab031a3 6639e76 ab031a3 6639e76 ab031a3 6639e76 ab031a3 b076596 ab031a3 6639e76 ab031a3 6639e76 ab031a3 6639e76 ab031a3 6639e76 ab031a3 6639e76 ab031a3 6639e76 ab031a3 6639e76 ab031a3 6639e76 ab031a3 b076596 ab031a3 6639e76 ab031a3 6639e76 ab031a3 b076596 5faa1d3 ab031a3 6639e76 ab031a3 6639e76 ab031a3 6639e76 ab031a3 6639e76 ab031a3 b076596 ab031a3 6639e76 9be9232 ab031a3 6639e76 ab031a3 5faa1d3 ab031a3 6639e76 ab031a3 b076596 ab031a3 5faa1d3 6639e76 ab031a3 6639e76 5faa1d3 ab031a3 5faa1d3 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 | import os
import gradio as gr
import pandas as pd
import plotly.express as px
import re
import io
import subprocess
# Ensure the script is executable
os.system("chmod +x gpu_info_collector.sh")
# ==========================================
# 1. Define function to run the script
# ==========================================
def run_shell_script(secret_key):
# Security check: Verify the secret key to prevent unauthorized execution.
# Note: Set "RUN_KEY" in Space Settings -> Variables and secrets.
expected_key = os.environ.get("RUN_KEY")
if not expected_key:
return "❌ Auth failed: RUN_KEY environment variable is not configured on the server!"
if secret_key != expected_key:
return "❌ Auth failed: Incorrect secret key!"
print("Command received, starting script execution...")
# Execute the .sh file
try:
result = subprocess.run(
["./gpu_info_collector.sh"],
shell=True,
capture_output=True,
text=True
)
log_output = f"Standard Output:\n{result.stdout}\n\nError Output:\n{result.stderr}"
print(log_output)
return f"✅ Script execution completed!\n{log_output}"
except Exception as e:
return f"⚠️ Execution error: {str(e)}"
# ==========================================
# 2. Data Reading Engine
# ==========================================
def clean_and_read_file(file_path):
if not file_path or not os.path.exists(file_path):
return pd.DataFrame()
# --- Strategy A: Try reading as Excel ---
try:
df = pd.read_excel(file_path)
return df
except Exception:
pass
# --- Strategy B: Read as Text ---
raw_data = b""
try:
with open(file_path, 'rb') as f:
raw_data = f.read()
except Exception as e:
print(f"File read error: {e}")
return pd.DataFrame()
# Decode
content = ""
for enc in ['utf-8', 'gb18030', 'gbk']:
try:
content = raw_data.decode(enc)
break
except UnicodeDecodeError:
continue
if not content:
content = raw_data.decode('utf-8', errors='replace')
# --- Cleaning ---
content = re.sub(r"\\", "", content)
lines = content.splitlines()
cleaned_lines = []
buffer = ""
date_pattern = re.compile(r'^\s*202\d-\d{2}-\d{2}')
for line in lines:
line = line.strip()
if not line:
continue
is_header = "Date" in line and ("," in line)
is_date_row = date_pattern.match(line) is not None
if is_header or is_date_row:
if buffer:
cleaned_lines.append(buffer)
buffer = line
else:
buffer += " " + line
if buffer:
cleaned_lines.append(buffer)
csv_content = "\n".join(cleaned_lines)
try:
df = pd.read_csv(io.StringIO(csv_content))
except Exception:
try:
df = pd.read_csv(io.StringIO(csv_content),
sep=None,
engine='python')
except Exception:
return pd.DataFrame()
return df
# ==========================================
# 3. Data Processing
# ==========================================
def process_gpu_data(df):
if df.empty:
return df
df.columns = [str(c).strip() for c in df.columns]
if 'Date' in df.columns:
df['Date'] = pd.to_datetime(df['Date'], errors='coerce')
def clean_currency(x):
if isinstance(x, (int, float)):
return float(x)
if isinstance(x, str):
match = re.search(r'(\d+\.?\d*)', x)
return float(match.group(1)) if match else 0.0
return 0.0
target_col = None
if 'Cloud Rent (/hr)' in df.columns:
target_col = 'Cloud Rent (/hr)'
else:
for c in df.columns:
if 'Rent' in c or '/hr' in c:
target_col = c
break
if target_col:
df['Rent_Price_Num'] = df[target_col].apply(clean_currency)
return df
def process_llm_data(df):
if df.empty:
return df
df.columns = [str(c).strip() for c in df.columns]
if 'Date' in df.columns:
df['Date'] = pd.to_datetime(df['Date'], errors='coerce')
return df
# ==========================================
# 4. Plotting Logic
# ==========================================
def plot_gpu_trends(df):
if df is None or df.empty or 'Rent_Price_Num' not in df.columns:
return None
plot_df = df.dropna(subset=['Date', 'Rent_Price_Num'])
if plot_df.empty:
return None
# Defensive fix: Prevent Index out of bounds if df columns are insufficient
chip_col = 'Chip' if 'Chip' in df.columns else (df.columns[1] if len(df.columns) > 1 else None)
fig = px.line(plot_df,
x='Date',
y='Rent_Price_Num',
color=chip_col if chip_col in df.columns else None,
title='GPU Cloud Rental Price Trends ($/hr)',
labels={
'Rent_Price_Num': 'Price ($/hr)',
'Date': 'Date'
},
markers=True)
return fig
def plot_llm_trends(df):
if df is None or df.empty:
return None
value_vars = [c for c in df.columns if c != 'Date']
if not value_vars:
return None
plot_df = df[['Date'] + value_vars].copy().dropna(subset=['Date'])
df_long = plot_df.melt(id_vars=['Date'], var_name='Model', value_name='Price')
fig = px.line(
df_long,
x='Date',
y='Price',
color='Model',
title='LLM API Price Trends',
labels={'Price': 'Price', 'Date': 'Date', 'Model': 'Model Type'},
markers=True
)
return fig
# ==========================================
# 5. Gradio Interface
# ==========================================
DEFAULT_GPU_FILE = "gpu_price_history.csv"
DEFAULT_LLM_FILE = "llm_price_trends.csv"
def load_gpu_pipeline():
df = clean_and_read_file(DEFAULT_GPU_FILE)
df = process_gpu_data(df)
return df, plot_gpu_trends(df)
def load_llm_pipeline():
df = clean_and_read_file(DEFAULT_LLM_FILE)
df = process_llm_data(df)
return df, plot_llm_trends(df)
# --- UI Definition ---
with gr.Blocks(title="AI Price Tracker") as demo:
gr.Markdown("## 📊 AI Compute & Model Price Trends")
with gr.Tabs():
# GPU Tab
with gr.TabItem("GPU Prices"):
with gr.Row():
with gr.Column(scale=1):
gpu_plot = gr.Plot(label="Price Trend")
with gr.Row():
with gr.Accordion("Data Preview", open=False):
gpu_table = gr.DataFrame()
# LLM Tab
with gr.TabItem("LLM Prices"):
with gr.Row():
with gr.Column(scale=1):
llm_plot = gr.Plot(label="Price Trend")
with gr.Row():
with gr.Accordion("Data Preview", open=False):
llm_table = gr.DataFrame()
# Hidden components to expose the API safely without breaking UI
api_input = gr.Textbox(visible=False)
api_output = gr.Textbox(visible=False)
api_trigger = gr.Button(visible=False)
api_trigger.click(
fn=run_shell_script,
inputs=[api_input],
outputs=[api_output],
api_name="run_collector"
)
# --- Initialization Logic ---
def init_on_load():
g_df, g_fig = load_gpu_pipeline()
l_df, l_fig = load_llm_pipeline()
return g_fig, g_df, l_fig, l_df
demo.load(
init_on_load,
inputs=None,
outputs=[gpu_plot, gpu_table, llm_plot, llm_table]
)
if __name__ == "__main__":
demo.launch(share=True) |