Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,6 +5,105 @@ import pandas as pd # read csv, df manipulation
|
|
| 5 |
import plotly.express as px # interactive charts
|
| 6 |
import streamlit as st # π data web app development
|
| 7 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
st.set_page_config(
|
| 9 |
page_title="Real-Time Data Science Dashboard",
|
| 10 |
page_icon="β
",
|
|
@@ -89,4 +188,7 @@ for seconds in range(200):
|
|
| 89 |
|
| 90 |
st.markdown("### Detailed Data View")
|
| 91 |
st.dataframe(df)
|
|
|
|
|
|
|
|
|
|
| 92 |
time.sleep(1)
|
|
|
|
| 5 |
import plotly.express as px # interactive charts
|
| 6 |
import streamlit as st # π data web app development
|
| 7 |
|
| 8 |
+
|
| 9 |
+
# PersistDataset -----
|
| 10 |
+
import os
|
| 11 |
+
import csv
|
| 12 |
+
import gradio as gr
|
| 13 |
+
from gradio import inputs, outputs
|
| 14 |
+
import huggingface_hub
|
| 15 |
+
from huggingface_hub import Repository, hf_hub_download, upload_file
|
| 16 |
+
from datetime import datetime
|
| 17 |
+
|
| 18 |
+
# Dataset and Token links - change awacke1 to your own HF id, and add a HF_TOKEN copy to your repo for write permissions
|
| 19 |
+
# This should allow you to save your results to your own Dataset hosted on HF. ---
|
| 20 |
+
#DATASET_REPO_URL = "https://huggingface.co/datasets/awacke1/Carddata.csv"
|
| 21 |
+
DATASET_REPO_URL = "https://huggingface.co/datasets/" + "awacke1/PrivateASRWithMemory.csv"
|
| 22 |
+
#DATASET_REPO_ID = "awacke1/Carddata.csv"
|
| 23 |
+
DATASET_REPO_ID = "awacke1/PrivateASRWithMemory.csv"
|
| 24 |
+
DATA_FILENAME = "PrivateASRWithMemory.csv"
|
| 25 |
+
DATA_FILE = os.path.join("data", DATA_FILENAME)
|
| 26 |
+
HF_TOKEN = os.environ.get("HF_TOKEN")
|
| 27 |
+
|
| 28 |
+
DataText = ""
|
| 29 |
+
# ---------------------------------------------
|
| 30 |
+
|
| 31 |
+
SCRIPT = """
|
| 32 |
+
<script>
|
| 33 |
+
if (!window.hasBeenRun) {
|
| 34 |
+
window.hasBeenRun = true;
|
| 35 |
+
console.log("should only happen once");
|
| 36 |
+
document.querySelector("button.submit").click();
|
| 37 |
+
}
|
| 38 |
+
</script>
|
| 39 |
+
"""
|
| 40 |
+
|
| 41 |
+
@st.experimental_singleton
|
| 42 |
+
def get_database_session(url):
|
| 43 |
+
# Create a database session object that points to the URL.
|
| 44 |
+
return session
|
| 45 |
+
Clear memo
|
| 46 |
+
Clear all in-memory and on-disk memo caches.
|
| 47 |
+
|
| 48 |
+
@st.experimental_memo
|
| 49 |
+
def fetch_and_clean_data(url):
|
| 50 |
+
# Fetch data from URL here, and then clean it up.
|
| 51 |
+
return data
|
| 52 |
+
|
| 53 |
+
if st.checkbox("Clear All"):
|
| 54 |
+
# Clear values from *all* memoized functions
|
| 55 |
+
st.experimental_memo.clear()
|
| 56 |
+
Clear singleton
|
| 57 |
+
Clear all singleton caches.
|
| 58 |
+
|
| 59 |
+
@st.experimental_singleton
|
| 60 |
+
def get_database_session():
|
| 61 |
+
# Create a database session object that points to the URL.
|
| 62 |
+
try:
|
| 63 |
+
DataText = hf_hub_download(
|
| 64 |
+
repo_id=DATASET_REPO_ID,
|
| 65 |
+
filename=DATA_FILENAME,
|
| 66 |
+
cache_dir=DATA_DIRNAME,
|
| 67 |
+
force_filename=DATA_FILENAME
|
| 68 |
+
)
|
| 69 |
+
except:
|
| 70 |
+
print("file not found")
|
| 71 |
+
repo = Repository(
|
| 72 |
+
local_dir="data", clone_from=DATASET_REPO_URL, use_auth_token=HF_TOKEN
|
| 73 |
+
) return session
|
| 74 |
+
|
| 75 |
+
def generate_html() -> str:
|
| 76 |
+
with open(DATA_FILE) as csvfile:
|
| 77 |
+
reader = csv.DictReader(csvfile)
|
| 78 |
+
rows = []
|
| 79 |
+
for row in reader:
|
| 80 |
+
rows.append(row)
|
| 81 |
+
rows.reverse()
|
| 82 |
+
if len(rows) == 0:
|
| 83 |
+
return "no messages yet"
|
| 84 |
+
else:
|
| 85 |
+
html = "<div class='chatbot'>"
|
| 86 |
+
for row in rows:
|
| 87 |
+
html += "<div>"
|
| 88 |
+
html += f"<span>{row['inputs']}</span>"
|
| 89 |
+
html += f"<span class='outputs'>{row['outputs']}</span>"
|
| 90 |
+
html += "</div>"
|
| 91 |
+
html += "</div>"
|
| 92 |
+
return html
|
| 93 |
+
|
| 94 |
+
|
| 95 |
+
def store_message(name: str, message: str):
|
| 96 |
+
if name and message:
|
| 97 |
+
with open(DATA_FILE, "a") as csvfile:
|
| 98 |
+
writer = csv.DictWriter(csvfile, fieldnames=["name", "message", "time"])
|
| 99 |
+
writer.writerow(
|
| 100 |
+
{"name": name.strip(), "message": message.strip(), "time": str(datetime.now())}
|
| 101 |
+
)
|
| 102 |
+
# uncomment line below to begin saving -
|
| 103 |
+
commit_url = repo.push_to_hub()
|
| 104 |
+
return ""
|
| 105 |
+
|
| 106 |
+
|
| 107 |
st.set_page_config(
|
| 108 |
page_title="Real-Time Data Science Dashboard",
|
| 109 |
page_icon="β
",
|
|
|
|
| 188 |
|
| 189 |
st.markdown("### Detailed Data View")
|
| 190 |
st.dataframe(df)
|
| 191 |
+
|
| 192 |
+
st.markdown(DataFile)
|
| 193 |
+
|
| 194 |
time.sleep(1)
|