Spaces:
mrbui1990
/
Running on Zero

mrbui1990 commited on
Commit
ceb74c8
·
verified ·
1 Parent(s): 22c2bcf

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -48
app.py CHANGED
@@ -1,48 +1,48 @@
1
- import os
2
- import requests
3
- import gradio as gr
4
-
5
- HF_MODEL = "TOPAI-Network/nsfw_chat_0124"
6
- HF_TOKEN = os.getenv("HF_TOKEN")
7
- API_URL = f"https://api-inference.huggingface.co/models/{HF_MODEL}"
8
- HEADERS = {"Authorization": f"Bearer {HF_TOKEN}"} if HF_TOKEN else {}
9
-
10
- # Safety / gate: simple age checkbox + confirmation of NSFW
11
- def generate(system_prompt, user_prompt, allow_nsfw):
12
- if not allow_nsfw:
13
- return "Bạn phải đánh dấu xác nhận để sử dụng model này (chứa nội dung nhạy cảm)."
14
- if not HF_TOKEN:
15
- return "HF_TOKEN chưa được cấu hình. Thêm token vào Secrets của Space."
16
-
17
- # Kết hợp prompt (tùy chỉnh theo model)
18
- payload = {
19
- "inputs": f"{system_prompt}\nUser: {user_prompt}",
20
- "options": {"use_cache": False, "wait_for_model": True}
21
- }
22
-
23
- resp = requests.post(API_URL, headers=HEADERS, json=payload, timeout=120)
24
- if resp.status_code != 200:
25
- return f"Error {resp.status_code}: {resp.text}"
26
- data = resp.json()
27
- # Inference API trả về list hoặc dict tùy model; xử lý an toàn:
28
- if isinstance(data, dict) and data.get("error"):
29
- return "Model error: " + data["error"]
30
- # Một số model trả về [{"generated_text": "..."}]
31
- if isinstance(data, list) and "generated_text" in data[0]:
32
- return data[0]["generated_text"]
33
- # Nếu trả về chuỗi hoặc khác
34
- return str(data)
35
-
36
- # UI
37
- with gr.Blocks(title="NSFW Chat (gated)") as demo:
38
- gr.Markdown("## Cảnh báo: nội dung nhạy cảm\nModel này có thể tạo nội dung NSFW. Vui lòng xác nhận tuổi >=18 và chấp nhận rủi ro.")
39
- system = gr.Textbox(value="You are a helpful assistant.", label="System prompt (tuỳ chọn)")
40
- user_in = gr.Textbox(label="User prompt", placeholder="Nhập lời nhắc...")
41
- allow = gr.Checkbox(label="Tôi xác nhận tôi 18+ và đồng ý xem nội dung nhạy cảm", value=False)
42
- out = gr.Textbox(label="Model output")
43
- btn = gr.Button("Gửi")
44
-
45
- btn.click(fn=generate, inputs=[system, user_in, allow], outputs=[out])
46
-
47
- if __name__ == "__main__":
48
- demo.launch()
 
1
+ import os
2
+ import requests
3
+ import gradio as gr
4
+
5
+ HF_MODEL = "TOPAI-Network/nsfw_chat_0124"
6
+ HF_TOKEN = os.getenv("HF_TOKEN")
7
+ API_URL = f"https://router.huggingface.co/hf-inference/models/{HF_MODEL}"
8
+ HEADERS = {"Authorization": f"Bearer {HF_TOKEN}"} if HF_TOKEN else {}
9
+
10
+ # Safety / gate: simple age checkbox + confirmation of NSFW
11
+ def generate(system_prompt, user_prompt, allow_nsfw):
12
+ if not allow_nsfw:
13
+ return "Bạn phải đánh dấu xác nhận để sử dụng model này (chứa nội dung nhạy cảm)."
14
+ if not HF_TOKEN:
15
+ return "HF_TOKEN chưa được cấu hình. Thêm token vào Secrets của Space."
16
+
17
+ # Kết hợp prompt (tùy chỉnh theo model)
18
+ payload = {
19
+ "inputs": f"{system_prompt}\nUser: {user_prompt}",
20
+ "options": {"use_cache": False, "wait_for_model": True}
21
+ }
22
+
23
+ resp = requests.post(API_URL, headers=HEADERS, json=payload, timeout=120)
24
+ if resp.status_code != 200:
25
+ return f"Error {resp.status_code}: {resp.text}"
26
+ data = resp.json()
27
+ # Inference API trả về list hoặc dict tùy model; xử lý an toàn:
28
+ if isinstance(data, dict) and data.get("error"):
29
+ return "Model error: " + data["error"]
30
+ # Một số model trả về [{"generated_text": "..."}]
31
+ if isinstance(data, list) and "generated_text" in data[0]:
32
+ return data[0]["generated_text"]
33
+ # Nếu trả về chuỗi hoặc khác
34
+ return str(data)
35
+
36
+ # UI
37
+ with gr.Blocks(title="NSFW Chat (gated)") as demo:
38
+ gr.Markdown("## Cảnh báo: nội dung nhạy cảm\nModel này có thể tạo nội dung NSFW. Vui lòng xác nhận tuổi >=18 và chấp nhận rủi ro.")
39
+ system = gr.Textbox(value="You are a helpful assistant.", label="System prompt (tuỳ chọn)")
40
+ user_in = gr.Textbox(label="User prompt", placeholder="Nhập lời nhắc...")
41
+ allow = gr.Checkbox(label="Tôi xác nhận tôi 18+ và đồng ý xem nội dung nhạy cảm", value=False)
42
+ out = gr.Textbox(label="Model output")
43
+ btn = gr.Button("Gửi")
44
+
45
+ btn.click(fn=generate, inputs=[system, user_in, allow], outputs=[out])
46
+
47
+ if __name__ == "__main__":
48
+ demo.launch()