mrfakename commited on
Commit
09cd5ce
β€’
1 Parent(s): b72e390

2x speedup?

Browse files
Files changed (1) hide show
  1. app.py +59 -31
app.py CHANGED
@@ -1,13 +1,7 @@
1
  import gradio as gr
2
- import random
3
- import os
4
- import shutil
5
  import pandas as pd
6
- import sqlite3
7
  from datasets import load_dataset
8
- import threading
9
- import time
10
- import uuid
11
  from pathlib import Path
12
  from huggingface_hub import CommitScheduler, delete_file, hf_hub_download
13
  from gradio_client import Client
@@ -475,6 +469,39 @@ with gr.Blocks() as leaderboard:
475
  # bothgood.click(both_good, outputs=outputs, inputs=[model1, model2, useridstate])
476
 
477
  # vote.load(reload, outputs=[aud1, aud2, model1, model2])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
478
  def synthandreturn(text):
479
  text = text.strip()
480
  if len(text) > MAX_SAMPLE_TXT_LENGTH:
@@ -489,30 +516,31 @@ def synthandreturn(text):
489
  mdl1, mdl2 = random.sample(list(AVAILABLE_MODELS.keys()), 2)
490
  log_text(text)
491
  print("[debug] Using", mdl1, mdl2)
492
- return (
493
- text,
494
- "Synthesize",
495
- gr.update(visible=True), # r2
496
- mdl1, # model1
497
- mdl2, # model2
498
- # 'Vote to reveal model A', # prevmodel1
499
- gr.update(visible=True, value=router.predict(
500
- text,
501
- AVAILABLE_MODELS[mdl1],
502
- api_name="/synthesize"
503
- )), # aud1
504
- # 'Vote to reveal model B', # prevmodel2
505
- gr.update(visible=True, value=router.predict(
506
- text,
507
- AVAILABLE_MODELS[mdl2],
508
- api_name="/synthesize"
509
- )), # aud2
510
- gr.update(visible=True, interactive=True),
511
- gr.update(visible=True, interactive=True),
512
- gr.update(visible=False),
513
- gr.update(visible=False),
514
- gr.update(visible=False), #nxt round btn
515
- )
 
516
  def randomsent():
517
  return random.choice(sents), '🎲'
518
  def clear_stuff():
 
1
  import gradio as gr
 
 
 
2
  import pandas as pd
 
3
  from datasets import load_dataset
4
+ import threading, time, uuid, sqlite3, shutil, os, random, asyncio
 
 
5
  from pathlib import Path
6
  from huggingface_hub import CommitScheduler, delete_file, hf_hub_download
7
  from gradio_client import Client
 
469
  # bothgood.click(both_good, outputs=outputs, inputs=[model1, model2, useridstate])
470
 
471
  # vote.load(reload, outputs=[aud1, aud2, model1, model2])
472
+
473
+ ############
474
+ # 2x speedup (hopefully)
475
+ ############
476
+ async def predict_async(text, model, api_name):
477
+ return await router.predict(text, AVAILABLE_MODELS[model], api_name=api_name)
478
+
479
+ async def predict_both_concurrently(text, mdl1, mdl2):
480
+ task1 = predict_async(text, mdl1, "/synthesize")
481
+ task2 = predict_async(text, mdl2, "/synthesize")
482
+ results = await asyncio.gather(task1, task2)
483
+ return results
484
+
485
+ ############
486
+ # 2x speedup (hopefully)
487
+ ############
488
+
489
+ async def process_predictions(text, mdl1, mdl2):
490
+ aud1, aud2 = await predict_both_concurrently(text, mdl1, mdl2)
491
+ return (
492
+ text,
493
+ "Synthesize",
494
+ gr.update(visible=True),
495
+ mdl1,
496
+ mdl2,
497
+ gr.update(visible=True, value=aud1),
498
+ gr.update(visible=True, value=aud2),
499
+ gr.update(visible=True, interactive=True),
500
+ gr.update(visible=True, interactive=True),
501
+ gr.update(visible=False),
502
+ gr.update(visible=False),
503
+ gr.update(visible=False),
504
+ )
505
  def synthandreturn(text):
506
  text = text.strip()
507
  if len(text) > MAX_SAMPLE_TXT_LENGTH:
 
516
  mdl1, mdl2 = random.sample(list(AVAILABLE_MODELS.keys()), 2)
517
  log_text(text)
518
  print("[debug] Using", mdl1, mdl2)
519
+ return asyncio.run(process_predictions(text, mdl1, mdl2))
520
+ # return (
521
+ # text,
522
+ # "Synthesize",
523
+ # gr.update(visible=True), # r2
524
+ # mdl1, # model1
525
+ # mdl2, # model2
526
+ # # 'Vote to reveal model A', # prevmodel1
527
+ # gr.update(visible=True, value=router.predict(
528
+ # text,
529
+ # AVAILABLE_MODELS[mdl1],
530
+ # api_name="/synthesize"
531
+ # )), # aud1
532
+ # # 'Vote to reveal model B', # prevmodel2
533
+ # gr.update(visible=True, value=router.predict(
534
+ # text,
535
+ # AVAILABLE_MODELS[mdl2],
536
+ # api_name="/synthesize"
537
+ # )), # aud2
538
+ # gr.update(visible=True, interactive=True),
539
+ # gr.update(visible=True, interactive=True),
540
+ # gr.update(visible=False),
541
+ # gr.update(visible=False),
542
+ # gr.update(visible=False), #nxt round btn
543
+ # )
544
  def randomsent():
545
  return random.choice(sents), '🎲'
546
  def clear_stuff():