Pendrokar commited on
Commit
878ff6b
1 Parent(s): 3617d86

DB: link spokentext to votelog ID

Browse files
Files changed (1) hide show
  1. app.py +57 -27
app.py CHANGED
@@ -411,10 +411,16 @@ def create_db_if_missing():
411
  cursor.execute('''
412
  CREATE TABLE IF NOT EXISTS spokentext (
413
  id INTEGER PRIMARY KEY AUTOINCREMENT,
 
414
  spokentext TEXT,
 
415
  timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP
416
  );
417
  ''')
 
 
 
 
418
  def get_db():
419
  return sqlite3.connect(DB_PATH)
420
 
@@ -755,10 +761,15 @@ def upvote_model(model, uname):
755
  with scheduler.lock:
756
  conn.commit()
757
  cursor.close()
758
- def log_text(text):
 
 
 
 
759
  conn = get_db()
760
  cursor = conn.cursor()
761
- cursor.execute('INSERT INTO spokentext (spokentext) VALUES (?)', (text,))
 
762
  with scheduler.lock:
763
  conn.commit()
764
  cursor.close()
@@ -773,36 +784,52 @@ def downvote_model(model, uname):
773
  conn.commit()
774
  cursor.close()
775
 
776
- def a_is_better(model1, model2, userid):
777
- # print("A is better", model1, model2)
778
- if not model1 in AVAILABLE_MODELS.keys() and not model1 in AVAILABLE_MODELS.values():
779
- raise gr.Error('Sorry, please try voting again.')
780
- userid = mkuuid(userid)
781
- if model1 and model2:
782
- conn = get_db()
783
- cursor = conn.cursor()
784
- cursor.execute('INSERT INTO votelog (username, chosen, rejected) VALUES (?, ?, ?)', (str(userid), model1, model2,))
785
- with scheduler.lock:
786
- conn.commit()
787
- cursor.close()
788
- upvote_model(model1, str(userid))
789
- downvote_model(model2, str(userid))
790
- return reload(model1, model2, userid, chose_a=True)
791
- def b_is_better(model1, model2, userid):
792
- # print("B is better", model1, model2)
793
- if not model1 in AVAILABLE_MODELS.keys() and not model1 in AVAILABLE_MODELS.values():
794
  raise gr.Error('Sorry, please try voting again.')
 
 
795
  userid = mkuuid(userid)
796
  if model1 and model2:
797
  conn = get_db()
798
  cursor = conn.cursor()
799
- cursor.execute('INSERT INTO votelog (username, chosen, rejected) VALUES (?, ?, ?)', (str(userid), model2, model1,))
 
 
 
 
 
800
  with scheduler.lock:
801
  conn.commit()
 
 
 
802
  cursor.close()
803
- upvote_model(model2, str(userid))
804
- downvote_model(model1, str(userid))
805
- return reload(model1, model2, userid, chose_b=True)
 
 
 
 
 
 
 
 
806
  def both_bad(model1, model2, userid):
807
  userid = mkuuid(userid)
808
  if model1 and model2:
@@ -954,17 +981,20 @@ def synthandreturn(text):
954
  except:
955
  pass
956
  # Get two random models
 
957
  # forced model: your TTS model versus The World!!!
958
  # mdl1 = 'Pendrokar/xVASynth'
959
- vsModels = dict(AVAILABLE_MODELS)
960
  # del vsModels[mdl1]
961
  # randomize position of the forced model
962
- mdl2 = random.sample(list(vsModels.keys()), 1)
963
  # forced random
964
  # mdl1, mdl2 = random.sample(list([mdl1, mdl2[0]]), 2)
 
965
  # actual random
966
  mdl1, mdl2 = random.sample(list(AVAILABLE_MODELS.keys()), 2)
967
- log_text(text)
 
968
  print("[debug] Using", mdl1, mdl2)
969
  def predict_and_update_result(text, model, result_storage):
970
  # 3 attempts
 
