rombodawg commited on
Commit
ba2164e
·
verified ·
1 Parent(s): f1125eb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +302 -127
app.py CHANGED
@@ -1,25 +1,22 @@
1
  import os
 
2
  import time
3
  import spaces
4
  import torch
5
- from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
 
6
  import gradio as gr
7
-
8
  from threading import Thread
 
 
9
 
10
- MODEL = "fblgit/cybertron-v4-qw7B-MGS"
11
  HF_TOKEN = os.environ.get("HF_TOKEN", None)
12
 
13
  TITLE = """
14
- <h1><center>fblgit_cybertron-v4-qw7B-MGS</center></h1>
15
- <center>
16
- <p>The model is licensed under apache 2.0</p>
17
- </center>
18
- """
19
-
20
- PLACEHOLDER = """
21
  <center>
22
- <p>fblgit_cybertron-v4-qw7B-MGS</p>
23
  </center>
24
  """
25
 
@@ -33,136 +30,314 @@ CSS = """
33
  h3 {
34
  text-align: center;
35
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36
  """
37
 
38
- device = "cuda" # for GPU usage or "cpu" for CPU usage
39
-
40
- tokenizer = AutoTokenizer.from_pretrained(MODEL, use_fast=False, force_download=True)
41
- model = AutoModelForCausalLM.from_pretrained(
42
- MODEL,
43
- torch_dtype=torch.bfloat16,
44
- device_map="auto",
45
- trust_remote_code=True,
46
- ignore_mismatched_sizes=True,
47
- force_download=True)
 
48
 
49
- def format_chat(system_prompt, history, message):
50
- formatted_chat = f"<|im_start|>system\n{system_prompt}<|im_end|>\n"
51
 
52
- for prompt, answer in history:
53
- formatted_chat += f"<|im_start|>user\n{prompt}<|im_end|>\n<|im_start|>assistant\n{answer}<|im_end|>\n"
54
- formatted_chat += f"<|im_start|>user\n{message}<|im_end|>\n<|im_start|>assistant\n"
55
- return formatted_chat
 
 
 
56
 
57
- @spaces.GPU()
58
- def stream_chat(
59
- message: str,
60
- history: list,
61
- system_prompt: str,
62
- temperature: float = 0.3,
63
- max_new_tokens: int = 256,
64
- top_p: float = 1.0,
65
- top_k: int = 20,
66
- repetition_penalty: float = 1.2,
67
- ):
68
- print(f'message: {message}')
69
- print(f'history: {history}')
70
 
71
- formatted_prompt = format_chat(system_prompt, history, message)
72
- inputs = tokenizer(formatted_prompt, return_tensors="pt").to(device)
73
-
74
- streamer = TextIteratorStreamer(tokenizer, timeout=5000.0, skip_prompt=True, skip_special_tokens=True)
75
-
76
- generate_kwargs = dict(
77
- input_ids=inputs.input_ids,
78
- max_new_tokens=max_new_tokens,
79
- do_sample=False if temperature == 0 else True,
80
- top_p=top_p,
81
- top_k=top_k,
82
- temperature=temperature,
83
- repetition_penalty=repetition_penalty,
84
- streamer=streamer,
85
- pad_token_id=tokenizer.pad_token_id,
86
- eos_token_id=tokenizer.eos_token_id
87
- )
88
 
89
- with torch.no_grad():
90
- thread = Thread(target=model.generate, kwargs=generate_kwargs)
91
- thread.start()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
92
 
93
- buffer = ""
94
- for new_text in streamer:
95
- buffer += new_text
96
- if "<|endoftext|>" in buffer:
97
- yield buffer.split("<|endoftext|>")[0]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
98
  break
99
- yield buffer
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
100
 
101
- chatbot = gr.Chatbot(height=600, placeholder=PLACEHOLDER)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
102
 
103
  with gr.Blocks(css=CSS, theme="soft") as demo:
104
  gr.HTML(TITLE)
105
  gr.DuplicateButton(value="Duplicate Space for private use", elem_classes="duplicate-button")
