Spaces:
Runtime error
Runtime error
Update app.py
Browse filesBelow is an example of how you might make such a call to an API, handle the potential KeyError, and integrate this with your Gradio interface:
app.py
CHANGED
@@ -1,176 +1,45 @@
|
|
1 |
-
import
|
2 |
import gradio as gr
|
3 |
-
import pandas as pd
|
4 |
-
from huggingface_hub import InferenceClient
|
5 |
from threading import Timer
|
6 |
-
from tqdm import tqdm
|
7 |
-
import time
|
8 |
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
models = models_dict['text-generation'] + models_dict['text2text-generation']
|
15 |
-
models_vision = models_dict['image-text-to-text']
|
16 |
-
models_others = InferenceClient(token=HUGGINGFACE_TOKEN).list_deployed_models(frameworks="all")["text-generation"]
|
17 |
-
|
18 |
-
models_conclusion = {
|
19 |
-
"Model": [],
|
20 |
-
"API": [],
|
21 |
-
"Text Completion": [],
|
22 |
-
"Chat Completion": [],
|
23 |
-
"Vision": []
|
24 |
}
|
|
|
|
|
|
|
25 |
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
time.sleep(60*60)
|
47 |
-
run_text = True
|
48 |
-
run_chat = True
|
49 |
-
while run_chat:
|
50 |
-
try:
|
51 |
-
run_chat = False
|
52 |
-
InferenceClient(m, timeout=10).chat_completion(messages=[{'role': 'user', 'content': 'Hi.'}], max_tokens=1)
|
53 |
-
chat_available = True
|
54 |
-
except Exception as e:
|
55 |
-
# print(e)
|
56 |
-
if e and "Model requires a Pro subscription" in str(e):
|
57 |
-
pro_sub = True
|
58 |
-
if e and "Rate limit reached" in str(e):
|
59 |
-
print("Rate Limited, waiting 1 hour...")
|
60 |
-
time.sleep(60*60)
|
61 |
-
run_chat = True
|
62 |
-
models_conclusion["Model"].append(m)
|
63 |
-
models_conclusion["API"].append("Free" if chat_available or text_available else ("Pro Subscription" if pro_sub else "Not Responding"))
|
64 |
-
models_conclusion["Chat Completion"].append("---" if (pro_sub or (not chat_available and not text_available)) else ("✓" if chat_available else "⌀"))
|
65 |
-
models_conclusion["Text Completion"].append("---" if (pro_sub or (not chat_available and not text_available)) else ("✓" if text_available else "⌀"))
|
66 |
-
models_conclusion["Vision"].append("✓" if vision_available else "⌀")
|
67 |
-
pd.DataFrame(models_conclusion).to_csv(str(os.getcwd())+"/data.csv", index=False)
|
68 |
-
return models_conclusion
|
69 |
-
|
70 |
-
def get_available_free(use_cache = False):
|
71 |
-
if use_cache:
|
72 |
-
if os.path.exists(str(os.getcwd())+"/data.csv"):
|
73 |
-
# print("Loading data from file...")
|
74 |
-
return pd.read_csv("data.csv").to_dict(orient='list')
|
75 |
-
else:
|
76 |
-
return loop_query_data()
|
77 |
-
|
78 |
-
def update_data(use_cache = False):
|
79 |
-
data = get_available_free(use_cache)
|
80 |
-
print(data)
|
81 |
-
df = pd.DataFrame(data)
|
82 |
-
|
83 |
-
status_mapping = {"✓": 0, "⌀": 1, "---": 2}
|
84 |
-
|
85 |
-
df['Text Completion'] = df['Text Completion'].map(status_mapping)
|
86 |
-
df['Chat Completion'] = df['Chat Completion'].map(status_mapping)
|
87 |
-
|
88 |
-
df = df.sort_values(by=['API', 'Text Completion', 'Chat Completion', 'Vision'])
|
89 |
-
|
90 |
-
df['Text Completion'] = df['Text Completion'].map({v: k for k, v in status_mapping.items()})
|
91 |
-
df['Chat Completion'] = df['Chat Completion'].map({v: k for k, v in status_mapping.items()})
|
92 |
-
|
93 |
-
return df
|
94 |
-
|
95 |
-
def display_table(search_query="", filters=[], use_cache=False):
|
96 |
-
df = update_data(use_cache)
|
97 |
-
search_query = str(search_query)
|
98 |
-
|
99 |
-
if search_query:
|
100 |
-
filtered_df = df[df["Model"].str.contains(search_query, case=False)]
|
101 |
-
else:
|
102 |
-
filtered_df = df
|
103 |
-
|
104 |
-
if filters:
|
105 |
-
api_filters = [f for f in filters if f in ["Free", "Pro Subscription", "Not Responding"]]
|
106 |
-
if api_filters:
|
107 |
-
filtered_df = filtered_df[filtered_df["API"].isin(api_filters)]
|
108 |
-
if "Text Completion" in filters:
|
109 |
-
filtered_df = filtered_df[filtered_df["Text Completion"] == "✓"]
|
110 |
-
if "Chat Completion" in filters:
|
111 |
-
filtered_df = filtered_df[filtered_df["Chat Completion"] == "✓"]
|
112 |
-
if "Vision" in filters:
|
113 |
-
filtered_df = filtered_df[filtered_df["Vision"] == "✓"]
|
114 |
-
|
115 |
-
styled_df = filtered_df.style.apply(apply_row_styles, axis=1, subset=["Model", "API", "Text Completion", "Chat Completion", "Vision"])
|
116 |
-
return styled_df
|
117 |
-
|
118 |
-
def apply_row_styles(row):
|
119 |
-
api_value = row["API"]
|
120 |
-
return [
|
121 |
-
color_status(api_value, row["Model"]),
|
122 |
-
color_status(api_value, row["API"]),
|
123 |
-
color_status(api_value, row["Text Completion"]),
|
124 |
-
color_status(api_value, row["Chat Completion"]),
|
125 |
-
color_status(api_value, row["Vision"])
|
126 |
-
]
|
127 |
-
|
128 |
-
def color_status(api_value, cell_value):
|
129 |
-
if cell_value == "---":
|
130 |
-
if api_value == "Free":
|
131 |
-
return 'background-color: green'
|
132 |
-
elif api_value == "Pro Subscription":
|
133 |
-
return 'background-color: blue'
|
134 |
-
elif api_value == "Not Responding":
|
135 |
-
return 'background-color: red'
|
136 |
-
else:
|
137 |
-
if cell_value == "Free":
|
138 |
-
return 'background-color: green'
|
139 |
-
elif cell_value == "Pro Subscription":
|
140 |
-
return 'background-color: blue'
|
141 |
-
elif cell_value == "Not Responding":
|
142 |
-
return 'background-color: red'
|
143 |
-
elif cell_value == "✓":
|
144 |
-
return 'background-color: green'
|
145 |
-
elif cell_value == "⌀":
|
146 |
-
return 'background-color: red'
|
147 |
-
return ''
|
148 |
-
|
149 |
-
def search_models(query, filters = [], use_cache = True):
|
150 |
-
return display_table(query, filters, use_cache)
|
151 |
-
|
152 |
-
description = """
|
153 |
-
This is a space that retrieves the status of supported HF LLM Serverless Inference APIs.
|
154 |
-
*Updates every 2 hours!*
|
155 |
-
|
156 |
-
If you are a student or you just want to quickly see what models are available to experiment for free, you are most likely highly interested on the free API huggingface provides... but like me, you struggle to find what models are available or not!
|
157 |
-
This is why I made this space that every 2 hours checks and updates the status of the list of LLMs that are cached and, in theory, supported by retrieving the list in `InferenceClient().list_deployed_models()`.
|
158 |
-
*It may not have all of the available ones... for now... it's WIP*
|
159 |
|
160 |
-
So all you need is to plug:
|
161 |
-
```py
|
162 |
-
from huggingface_hub import InferenceClient
|
163 |
-
inf = InferenceClient(model = "MODEL", token = "TOKEN")
|
164 |
-
response = inf.text_generation("And play !!")
|
165 |
-
print(response)
|
166 |
-
```
|
167 |
-
"""
|
168 |
first_run = True
|
169 |
all_models = []
|
170 |
loop_query_data()
|
|
|
171 |
with gr.Blocks() as demo:
|
172 |
gr.Markdown("## HF Serverless LLM Inference API Status")
|
173 |
-
gr.Markdown(
|
174 |
search_box = gr.Textbox(label="Search for a model", placeholder="Type model name here...")
|
175 |
filter_box = gr.CheckboxGroup(choices=["Free", "Pro Subscription", "Not Responding", "Text Completion", "Chat Completion", "Vision"], label="Filters")
|
176 |
table = gr.Dataframe(value=display_table(use_cache=True), headers="keys")
|
@@ -180,11 +49,12 @@ with gr.Blocks() as demo:
|
|
180 |
|
181 |
search_box.change(fn=update_filters, inputs=[search_box, filter_box], outputs=table)
|
182 |
filter_box.change(fn=update_filters, inputs=[search_box, filter_box], outputs=table)
|
183 |
-
|
184 |
def update_every_two_hours(first_run):
|
185 |
-
|
186 |
-
|
187 |
-
|
|
|
188 |
Timer(0, update_every_two_hours, args=(first_run,)).start()
|
189 |
-
|
190 |
-
demo.launch()
|
|
|
1 |
+
import requests
|
2 |
import gradio as gr
|
|
|
|
|
3 |
from threading import Timer
|
|
|
|
|
4 |
|
5 |
+
def fetch_models():
|
6 |
+
# Example URL, replace with the actual API endpoint
|
7 |
+
url = "https://api.example.com/v1/models"
|
8 |
+
headers = {
|
9 |
+
"Authorization": f"Bearer {TOKEN}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
}
|
11 |
+
response = requests.get(url, headers=headers)
|
12 |
+
response.raise_for_status()
|
13 |
+
return response.json()
|
14 |
|
15 |
+
def loop_query_data():
|
16 |
+
global all_models, first_run
|
17 |
+
try:
|
18 |
+
models_dict = fetch_models()
|
19 |
+
models = models_dict.get('text-generation', []) + models_dict.get('text2text-generation', [])
|
20 |
+
all_models = models
|
21 |
+
except KeyError as e:
|
22 |
+
print(f"KeyError: {e} not found in the models dictionary")
|
23 |
+
all_models = []
|
24 |
+
|
25 |
+
def search_models(query, filters, use_cache=False):
|
26 |
+
# Add your logic here to filter models based on the query and filters
|
27 |
+
filtered_models = [model for model in all_models if query.lower() in model.lower()]
|
28 |
+
# Apply additional filtering based on the 'filters' parameter
|
29 |
+
return filtered_models
|
30 |
+
|
31 |
+
def display_table(use_cache=False):
|
32 |
+
# Create a table display of models
|
33 |
+
data = [{"Model Name": model} for model in all_models]
|
34 |
+
return data
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
35 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
36 |
first_run = True
|
37 |
all_models = []
|
38 |
loop_query_data()
|
39 |
+
|
40 |
with gr.Blocks() as demo:
|
41 |
gr.Markdown("## HF Serverless LLM Inference API Status")
|
42 |
+
gr.Markdown("Description of the API")
|
43 |
search_box = gr.Textbox(label="Search for a model", placeholder="Type model name here...")
|
44 |
filter_box = gr.CheckboxGroup(choices=["Free", "Pro Subscription", "Not Responding", "Text Completion", "Chat Completion", "Vision"], label="Filters")
|
45 |
table = gr.Dataframe(value=display_table(use_cache=True), headers="keys")
|
|
|
49 |
|
50 |
search_box.change(fn=update_filters, inputs=[search_box, filter_box], outputs=table)
|
51 |
filter_box.change(fn=update_filters, inputs=[search_box, filter_box], outputs=table)
|
52 |
+
|
53 |
def update_every_two_hours(first_run):
|
54 |
+
loop_query_data()
|
55 |
+
search_models(search_box.value, [], use_cache=first_run)
|
56 |
+
Timer(7200, update_every_two_hours, args=(False,)).start() # 7200 seconds = 2 hours
|
57 |
+
|
58 |
Timer(0, update_every_two_hours, args=(first_run,)).start()
|
59 |
+
|
60 |
+
demo.launch()
|