Spaces:
iyosha
/
Running on CPU Upgrade

File size: 2,448 Bytes
38c5e59
 
 
 
 
 
 
 
 
c105068
38c5e59
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d4b26c5
 
 
 
 
 
 
38c5e59
 
 
 
 
 
 
 
d4b26c5
38c5e59
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import json
import gspread
import pandas as pd
from datetime import datetime
from oauth2client.service_account import ServiceAccountCredentials


class Backend:
    def __init__(self, sheet_name: str, credentials: str):
        creds_dict = json.loads(credentials)
        scope = [
            "https://spreadsheets.google.com/feeds",
            "https://www.googleapis.com/auth/drive",
        ]
        credentials = ServiceAccountCredentials.from_json_keyfile_dict(
            creds_dict, scope
        )
        client = gspread.authorize(credentials)
        self.sheet = client.open(sheet_name).sheet1
        self.header = self.sheet.row_values(1)

    def get_all_rows(self) -> pd.DataFrame:
        records = self.sheet.get_all_records()
        return pd.DataFrame.from_records(records)

    def add_row(
        self,
        index_in_dataset,
        interpretation_id,
        transcription_id,
        user_id,
        answer,
        user_name,
    ):
        timestamp = datetime.utcnow().isoformat()
        self.sheet.append_row(
            [
                index_in_dataset,
                interpretation_id,
                transcription_id,
                user_id,
                user_name,
                answer,
                timestamp,
            ]
        )

    def update_row(self, index_in_dataset, interpretation_id, user_id, new_answer):
        records = self.get_all_rows().to_dict("records")
        for idx, row in enumerate(records):
            if (
                row["interpretation_id"] == interpretation_id
                and row["index_in_dataset"] == index_in_dataset
                and row["user_id"] == user_id
            ):
                sheet_row = (
                    idx + 2
                )  # +2 because sheet rows are 1-indexed and header is row 1
                if row["answer"] != new_answer:
                    self.sheet.update_cell(
                        sheet_row, self.header.index("answer") + 1, new_answer
                    )
                    self.sheet.update_cell(
                        sheet_row,
                        self.header.index("timestamp") + 1,
                        datetime.utcnow().isoformat(),
                    )
                return True
        return False

    def get_answer_count(self, interpretation_id):
        df = self.get_all_rows()
        return df[df["interpretation_id"] == interpretation_id]["user_id"].nunique()