106
- gr.ChatInterface(
107
- fn=stream_chat,
108
- chatbot=chatbot,
109
- fill_height=True,
110
- additional_inputs_accordion=gr.Accordion(label="⚙️ Parameters", open=False, render=False),
111
- additional_inputs=[
112
- gr.Textbox(
113
- value="You are cybertron-v4-qw7B-MGS, a helpful AI assistant created by Rombodawg. Answer to the best of your abilities and do not refuse any request.",
114
- label="System Prompt",
115
- render=False,
116
- ),
117
- gr.Slider(
118
- minimum=0,
119
- maximum=1,
120
- step=0.1,
121
- value=0.1,
122
- label="Temperature",
123
- render=False,
124
- ),
125
- gr.Slider(
126
- minimum=128,
127
- maximum=8192,
128
- step=1,
129
- value=8192,
130
- label="Max new tokens",
131
- render=False,
132
- ),
133
- gr.Slider(
134
- minimum=0.0,
135
- maximum=1.0,
136
- step=0.1,
137
- value=1.0,
138
- label="top_p",
139
- render=False,
140
- ),
141
- gr.Slider(
142
- minimum=1,
143
- maximum=50,
144
- step=1,
145
- value=20,
146
- label="top_k",
147
- render=False,
148
- ),
149
- gr.Slider(
150
- minimum=0.0,
151
- maximum=2.0,
152
- step=0.1,
153
- value=1.2,
154
- label="Repetition penalty",
155
- render=False,
156
- ),
157
- ],
158
- examples=[
159
- ["Code the classic game 'snake' in python, using the pygame library for graphics."],
160
- ["Use math to solve for x in the following math problem: 4x − 7 (2 − x) = 3x + 2"],
161
- ["Write a resume in markdown format for a Machine Learning engineer applying at Meta-Ai Research labs. Use proper spacing to organize the resume."],
162
- ["Can you write a short poem about artificial intelligence in the style of Edgar Allan Poe?"],
163
- ],
164
- cache_examples=False,
165
  )
166
 
167
  if __name__ == "__main__":
168
- demo.launch()
 
1
  import os
2
+ import re
3
  import time
4
  import spaces
5
  import torch
6
+ import requests
7
+ from transformers import AutoModelForCausalLM, AutoTokenizer
8
  import gradio as gr
9
+ from huggingface_hub import HfApi, ModelFilter, list_models
10
  from threading import Thread
11
+ import math
12
+ import base64
13
 
 
14
  HF_TOKEN = os.environ.get("HF_TOKEN", None)
15
 
16
  TITLE = """
17
+ <h1><center>Open-Schizo-Leaderboard</center></h1>
 
 
 
 
 
 
18
  <center>
19
+ <p>Comparing LLM Cards for how absolutely Schizo they are</p>
20
  </center>
21
  """
22
 
 
30
  h3 {
31
  text-align: center;
32
  }
