init
Browse files- .gitignore +2 -0
- app/main.py +80 -0
- run.py +3 -0
.gitignore
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
__pycache__
|
| 2 |
+
datasets
|
app/main.py
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import csv
|
| 3 |
+
import time
|
| 4 |
+
import gradio as gr
|
| 5 |
+
from fastapi import FastAPI
|
| 6 |
+
from fastapi_utils.tasks import repeat_every
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
class Timer(object):
|
| 10 |
+
def __enter__(self):
|
| 11 |
+
self.start = time.perf_counter()
|
| 12 |
+
return self
|
| 13 |
+
def __exit__(self, typ, value, traceback):
|
| 14 |
+
self.elapsed = time.perf_counter() - self.start
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
file_changed = False
|
| 18 |
+
HF_TOKEN = os.getenv('HF_TOKEN')
|
| 19 |
+
root_dataset_path = "datasets"
|
| 20 |
+
huggingface_dataset_path = "Vsevolod/text2rec-users-feedback"
|
| 21 |
+
feedback_dataset = gr.HuggingFaceDatasetSaver(HF_TOKEN, huggingface_dataset_path)
|
| 22 |
+
local_dataset_path = f"{root_dataset_path}/{huggingface_dataset_path}"
|
| 23 |
+
data_path = f"{local_dataset_path}/data.csv"
|
| 24 |
+
print(os.getcwd())
|
| 25 |
+
print(local_dataset_path)
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
def foo(x):
|
| 29 |
+
global file_changed
|
| 30 |
+
now = time.time()
|
| 31 |
+
with open(data_path, mode="a", newline="", encoding="utf-8") as file:
|
| 32 |
+
writer = csv.writer(file)
|
| 33 |
+
writer.writerow(("Фильмы про путешествия во времени","Грань будущего","Релевантно", "mNpuTAujx-j1E6mm4PVOMQ", now, 228))
|
| 34 |
+
file_changed = True
|
| 35 |
+
return x
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
def create_interface():
|
| 39 |
+
with gr.Blocks() as demo:
|
| 40 |
+
query = gr.Textbox(label="Запрос")
|
| 41 |
+
search_btn = gr.Button(label="Поиск")
|
| 42 |
+
output = gr.Textbox(label="Ответ")
|
| 43 |
+
search_btn.click(foo, inputs=query, outputs=output)
|
| 44 |
+
feedback_dataset.setup([], root_dataset_path)
|
| 45 |
+
return demo
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
def get_line_count(data_path):
|
| 49 |
+
with open(data_path, "r", encoding="utf-8") as csvfile:
|
| 50 |
+
line_count = len([None for row in csv.reader(csvfile)]) - 1
|
| 51 |
+
return line_count
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
demo = create_interface()
|
| 55 |
+
app = FastAPI(docs_url=None, redoc_url=None)
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
@app.on_event("startup")
|
| 59 |
+
@repeat_every(seconds=20)
|
| 60 |
+
def repeat():
|
| 61 |
+
global file_changed
|
| 62 |
+
if not file_changed:
|
| 63 |
+
return
|
| 64 |
+
print("Updating remote repo")
|
| 65 |
+
with Timer() as timer:
|
| 66 |
+
feedback_dataset.repo.git_pull(lfs=True)
|
| 67 |
+
line_count = get_line_count(data_path)
|
| 68 |
+
feedback_dataset.repo.push_to_hub(commit_message="Flagged sample #{}".format(line_count))
|
| 69 |
+
print(f"Elapsed: {timer.elapsed:.3f}")
|
| 70 |
+
file_changed = False
|
| 71 |
+
|
| 72 |
+
app.mount("/", demo.app)
|
| 73 |
+
|
| 74 |
+
|
| 75 |
+
# from fastapi import FastAPI import gradio as gr app = FastAPI()
|
| 76 |
+
# @app.get("/")
|
| 77 |
+
# def read_main():
|
| 78 |
+
# return {"message": "This is your main app"}
|
| 79 |
+
# io = gr.Interface(lambda x: "Hello, " + x + "!", "textbox", "textbox")
|
| 80 |
+
# app = gr.mount_gradio_app(app, io, path="/gradio")
|
run.py
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import subprocess
|
| 2 |
+
|
| 3 |
+
subprocess.run("uvicorn app.main:app --host localhost --port 7860", shell=True)
|