File size: 4,543 Bytes
4fe8a03
 
 
 
 
 
f3f2130
4fe8a03
 
 
 
 
 
 
 
 
 
0b9ee65
78ed805
0c42908
de07dc3
4fe8a03
b9fa3aa
4fe8a03
 
 
 
 
1c010e6
 
4fe8a03
 
 
 
 
b9fa3aa
4fe8a03
 
 
 
 
 
 
 
 
 
 
 
6b5276f
4fe8a03
 
 
 
 
03e689c
4fe8a03
6b5276f
4fe8a03
 
 
50a53df
4fe8a03
 
50a53df
4fe8a03
 
 
54725b9
4fe8a03
 
 
 
 
 
 
d2281a3
4d5642e
 
d2281a3
 
 
0b9ee65
 
 
de07dc3
 
 
 
d2281a3
 
0b9ee65
d2281a3
e74f020
4fe8a03
d2281a3
4fe8a03
a297e9a
5072ba2
5d0fa3e
b7956c7
 
344e7b1
b7956c7
8cafaa1
6b5276f
 
8cafaa1
5fb295f
 
870c16a
5fb295f
6b5276f
13c256e
dc30e8c
 
 
6b5276f
13c256e
dce0c0d
5fb295f
14c3b54
dce0c0d
8828ef5
4fe8a03
b9fa3aa
4fe8a03
 
f99f85f
4fe8a03
 
 
022a19a
 
 
889a394
022a19a
 
73276d2
b9fa3aa
73276d2
4eba62e
6b5276f
 
 
0d6dac3
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import gradio as gr
from bs4 import BeautifulSoup
import requests
from acogsphere import acf
from bcogsphere import bcf
import math

import sqlite3
import huggingface_hub
import pandas as pd
import shutil
import os
import datetime
from apscheduler.schedulers.background import BackgroundScheduler

import random
import time





DB_FILE = "./reviews1.db"

TOKEN = os.environ.get('HF_KEY')

repo = huggingface_hub.Repository(
    local_dir="data",
    repo_type="space",
    clone_from="CogSphere/aCogSphere",
    use_auth_token=TOKEN
)
repo.git_pull()

# Set db to latest
#shutil.copyfile("./data/reviews0.db", DB_FILE)

# Create table if it doesn't already exist

db = sqlite3.connect(DB_FILE)
try:
    db.execute("SELECT * FROM reviews").fetchall()
    db.close()
except sqlite3.OperationalError:
    db.execute(
        '''
        CREATE TABLE reviews (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
                              created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,
                              name TEXT, rate INTEGER, celsci TEXT)
        ''')
    db.commit()
    db.close()

def get_latest_reviews(db: sqlite3.Connection):
    reviews = db.execute("SELECT * FROM reviews ORDER BY id DESC limit 100").fetchall()
    total_reviews = db.execute("Select COUNT(id) from reviews").fetchone()[0]
    reviews = pd.DataFrame(reviews, columns=["id", "date_created", "name", "rate", "celsci"])
    return reviews, total_reviews


def ccogsphere(name: str, rate: int, celsci: str):
    db = sqlite3.connect(DB_FILE)
    cursor = db.cursor()
    cursor.execute("INSERT INTO reviews(name, rate, celsci) VALUES(?,?,?)", [name, rate, celsci])
    db.commit()
    reviews, total_reviews = get_latest_reviews(db)
    db.close()
    #demo.load()
    return reviews, total_reviews

def load_data():
    db = sqlite3.connect(DB_FILE)
    reviews, total_reviews = get_latest_reviews(db)
    db.close()
    return reviews, total_reviews
def run_code():
    from python_actr import log_everything

    #code1="tim = MyAgent()"
    #code2="subway=MyEnv()"
    #code3="subway.agent=tim"
    #code4="log_everything(subway)"]
    from dcogsphere import MyAgent
    from dcogsphere import MyEnv
    tim = MyAgent()
    subway=MyEnv()
    subway.agent=tim
    log_everything(subway)
    code5="subway.run()"
    exec(code5)
    print("done1")
    
css="footer {visibility: hidden}"

with gr.Blocks(css=css) as demo:
    with gr.Row():
        with gr.Column():
            #with gr.Box():
                #gr.Markdown("Based on dataset [here](https://huggingface.co/datasets/freddyaboulton/gradio-reviews)")
            data = gr.Dataframe()
            count = gr.Number(label="Rates!")
    with gr.Row():
        with gr.Column():
            name = gr.Textbox(label="a") #, placeholder="What is your name?")
            rate =  gr.Textbox(label="b") #, placeholder="What is your name?") #gr.Radio(label="How satisfied are you with using gradio?", choices=[1, 2, 3, 4, 5])
            celsci = gr.Textbox(label="c") #, lines=10, placeholder="Do you have any feedback on gradio?")
            submit = gr.Button(value=".")
            submit2 = gr.Button(value="Run Code")

            submit2.click(run_code)
             
            submit.click(ccogsphere, [name, rate, celsci], [data, count])
            demo.load(load_data, None, [data, count])
            @name.change(inputs=name, outputs=celsci,_js="window.location.reload()")
            @rate.change(inputs=rate, outputs=name,_js="window.location.reload()")
            @celsci.change(inputs=celsci, outputs=rate,_js="window.location.reload()")

            def secwork(name):
                #if name=="abc":
                #run_code()
                load_data()
                #return "Hello " + name + "!"
    
def backup_db():
    shutil.copyfile(DB_FILE, "./reviews.2db")
    db = sqlite3.connect(DB_FILE)
    reviews = db.execute("SELECT * FROM reviews").fetchall()
    pd.DataFrame(reviews).to_csv("./reviews1.csv", index=False)
    print("updating db")
    repo.push_to_hub(blocking=False, commit_message=f"Updating data at {datetime.datetime.now()}")

def load_data2():
    db = sqlite3.connect(DB_FILE)
    reviews, total_reviews = get_latest_reviews(db)
    #db.close()
    return reviews, total_reviews
    
scheduler = BackgroundScheduler()
scheduler.add_job(func=backup_db, trigger="interval", seconds=240)
scheduler.start()

#scheduler2 = BackgroundScheduler()
#scheduler2.add_job(func=load_data2, trigger="interval", seconds=10)
#scheduler2.start()
demo.launch()