411
  cursor.execute('''
412
  CREATE TABLE IF NOT EXISTS spokentext (
413
  id INTEGER PRIMARY KEY AUTOINCREMENT,
414
+ votelog_id INTEGER UNIQUE,
415
  spokentext TEXT,
416
+ lang TEXT,
417
  timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP
418
  );
419
  ''')
420
+ # foreign keys
421
+ cursor.execute('''
422
+ CREATE UNIQUE INDEX IF NOT EXISTS st_to_vl ON spokentext(votelog_id);
423
+ ''')
424
  def get_db():
425
  return sqlite3.connect(DB_PATH)
426
 
 
761
  with scheduler.lock:
762
  conn.commit()
763
  cursor.close()
764
+ def log_text(text, voteid):
765
+ # log only hardcoded sentences
766
+ if (text not in sents):
767
+ return
768
+
769
  conn = get_db()
770
  cursor = conn.cursor()
771
+ # TODO: multilang
772
+ cursor.execute('INSERT INTO spokentext (spokentext, lang, votelog_id) VALUES (?,?,?)', (text,'en',voteid))
773
  with scheduler.lock:
774
  conn.commit()
775
  cursor.close()
 
784
  conn.commit()
785
  cursor.close()
786
 
787
+ def a_is_better(model1, model2, userid, text):
788
+ return is_better(model1, model2, userid, text, True)
789
+ def b_is_better(model1, model2, userid, text):
790
+ return is_better(model1, model2, userid, text, False)
791
+
792
+ def is_better(model1, model2, userid, text, chose_a):
793
+ if(
794
+ (
795
+ not model1 in AVAILABLE_MODELS.keys()
796
+ and not model1 in AVAILABLE_MODELS.values()
797
+ )
798
+ or (
799
+ not model2 in AVAILABLE_MODELS.keys()
800
+ and not model2 in AVAILABLE_MODELS.values()
801
+ )
802
+ ):
 
 
803
  raise gr.Error('Sorry, please try voting again.')
804
+
805
+ # userid is unique for each cast vote pair
806
  userid = mkuuid(userid)
807
  if model1 and model2:
808
  conn = get_db()
809
  cursor = conn.cursor()
810
+ sql_query = 'INSERT INTO votelog (username, chosen, rejected) VALUES (?, ?, ?)'
811
+ if chose_a:
812
+ cursor.execute(sql_query, (str(userid), model1, model2))
813
+ else:
814
+ cursor.execute(sql_query, (str(userid), model2, model1))
815
+
816
  with scheduler.lock:
817
  conn.commit()
818
+ # also retrieve primary key ID
819
+ cursor.execute('SELECT last_insert_rowid()')
820
+ votelogid = cursor.fetchone()[0]
821
  cursor.close()
822
+
823
+ if chose_a:
824
+ upvote_model(model1, str(userid))
825
+ downvote_model(model2, str(userid))
826
+ else:
827
+ upvote_model(model2, str(userid))
828
+ downvote_model(model1, str(userid))
829
+ log_text(text, votelogid)
830
+
831
+ return reload(model1, model2, userid, chose_a=chose_a, chose_b=(not chose_a))
832
+
833
  def both_bad(model1, model2, userid):
834
  userid = mkuuid(userid)
835
  if model1 and model2:
 
981
  except:
982
  pass
983
  # Get two random models
984
+
985
  # forced model: your TTS model versus The World!!!
986
  # mdl1 = 'Pendrokar/xVASynth'
987
+ # vsModels = dict(AVAILABLE_MODELS)
988
  # del vsModels[mdl1]
989
  # randomize position of the forced model
990
+ # mdl2 = random.sample(list(vsModels.keys()), 1)
991
  # forced random
992
  # mdl1, mdl2 = random.sample(list([mdl1, mdl2[0]]), 2)
993
+
994
  # actual random
995
  mdl1, mdl2 = random.sample(list(AVAILABLE_MODELS.keys()), 2)
996
+ # pointless saving of text to DB
997
+ # log_text(text)
998
  print("[debug] Using", mdl1, mdl2)
999
  def predict_and_update_result(text, model, result_storage):
1000
  # 3 attempts