Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import pandas as pd
|
| 4 |
+
from huggingface_hub import InferenceClient
|
| 5 |
+
from threading import Timer
|
| 6 |
+
|
| 7 |
+
HUGGINGFACE_TOKEN = os.environ.get("HUGGINGFACE_TOKEN")
|
| 8 |
+
def get_available_free():
|
| 9 |
+
models = InferenceClient().list_deployed_models("text-generation-inference")['text-generation']
|
| 10 |
+
models_conclusion = {
|
| 11 |
+
"Model": [],
|
| 12 |
+
"API": [],
|
| 13 |
+
"Text Completion": [],
|
| 14 |
+
"Chat Completion": []
|
| 15 |
+
}
|
| 16 |
+
for m in models:
|
| 17 |
+
text_available = False
|
| 18 |
+
chat_available = False
|
| 19 |
+
pro_sub = False
|
| 20 |
+
try:
|
| 21 |
+
InferenceClient(m, timeout=10, token="HUGGINGFACE_TOKEN").text_generation("Hi.", max_new_tokens=1)
|
| 22 |
+
text_available = True
|
| 23 |
+
InferenceClient(m, timeout=10, token="HUGGINGFACE_TOKEN").chat_completion(messages=[{'role': 'user', 'content': 'Hi.'}], max_tokens=1)
|
| 24 |
+
chat_available = True
|
| 25 |
+
except Exception as e:
|
| 26 |
+
print(e)
|
| 27 |
+
if e and "Model requires a Pro subscription" in str(e):
|
| 28 |
+
pro_sub = True
|
| 29 |
+
if e and "Rate limit reached" in str(e):
|
| 30 |
+
print("Rate Limited!!")
|
| 31 |
+
if os.path.exists("data.csv"):
|
| 32 |
+
print("Loading data from file...")
|
| 33 |
+
return pd.read_csv("data.csv").to_dict(orient='list')
|
| 34 |
+
return []
|
| 35 |
+
models_conclusion["Model"].append(m)
|
| 36 |
+
models_conclusion["API"].append("Free" if chat_available or text_available else ("Pro Subscription" if pro_sub else "Not Responding"))
|
| 37 |
+
models_conclusion["Chat Completion"].append("---" if (pro_sub or (not chat_available and not text_available)) else ("β" if chat_available else "β"))
|
| 38 |
+
models_conclusion["Text Completion"].append("---" if (pro_sub or (not chat_available and not text_available)) else ("β" if text_available else "β"))
|
| 39 |
+
pd.DataFrame(models_conclusion).to_csv("data.csv", index=False)
|
| 40 |
+
return models_conclusion
|
| 41 |
+
|
| 42 |
+
def update_data():
|
| 43 |
+
data = get_available_free()
|
| 44 |
+
df = pd.DataFrame(data)
|
| 45 |
+
return df
|
| 46 |
+
|
| 47 |
+
def display_table(search_query=""):
|
| 48 |
+
df = update_data()
|
| 49 |
+
if search_query:
|
| 50 |
+
filtered_df = df[df["Model"].str.contains(search_query, case=False)]
|
| 51 |
+
else:
|
| 52 |
+
filtered_df = df
|
| 53 |
+
|
| 54 |
+
styled_df = filtered_df.style.apply(apply_row_styles, axis=1, subset=["Model", "API", "Text Completion", "Chat Completion"])
|
| 55 |
+
return styled_df
|
| 56 |
+
|
| 57 |
+
def apply_row_styles(row):
|
| 58 |
+
api_value = row["API"]
|
| 59 |
+
return [
|
| 60 |
+
color_status(api_value, row["Model"]),
|
| 61 |
+
color_status(api_value, row["API"]),
|
| 62 |
+
color_status(api_value, row["Text Completion"]),
|
| 63 |
+
color_status(api_value, row["Chat Completion"])
|
| 64 |
+
]
|
| 65 |
+
|
| 66 |
+
def color_status(api_value, cell_value):
|
| 67 |
+
if cell_value == "---":
|
| 68 |
+
if api_value == "Free":
|
| 69 |
+
return 'background-color: green'
|
| 70 |
+
elif api_value == "Pro Subscription":
|
| 71 |
+
return 'background-color: blue'
|
| 72 |
+
elif api_value == "Not Responding":
|
| 73 |
+
return 'background-color: red'
|
| 74 |
+
else:
|
| 75 |
+
if cell_value == "Free":
|
| 76 |
+
return 'background-color: green'
|
| 77 |
+
elif cell_value == "Pro Subscription":
|
| 78 |
+
return 'background-color: blue'
|
| 79 |
+
elif cell_value == "Not Responding":
|
| 80 |
+
return 'background-color: red'
|
| 81 |
+
elif cell_value == "β":
|
| 82 |
+
return 'background-color: green'
|
| 83 |
+
elif cell_value == "β":
|
| 84 |
+
return 'background-color: red'
|
| 85 |
+
return ''
|
| 86 |
+
|
| 87 |
+
def search_models(query):
|
| 88 |
+
return display_table(query)
|
| 89 |
+
|
| 90 |
+
description = "This is a space that retrieves the status of all supported HF LLM Serverless Inference APIs.\nUpdates every 2 hours."
|
| 91 |
+
with gr.Blocks() as demo:
|
| 92 |
+
gr.Markdown("## HF Serverless LLM Inference API Status")
|
| 93 |
+
gr.Markdown(description)
|
| 94 |
+
search_box = gr.Textbox(label="Search for a model", placeholder="Type model name here...")
|
| 95 |
+
table = gr.Dataframe(value=display_table(), headers="keys")
|
| 96 |
+
|
| 97 |
+
search_box.change(fn=search_models, inputs=search_box, outputs=table)
|
| 98 |
+
|
| 99 |
+
def update_every_two_hours():
|
| 100 |
+
search_models(search_box.value)
|
| 101 |
+
Timer(7200, update_every_two_hours).start()
|
| 102 |
+
|
| 103 |
+
Timer(7200, update_every_two_hours).start()
|
| 104 |
+
|
| 105 |
+
demo.launch()
|