sedrickkeh commited on
Commit
1de1fd2
1 Parent(s): 2424844

change db to online db

Browse files
Files changed (4) hide show
  1. app.py +2 -2
  2. open_db.py +4 -4
  3. requirements.txt +1 -1
  4. response_db.py +15 -71
app.py CHANGED
@@ -1,5 +1,5 @@
1
  import gradio as gr
2
- from response_db import StResponseDb
3
  from create_cache import Game_Cache
4
  import numpy as np
5
  from PIL import Image
@@ -11,7 +11,7 @@ import uuid
11
  import nltk
12
  nltk.download('punkt')
13
 
14
- db = StResponseDb()
15
  css = """
16
  .chatbot {display:flex;flex-direction:column}
17
  .msg {padding:4px;margin-bottom:4px;border-radius:4px;width:80%}
 
1
  import gradio as gr
2
+ from response_db import ResponseDb
3
  from create_cache import Game_Cache
4
  import numpy as np
5
  from PIL import Image
 
11
  import nltk
12
  nltk.download('punkt')
13
 
14
+ db = ResponseDb()
15
  css = """
16
  .chatbot {display:flex;flex-direction:column}
17
  .msg {padding:4px;margin-bottom:4px;border-radius:4px;width:80%}
open_db.py CHANGED
@@ -1,7 +1,7 @@
1
- import sqlite3
2
  import pandas as pd
3
 
4
- db = sqlite3.connect("response.db")
5
- df = pd.read_sql('SELECT * from responses', db)
6
  print(df)
7
- df.to_csv("responses.csv", index=False)
 
1
+ from response_db import ResponseDb
2
  import pandas as pd
3
 
4
+ db = ResponseDb()
5
+ df = pd.DataFrame(list(db.get()))
6
  print(df)
7
+ df.to_csv("responses.csv", index=False)
requirements.txt CHANGED
@@ -13,4 +13,4 @@ pycocotools
13
  Pillow
14
  benepar
15
  sentence-transformers
16
-
 
13
  Pillow
14
  benepar
15
  sentence-transformers
16
+ pymongo
response_db.py CHANGED
@@ -1,78 +1,22 @@
1
- import sqlite3
2
- from sqlite3 import Connection
3
  import datetime
4
 
5
  class ResponseDb:
6
- DB_PATH = "response.db"
7
-
8
  def __init__(self):
9
- # establish connection
10
- self.con = self.get_connection()
11
- cur = self.con.cursor()
12
- # create table if it doesnt already exist
13
- table_string = "CREATE TABLE IF NOT EXISTS responses (dialogue_id text, task_id text, turn integer, question text, response text, datetime date)"
14
- cur.execute(table_string)
15
- self.con.commit()
16
- cur.close()
17
-
18
- def get_connection(self):
19
- """Put the connection in cache to reuse if path does not change between Streamlit reruns.
20
- NB : https://stackoverflow.com/questions/48218065/programmingerror-sqlite-objects-created-in-a-thread-can-only-be-used-in-that-sa
21
- """
22
- return sqlite3.connect(self.DB_PATH, check_same_thread=False)
23
-
24
- #def __del__(self):
25
- #self.con.close()
26
 
27
  def add(self, dialogue_id, task_id, turn, question, response):
28
- cur = self.con.cursor()
29
  curr_datetime = datetime.datetime.now()
30
- cur.execute(
31
- "insert into responses values (?, ?, ?, ?, ?, ?)",
32
- (dialogue_id, int(task_id), turn, question, response, curr_datetime),
33
- )
34
- self.con.commit()
35
- cur.close()
36
-
37
- def get_id(self, dialogue_id):
38
- cur = self.con.cursor()
39
- cur.execute(
40
- "select * from responses where conv=:id",
41
- {"id": dialogue_id},
42
- )
43
- results = cur.fetchall()
44
- cur.close()
45
- return results
46
-
47
- def get_id_turn(self, dialogue_id, turn):
48
- cur = self.con.cursor()
49
- cur.execute(
50
- "select * from responses where conv=:id and turn=:turn",
51
- {
52
- "id": dialogue_id,
53
- "turn": turn,
54
- },
55
- )
56
- results = cur.fetchall()
57
- cur.close()
58
- return results
59
-
60
- def get_all(self):
61
- cur = self.con.cursor()
62
- cur.execute(
63
- "select * from responses",
64
- )
65
- results = cur.fetchall()
66
- cur.close()
67
- return results
68
-
69
- class StResponseDb(ResponseDb):
70
- def get_connection(self):
71
- """Put the connection in cache to reuse if path does not change between Streamlit reruns.
72
- NB : https://stackoverflow.com/questions/48218065/programmingerror-sqlite-objects-created-in-a-thread-can-only-be-used-in-that-sa
73
- """
74
- return sqlite3.connect(self.DB_PATH, check_same_thread=False)
75
-
76
- if __name__ == "__main__":
77
- db = ResponseDb()
78
- print(db.get_all())
 
1
+ from pymongo import MongoClient
 
2
  import datetime
3
 
4
  class ResponseDb:
 
 
5
  def __init__(self):
6
+ # Set up the connection
7
+ self.client = MongoClient(f"mongodb+srv://{mongodb_username}:{mongodb_pw}@{mongodb_cluster_url}/?retryWrites=true&w=majority")
8
+ self.db = self.client['vqa-game']
9
+ self.collection = self.db['vqa-game']
 
 
 
 
 
 
 
 
 
 
 
 
 
10
 
11
  def add(self, dialogue_id, task_id, turn, question, response):
 
12
  curr_datetime = datetime.datetime.now()
13
+ document = {"dialogue_id":dialogue_id,
14
+ "task_id":task_id,
15
+ "turn":turn,
16
+ "question":question,
17
+ "response":response,
18
+ "datetime":curr_datetime}
19
+ result = self.collection.insert_one(document)
20
+
21
+ def get(self):
22
+ return self.collection.find()