Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,7 +3,10 @@ import time
|
|
| 3 |
import random
|
| 4 |
import json
|
| 5 |
import mysql.connector
|
| 6 |
-
import
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
session_index = None
|
| 9 |
|
|
@@ -23,6 +26,38 @@ get_window_session_index = """
|
|
| 23 |
}
|
| 24 |
"""
|
| 25 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
# get url params out of JavaScript function as variables and pass the session_index to fetch_personalized_data() function
|
| 27 |
|
| 28 |
with gr.Blocks() as demo:
|
|
@@ -37,8 +72,7 @@ type `read query`
|
|
| 37 |
def user(user_message, url_params, history):
|
| 38 |
return "", history + [[user_message, None]]
|
| 39 |
|
| 40 |
-
|
| 41 |
-
def bot(history, url_params):
|
| 42 |
global session_index
|
| 43 |
if "read query" in history[-1][0]:
|
| 44 |
session_index = url_params.get('session_index')
|
|
@@ -142,51 +176,13 @@ print(personalized_data)
|
|
| 142 |
demo.launch()
|
| 143 |
|
| 144 |
|
| 145 |
-
def generate_html() -> str:
|
| 146 |
-
with open(DATA_FILE) as csvfile:
|
| 147 |
-
reader = csv.DictReader(csvfile)
|
| 148 |
-
rows = []
|
| 149 |
-
for row in reader:
|
| 150 |
-
rows.append(row)
|
| 151 |
-
rows.reverse()
|
| 152 |
-
if len(rows) == 0:
|
| 153 |
-
return "no messages yet"
|
| 154 |
-
else:
|
| 155 |
-
html = "<div class='chatbot'>"
|
| 156 |
-
for row in rows:
|
| 157 |
-
html += "<div>"
|
| 158 |
-
html += f"<span>{row['name']}</span>"
|
| 159 |
-
html += f"<span class='message'>{row['message']}</span>"
|
| 160 |
-
html += "</div>"
|
| 161 |
-
html += "</div>"
|
| 162 |
-
return html
|
| 163 |
|
| 164 |
-
def store_message(name: str, message: str):
|
| 165 |
-
if name and message:
|
| 166 |
-
with open(DATA_FILE, "a") as csvfile:
|
| 167 |
-
writer = csv.DictWriter(csvfile, fieldnames=["name", "message", "time"])
|
| 168 |
-
writer.writerow(
|
| 169 |
-
{"name": name, "message": message, "time": str(datetime.now())}
|
| 170 |
-
)
|
| 171 |
-
commit_url = repo.push_to_hub()
|
| 172 |
-
return generate_html()
|
| 173 |
|
| 174 |
-
iface = gr.Interface(
|
| 175 |
-
store_message,
|
| 176 |
-
[
|
| 177 |
-
inputs.Textbox(placeholder="Your name"),
|
| 178 |
-
inputs.Textbox(placeholder="Your message", lines=2),
|
| 179 |
-
],
|
| 180 |
-
"html",
|
| 181 |
-
css="""
|
| 182 |
-
.message {background-color:cornflowerblue;color:white; padding:4px;margin:4px;border-radius:4px; }
|
| 183 |
-
""",
|
| 184 |
-
title="Reading/writing to a HuggingFace dataset repo from Spaces",
|
| 185 |
-
description=f"This is a demo of how to do simple *shared data persistence* in a Gradio Space, backed by a dataset repo.",
|
| 186 |
-
article=f"The dataset repo is [{DATASET_REPO_URL}]({DATASET_REPO_URL})",
|
| 187 |
-
)
|
| 188 |
|
| 189 |
-
|
|
|
|
|
|
|
|
|
|
| 190 |
|
| 191 |
|
| 192 |
'''
|
|
|
|
| 3 |
import random
|
| 4 |
import json
|
| 5 |
import mysql.connector
|
| 6 |
+
import os
|
| 7 |
+
import csv
|
| 8 |
+
from datetime import datetime
|
| 9 |
+
from huggingface_hub import Repository, hf_hub_download
|
| 10 |
|
| 11 |
session_index = None
|
| 12 |
|
|
|
|
| 26 |
}
|
| 27 |
"""
|
| 28 |
|
| 29 |
+
DATASET_REPO_URL = "https://huggingface.co/datasets/botsi/trust-game-llama-2-7b-chat"
|
| 30 |
+
DATASET_REPO_ID = "botsi/trust-game-llama-2-7b-chat"
|
| 31 |
+
DATA_FILENAME = "your_data.csv"
|
| 32 |
+
DATA_FILE = os.path.join("data", DATA_FILENAME)
|
| 33 |
+
HF_TOKEN = os.environ.get("HF_TOKEN")
|
| 34 |
+
|
| 35 |
+
# Initialize Hugging Face Dataset Repository
|
| 36 |
+
try:
|
| 37 |
+
hf_hub_download(
|
| 38 |
+
repo_id=DATASET_REPO_ID,
|
| 39 |
+
filename=DATA_FILENAME,
|
| 40 |
+
force_filename=DATA_FILENAME
|
| 41 |
+
)
|
| 42 |
+
except:
|
| 43 |
+
print("File not found")
|
| 44 |
+
|
| 45 |
+
repo = Repository(
|
| 46 |
+
local_dir="data", clone_from=DATASET_REPO_URL, use_auth_token=HF_TOKEN
|
| 47 |
+
)
|
| 48 |
+
|
| 49 |
+
# Store Message Function
|
| 50 |
+
def store_message(name: str, message: str):
|
| 51 |
+
if name and message:
|
| 52 |
+
with open(DATA_FILE, "a") as csvfile:
|
| 53 |
+
writer = csv.DictWriter(csvfile, fieldnames=["name", "message", "time"])
|
| 54 |
+
writer.writerow(
|
| 55 |
+
{"name": name, "message": message, "time": str(datetime.now())}
|
| 56 |
+
)
|
| 57 |
+
commit_url = repo.push_to_hub()
|
| 58 |
+
return generate_html()
|
| 59 |
+
|
| 60 |
+
|
| 61 |
# get url params out of JavaScript function as variables and pass the session_index to fetch_personalized_data() function
|
| 62 |
|
| 63 |
with gr.Blocks() as demo:
|
|
|
|
| 72 |
def user(user_message, url_params, history):
|
| 73 |
return "", history + [[user_message, None]]
|
| 74 |
|
| 75 |
+
def bot(history, url_params):
|
|
|
|
| 76 |
global session_index
|
| 77 |
if "read query" in history[-1][0]:
|
| 78 |
session_index = url_params.get('session_index')
|
|
|
|
| 176 |
demo.launch()
|
| 177 |
|
| 178 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 179 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 180 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 181 |
|
| 182 |
+
|
| 183 |
+
|
| 184 |
+
|
| 185 |
+
|
| 186 |
|
| 187 |
|
| 188 |
'''
|