Spaces:
Running on Zero
Running on Zero
Download app.py from ArieLLL123/Meivin-API-Data: direct link, hf CLI and curl.
- Browser
- Download file 1.42 kB
-
https://huggingface.co/spaces/ArieLLL123/Meivin-API-Data/resolve/main/app.py
- Command line
-
hf download hf://spaces/ArieLLL123/Meivin-API-Data/app.py
-
curl -L -o app.py https://huggingface.co/spaces/ArieLLL123/Meivin-API-Data/resolve/main/app.py
1.42 kB
| import json | |
| import os | |
| import uuid | |
| from datetime import datetime, timezone | |
| import gradio as gr | |
| import spaces | |
| from huggingface_hub import HfApi | |
| DATASET_ID = os.environ["DATASET_ID"] | |
| DATASET_TOKEN = os.environ["DATASET_TOKEN"] | |
| api = HfApi(token=DATASET_TOKEN) | |
| # ZeroGPU requires at least one @spaces.GPU function. | |
| # This function is never called, so it does not consume GPU time. | |
| def zerogpu_probe(): | |
| return None | |
| def save_feedback(data: dict) -> dict: | |
| """Receives feedback data and stores it in the private HF dataset.""" | |
| if not isinstance(data, dict): | |
| raise ValueError("data must be a JSON object") | |
| record = { | |
| "id": str(uuid.uuid4()), | |
| "timestamp": datetime.now(timezone.utc).isoformat(), | |
| **data, | |
| } | |
| content = json.dumps(record, ensure_ascii=False) + "\n" | |
| filename = f"incoming/{record['id']}.jsonl" | |
| api.upload_file( | |
| path_or_fileobj=content.encode("utf-8"), | |
| path_in_repo=filename, | |
| repo_id=DATASET_ID, | |
| repo_type="dataset", | |
| token=DATASET_TOKEN, | |
| commit_message="Add feedback record", | |
| ) | |
| return { | |
| "success": True, | |
| "id": record["id"], | |
| } | |
| demo = gr.Interface( | |
| fn=save_feedback, | |
| inputs=gr.JSON(label="Feedback"), | |
| outputs=gr.JSON(label="Result"), | |
| api_name="save_feedback", | |
| title="Zayit Feedback API", | |
| ) | |
| if __name__ == "__main__": | |
| demo.launch() |