Download app/main.py from Vsevolod/test: direct link, hf CLI and curl.
- Browser
- Download file 2.47 kB
-
https://huggingface.co/spaces/Vsevolod/test/resolve/main/app/main.py
- Command line
-
hf download hf://spaces/Vsevolod/test/app/main.py
-
curl -L -o main.py https://huggingface.co/spaces/Vsevolod/test/resolve/main/app/main.py
2.47 kB
| import os | |
| import csv | |
| import time | |
| import gradio as gr | |
| from fastapi import FastAPI | |
| from fastapi_utils.tasks import repeat_every | |
| class Timer(object): | |
| def __enter__(self): | |
| self.start = time.perf_counter() | |
| return self | |
| def __exit__(self, typ, value, traceback): | |
| self.elapsed = time.perf_counter() - self.start | |
| file_changed = False | |
| HF_TOKEN = os.getenv('HF_TOKEN') | |
| root_dataset_path = "datasets" | |
| huggingface_dataset_path = "Vsevolod/text2rec-users-feedback" | |
| feedback_dataset = gr.HuggingFaceDatasetSaver(HF_TOKEN, huggingface_dataset_path) | |
| local_dataset_path = f"{root_dataset_path}/{huggingface_dataset_path}" | |
| data_path = f"{local_dataset_path}/data.csv" | |
| print(os.getcwd()) | |
| print(local_dataset_path) | |
| def foo(x): | |
| global file_changed | |
| now = time.time() | |
| with open(data_path, mode="a", newline="", encoding="utf-8") as file: | |
| writer = csv.writer(file) | |
| writer.writerow(("Фильмы про путешествия во времени","Грань будущего","Релевантно", "mNpuTAujx-j1E6mm4PVOMQ", now, 228)) | |
| file_changed = True | |
| return x | |
| def create_interface(): | |
| with gr.Blocks() as demo: | |
| query = gr.Textbox(label="Запрос") | |
| search_btn = gr.Button(label="Поиск") | |
| output = gr.Textbox(label="Ответ") | |
| search_btn.click(foo, inputs=query, outputs=output) | |
| feedback_dataset.setup([], root_dataset_path) | |
| return demo | |
| def get_line_count(data_path): | |
| with open(data_path, "r", encoding="utf-8") as csvfile: | |
| line_count = len([None for row in csv.reader(csvfile)]) - 1 | |
| return line_count | |
| demo = create_interface() | |
| app = FastAPI(docs_url=None, redoc_url=None) | |
| def repeat(): | |
| global file_changed | |
| if not file_changed: | |
| return | |
| print("Updating remote repo") | |
| with Timer() as timer: | |
| feedback_dataset.repo.git_pull(lfs=True) | |
| line_count = get_line_count(data_path) | |
| feedback_dataset.repo.push_to_hub(commit_message="Flagged sample #{}".format(line_count)) | |
| print(f"Elapsed: {timer.elapsed:.3f}") | |
| file_changed = False | |
| app.mount("/", demo.app) | |
| # from fastapi import FastAPI import gradio as gr app = FastAPI() | |
| # @app.get("/") | |
| # def read_main(): | |
| # return {"message": "This is your main app"} | |
| # io = gr.Interface(lambda x: "Hello, " + x + "!", "textbox", "textbox") | |
| # app = gr.mount_gradio_app(app, io, path="/gradio") | |