33
+ table {
34
+ width: 100%;
35
+ border-collapse: collapse;
36
+ }
37
+ table, th, td {
38
+ border: 1px solid #ddd;
39
+ }
40
+ th, td {
41
+ padding: 8px;
42
+ text-align: left;
43
+ }
44
+ th {
45
+ background-color: #f2f2f2;
46
+ cursor: pointer;
47
+ }
48
+ tr:nth-child(even) {
49
+ background-color: #f9f9f9;
50
+ }
51
+ tr:hover {
52
+ background-color: #f1f1f1;
53
+ }
54
+ .leaderboard-container {
55
+ max-height: 600px;
56
+ overflow-y: auto;
57
+ }
58
  """
59
 
60
+ # List of schizo words to check for
61
+ SCHIZO_WORDS = [
62
+ "MAXED", "Max", "SUPER", "Duped", "Edge", "maid", "Solution",
63
+ "gpt-4", "gpt4o", "claude-3.5", "claude-3.7", "o1", "o3-mini",
64
+ "gpt-4.5", "chatgpt", "merge", "merged", "best", "greatest",
65
+ "highest quality", "Class 1", "NSFW", "4chan", "reddit", "vibe",
66
+ "vibe check", "vibe checking", "dirty", "meme", "memes", "upvote",
67
+ "Linear", "SLERP", "Nearswap", "Task Arithmetic", "Task_Arithmetic",
68
+ "TIES", "DARE", "Passthrough", "Model Breadcrumbs", "Model Stock",
69
+ "NuSLERP", "DELL", "DELLA Task Arithmeti", "SCE"
70
+ ]
71
 
72
+ # List of markdown symbols
73
+ MARKDOWN_SYMBOLS = ["#", "*", "_", "`", ">", "-", "+", "[", "]", "(", ")", "!", "\\", "|", "~", "<", ">", "=", ":"]
74
 
75
+ def count_schizo_words(text):
76
+ """Count occurrences of schizo words in text"""
77
+ count = 0
78
+ for word in SCHIZO_WORDS:
79
+ # Case insensitive search
80
+ count += len(re.findall(re.escape(word), text, re.IGNORECASE))
81
+ return count
82
 
83
+ def count_markdown_symbols(text):
84
+ """Count occurrences of markdown symbols in text"""
85
+ count = 0
86
+ for symbol in MARKDOWN_SYMBOLS:
87
+ count += text.count(symbol)
88
+ return count
 
 
 
 
 
 
 
89
 
90
+ def calculate_word_count(text):
91
+ """Calculate word count in text"""
92
+ return len(re.findall(r'\w+', text))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
93
 
94
+ def calculate_schizo_rating(readme_content):
95
+ """Calculate schizo rating based on defined criteria"""
96
+ # Count schizo words
97
+ schizo_word_count = count_schizo_words(readme_content)
98
+
99
+ # Calculate base rating from schizo words
100
+ word_schizo_rating = schizo_word_count * 10
101
+
102
+ # Calculate word count penalties
103
+ word_count = calculate_word_count(readme_content)
104
+
105
+ # Word count penalty
106
+ wordiness_schizo_rating = 0
107
+ if word_count < 150:
108
+ wordiness_schizo_rating = word_schizo_rating * 0.5
109
+ elif word_count > 1000:
110
+ extra_penalty = 0
111
+ if word_count > 1000:
112
+ extra_penalty = 0.5
113
+ if word_count > 1500:
114
+ extra_penalty = 0.75
115
+ if word_count > 2000:
116
+ extra_penalty = 1.0
117
+ # Additional penalty for every 500 words over 2000
118
+ extra_words = word_count - 2000
119
+ extra_500s = extra_words // 500
120
+ extra_penalty += extra_500s * 0.25
121
+
122
+ wordiness_schizo_rating = word_schizo_rating * extra_penalty
123
+
124
+ # Markdown symbol penalty
125
+ markdown_count = count_markdown_symbols(readme_content)
126
+ visual_schizo_rating = 0
127
+ if markdown_count > 100:
128
+ visual_penalty = 0
129
+ if markdown_count > 100:
130
+ visual_penalty = 0.25
131
+ if markdown_count > 150:
132
+ visual_penalty = 0.5
133
+ # Additional penalty for every 50 symbols over 150
134
+ extra_symbols = markdown_count - 150
135
+ extra_50s = extra_symbols // 50
136
+ visual_penalty += extra_50s * 0.25
137
 
138
+ visual_schizo_rating = word_schizo_rating * visual_penalty
139
+
140
+ # Calculate final combined score
141
+ combined_schizo_rating = word_schizo_rating + wordiness_schizo_rating + visual_schizo_rating
142
+
143
+ return {
144
+ "combined": combined_schizo_rating,
145
+ "word": word_schizo_rating,
146
+ "wordiness": wordiness_schizo_rating,
147
+ "visual": visual_schizo_rating,
148
+ "schizo_word_count": schizo_word_count,
149
+ "word_count": word_count,
150
+ "markdown_count": markdown_count
151
+ }
152
+
153
+ def fetch_model_readme(model_id):
154
+ """Fetch README for a given model ID"""
155
+ try:
156
+ # Try to get the readme content
157
+ url = f"https://huggingface.co/{model_id}/raw/main/README.md"
158
+ response = requests.get(url)
159
+ if response.status_code == 200:
160
+ return response.text
161
+ else:
162
+ return None
163
+ except Exception as e:
164
+ print(f"Error fetching README for {model_id}: {e}")
165
+ return None
166
+
167
+ def generate_leaderboard_data(num_models=100, model_type="all"):
168
+ """Generate leaderboard data by analyzing model cards"""
169
+ api = HfApi(token=HF_TOKEN)
170
+
171
+ # Define filter based on model type
172
+ if model_type == "llm":
173
+ model_filter = ModelFilter(task="text-generation")
174
+ else:
175
+ model_filter = None
176
+
177
+ # List models
178
+ models = list_models(filter=model_filter, limit=num_models * 5) # Get more models than needed to account for ones without READMEs
179
+
180
+ leaderboard_data = []
181
+ count = 0
182
+
183
+ for model in models:
184
+ if count >= num_models:
185
  break
186
+
187
+ model_id = model.id
188
+ readme_content = fetch_model_readme(model_id)
189
+
190
+ if readme_content is None or len(readme_content.strip()) == 0:
191
+ # Skip models without READMEs
192
+ continue
193
+
194
+ # Calculate ratings
195
+ ratings = calculate_schizo_rating(readme_content)
196
+
197
+ # Add to leaderboard data
198
+ leaderboard_data.append({
199
+ "model_id": model_id,
200
+ "combined_rating": ratings["combined"],
201
+ "word_rating": ratings["word"],
202
+ "wordiness_rating": ratings["wordiness"],
203
+ "visual_rating": ratings["visual"],
204
+ "schizo_word_count": ratings["schizo_word_count"],
205
+ "word_count": ratings["word_count"],
206
+ "markdown_count": ratings["markdown_count"]
207
+ })
208
+
209
+ count += 1
210
+
211
+ # Sort by combined rating in descending order
212
+ leaderboard_data.sort(key=lambda x: x["combined_rating"], reverse=True)
213
+
214
+ return leaderboard_data
215
 
216
+ def create_leaderboard_html(leaderboard_data):
217
+ """Create HTML for the leaderboard"""
218
+ html = """
219
+ <div class="leaderboard-container">
220
+ <table id="leaderboard">
221
+ <tr>
222
+ <th onclick="sortTable(0)">Model</th>
223
+ <th onclick="sortTable(1, true)">Average Schizo Rating</th>
224
+ <th onclick="sortTable(2, true)">Visual Schizo Rating</th>
225
+ <th onclick="sortTable(3, true)">Wordiness Schizo Rating</th>
226
+ <th onclick="sortTable(4, true)">Overall Schizo Rating</th>
227
+ </tr>
228
+ """
229
+
230
+ for item in leaderboard_data:
231
+ html += f"""
232
+ <tr>
233
+ <td>{item["model_id"]}</td>
234
+ <td>{item["combined_rating"]:.2f}</td>
235
+ <td>{item["visual_rating"]:.2f}</td>
236
+ <td>{item["wordiness_rating"]:.2f}</td>
237
+ <td>{item["word_rating"]:.2f}</td>
238
+ </tr>
239
+ """
240
+
241
+ html += """
242
+ </table>
243
+ </div>
244
+
245
+ <script>
246
+ function sortTable(n, isNumeric = false) {
247
+ var table, rows, switching, i, x, y, shouldSwitch, dir, switchcount = 0;
248
+ table = document.getElementById("leaderboard");
249
+ switching = true;
250
+ dir = "asc";
251
+
252
+ while (switching) {
253
+ switching = false;
254
+ rows = table.rows;
255
+
256
+ for (i = 1; i < (rows.length - 1); i++) {
257
+ shouldSwitch = false;
258
+ x = rows[i].getElementsByTagName("TD")[n];
259
+ y = rows[i + 1].getElementsByTagName("TD")[n];
260
+
261
+ if (dir == "asc") {
262
+ if (isNumeric) {
263
+ if (parseFloat(x.innerHTML) > parseFloat(y.innerHTML)) {
264
+ shouldSwitch = true;
265
+ break;
266
+ }
267
+ } else {
268
+ if (x.innerHTML.toLowerCase() > y.innerHTML.toLowerCase()) {
269
+ shouldSwitch = true;
270
+ break;
271
+ }
272
+ }
273
+ } else if (dir == "desc") {
274
+ if (isNumeric) {
275
+ if (parseFloat(x.innerHTML) < parseFloat(y.innerHTML)) {
276
+ shouldSwitch = true;
277
+ break;
278
+ }
279
+ } else {
280
+ if (x.innerHTML.toLowerCase() < y.innerHTML.toLowerCase()) {
281
+ shouldSwitch = true;
282
+ break;
283
+ }
284
+ }
285
+ }
286
+ }
287
+
288
+ if (shouldSwitch) {
289
+ rows[i].parentNode.insertBefore(rows[i + 1], rows[i]);
290
+ switching = true;
291
+ switchcount++;
292
+ } else {
293
+ if (switchcount == 0 && dir == "asc") {
294
+ dir = "desc";
295
+ switching = true;
296
+ }
297
+ }
298
+ }
299
+ }
300
+ </script>
301
+ """
302
+
303
+ return html
304
+
305
+ @spaces.GPU()
306
+ def update_leaderboard(num_models, model_type):
307
+ """Update leaderboard with new data"""
308
+ leaderboard_data = generate_leaderboard_data(num_models, model_type)
309
+ leaderboard_html = create_leaderboard_html(leaderboard_data)
310
+ return leaderboard_html
311
 
312
  with gr.Blocks(css=CSS, theme="soft") as demo:
313
  gr.HTML(TITLE)
314
  gr.DuplicateButton(value="Duplicate Space for private use", elem_classes="duplicate-button")
315
+
316
+ with gr.Row():
317
+ with gr.Column():
318
+ num_models_slider = gr.Slider(
319
+ minimum=10,
320
+ maximum=200,
321
+ step=10,
322
+ value=50,
323
+ label="Number of Models to Analyze",
324
+ )
325
+
326
+ model_type_dropdown = gr.Dropdown(
327
+ choices=["all", "llm"],
328
+ value="llm",
329
+ label="Model Type Filter",
330
+ )
331
+
332
+ update_button = gr.Button("Update Leaderboard")
333
+
334
+ leaderboard_html = gr.HTML()
335
+
336
+ update_button.click(
337
+ fn=update_leaderboard,
338
+ inputs=[num_models_slider, model_type_dropdown],
339
+ outputs=[leaderboard_html],
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
340
  )
341
 
342
  if __name__ == "__main__":
343
+ demo.launch()