woodmastr commited on
Commit
3c40783
β€’
1 Parent(s): dbf650d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +166 -38
app.py CHANGED
@@ -1,45 +1,174 @@
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,12 +178,11 @@ with gr.Blocks() as demo:
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()
 
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
+ from tqdm import tqdm
7
+ import time
8
 
9
+ HUGGINGFACE_TOKEN = os.environ.get("HUGGINGFACE_TOKEN")
 
 
 
 
 
 
 
 
10
 
11
  def loop_query_data():
12
+ global all_models
13
+ models_dict = InferenceClient(token=HUGGINGFACE_TOKEN).list_deployed_models("text-generation-inference")
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
+ all_models = list(set(all_models + models + models_vision + models_others))
27
+ for m in tqdm(all_models):
28
+ text_available = False
29
+ chat_available = False
30
+ vision_available = False
31
+ if m in models_vision:
32
+ vision_available = True
33
+ pro_sub = False
34
+ run_text = True
35
+ while run_text:
36
+ try:
37
+ run_text = False
38
+ InferenceClient(m, timeout=10, token=HUGGINGFACE_TOKEN).text_generation("Hi.", max_new_tokens=1)
39
+ text_available = True
40
+ except Exception as e:
41
+ # print(e)
42
+ if e and "Model requires a Pro subscription" in str(e):
43
+ pro_sub = True
44
+ if e and "Rate limit reached" in str(e):
45
+ print("Rate Limited, waiting 1 hour...")
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
+ 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!
156
+ 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()`.
157
+ *It may not have all of the available ones... for now... it's WIP*
158
+ So all you need is to plug:
159
+ ```py
160
+ from huggingface_hub import InferenceClient
161
+ inf = InferenceClient(model = "MODEL", token = "TOKEN")
162
+ response = inf.text_generation("And play !!")
163
+ print(response)
164
+ ```
165
+ """
166
  first_run = True
167
  all_models = []
168
  loop_query_data()
 
169
  with gr.Blocks() as demo:
170
  gr.Markdown("## HF Serverless LLM Inference API Status")
171
+ gr.Markdown(description)
172
  search_box = gr.Textbox(label="Search for a model", placeholder="Type model name here...")
173
  filter_box = gr.CheckboxGroup(choices=["Free", "Pro Subscription", "Not Responding", "Text Completion", "Chat Completion", "Vision"], label="Filters")
174
  table = gr.Dataframe(value=display_table(use_cache=True), headers="keys")
 
178
 
179
  search_box.change(fn=update_filters, inputs=[search_box, filter_box], outputs=table)
180
  filter_box.change(fn=update_filters, inputs=[search_box, filter_box], outputs=table)
181
+
182
  def update_every_two_hours(first_run):
183
+ search_models(search_box.value, use_cache = first_run)
184
+ Timer(60, update_every_two_hours, args=(False,)).start()
185
+
 
186
  Timer(0, update_every_two_hours, args=(first_run,)).start()
187
+
188
+ demo.launch()