K1SysAdmin commited on
Commit
f7727d2
·
verified ·
1 Parent(s): a8bd9fb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +79 -83
app.py CHANGED
@@ -1,38 +1,58 @@
1
  """
2
- Shared KGX3 API Endpoint. Includes a user responsibility disclaimer and agreement checkbox.
 
 
3
  """
4
 
5
  import gradio as gr
6
- import requests, json, pathlib, mimetypes
7
 
8
  # === CONFIGURATION ===
9
- API_ENDPOINT = "https://preprintwatch.com/wp-json/pw-kgx3/v1/submit"
10
- API_KEY = "sk-X1zY9wS3-vU4tQ0-rOjH5gK2fE7mN6"
11
-
12
-
13
- # === CORE LOGIC ===
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
  def call_kgx3(title, email, pdf_file, agreement):
15
- """Send uploaded PDF to the shared KGX3 endpoint on PreprintWatch if disclaimer accepted."""
16
  if not agreement:
17
  return "⚠️ You must accept the disclaimer before running KGX3.", ""
18
  if not pdf_file:
19
  return "Please select a PDF file.", ""
 
 
 
20
  pdf_path = pdf_file.name
21
  if not pdf_path.lower().endswith(".pdf"):
22
  return "Only PDF files are supported.", ""
23
 
24
- # Build the Hugging Face Space file URL
25
- space_root = gr.get_current_space() or "https://your-space-name.hf.space"
26
- file_name = pathlib.Path(pdf_path).name
27
- file_url = f"{space_root}/file={file_name}"
 
28
 
 
29
  headers = {"X-API-Key": API_KEY, "Content-Type": "application/json"}
30
- payload = {
31
- "title": title or pathlib.Path(pdf_path).stem,
32
- "pdf_url": file_url,
33
- }
34
- if email:
35
- payload["email"] = email
36
 
37
  try:
38
  resp = requests.post(API_ENDPOINT, headers=headers, json=payload, timeout=180)
@@ -45,104 +65,80 @@ def call_kgx3(title, email, pdf_file, agreement):
45
  return f"Request error: {e}", file_url
46
 
47
 
48
- # === STYLE (high-contrast futuristic) ===
49
  FUTURE_CSS = """
50
- /* === GLOBAL RESET === */
51
  html, body, .gradio-container {
52
- background-color: #000 !important;
53
- color: #00ff99 !important;
54
- font-family: 'Courier New', monospace !important;
55
  }
56
- * { color: #00ff99 !important; box-sizing: border-box; }
57
-
58
- /* === BOXES, PANELS, COLUMNS === */
59
- div, section, .gr-block, .gr-panel, .gr-box, .gr-column, .gr-row {
60
- background-color: #000 !important;
61
- border-color: #00ff99 !important;
62
  }
63
-
64
- /* === INPUTS, TEXTAREA, FILE UPLOAD === */
65
- input, textarea, select, .upload-box, .file-preview, .wrap-inner, .file-preview-container {
66
- background-color: #000 !important;
67
- color: #00ff99 !important;
68
- border: 1px solid #00ff99 !important;
69
- border-radius: 4px !important;
70
- padding: 6px 8px !important;
71
  }
72
- ::placeholder { color: #00ff99 !important; opacity: 0.6; }
73
-
74
- /* === BUTTONS === */
75
  button {
76
- background-color: #000 !important;
77
- color: #00ff99 !important;
78
- border: 2px solid #00ff99 !important;
79
- border-radius: 4px;
80
- text-transform: uppercase;
81
- font-weight: 700;
82
- letter-spacing: 0.5px;
83
- padding: 8px 16px !important;
84
  }
85
- button:hover { background-color: #00ff99 !important; color: #000 !important; }
86
-
87
- /* === CODE OUTPUT === */
88
- pre, code, .cm-editor, .cm-content, .cm-line {
89
- background-color: #000 !important;
90
- color: #00ff99 !important;
91
- border: 1px solid #00ff99 !important;
92
- border-radius: 4px;
93
  }
94
-
95
- /* === CHECKBOX === */
96
- input[type=checkbox] { accent-color: #00ff99 !important; transform: scale(1.1); }
97
-
98
- /* === SCROLLBARS === */
99
- ::-webkit-scrollbar { width: 8px; background: #000; }
100
- ::-webkit-scrollbar-thumb { background: #00ff99; border-radius: 4px; }
101
  """
102
 
103
- # === BUILD APP ===
104
- with gr.Blocks(css=FUTURE_CSS, title="Shared KGX3 API Endpoint") as demo:
 
105
  gr.Markdown("""
106
- # 🧠 **KGX3 Engine**
107
  Upload a scientific preprint PDF.
108
  The app sends it to the **PreprintWatch KGX3** API for automated paradigm-cycle analysis.
109
  """)
110
 
111
- # --- Disclaimer Section ---
112
  gr.Markdown("""
113
  ### ⚠️ Disclaimer
114
  By using this interface, you confirm that:
115
  - You are solely responsible and accountable for the files you upload.
116
  - All uploaded files are automatically deleted within **24 hours**.
117
- - System logs of KGX3 activity are retained securely for **up to 60 days** for service auditing.
118
  - You must ensure that any material you upload is lawful, non-confidential, and free of third-party rights restrictions.
119
  """)
120
-
121
  agreement = gr.Checkbox(
122
  label="I have read and accept full responsibility and accountability for the files I upload.",
123
  value=False
124
  )
125
 
126
- # --- Main layout ---
127
  with gr.Row():
128
  with gr.Column(scale=1):
129
  title_box = gr.Textbox(label="Preprint Title", placeholder="Enter or paste the paper title")
130
- email_box = gr.Textbox(label="Email (optional)", placeholder="you@example.com")
131
- file_box = gr.File(label="Upload PDF", file_types=[".pdf"], type="filepath")
132
- run_btn = gr.Button("Run KGX3", variant="primary")
133
  with gr.Column(scale=1):
134
  output_json = gr.Code(label="KGX3 Response", language="json")
135
- output_url = gr.Textbox(label="PDF Link Used", interactive=False)
136
 
137
- # --- Button logic (checkbox included) ---
138
- run_btn.click(
139
- call_kgx3,
140
- inputs=[title_box, email_box, file_box, agreement],
141
- outputs=[output_json, output_url]
142
- )
143
 
144
- gr.Markdown(
145
- "<small>Built on Gradio • Hosted on Hugging Face Spaces • Thomas Kuhn Foundation © 2025</small>"
146
- )
147
 
148
  demo.launch()
 
1
  """
2
+ KGX3 Research Analyzer Hugging Face Space
3
+ Uploads PDFs to the private WordPress /PDF endpoint (secured with X-API-Key),
4
+ retrieves a signed temporary link, and sends it to the PreprintWatch KGX3 API.
5
  """
6
 
7
  import gradio as gr
8
+ import requests, json, pathlib, mimetypes, re
9
 
10
  # === CONFIGURATION ===
11
+ WP_UPLOAD_URL = "https://preprintwatch.com/wp-json/kgx3/v1/upload" # your WP REST route
12
+ WP_UPLOAD_KEY = "lmsijtadti7tiDD6750xNdN" # shared secret
13
+ API_ENDPOINT = "https://preprintwatch.com/wp-json/pw-kgx3/v1/submit" # KGX3 proxy
14
+ API_KEY = "sk-X1zY9wS3-vU4tQ0-rOjH5gK2fE7mN6" # internal KGX3 key
15
+
16
+
17
+ # === HELPERS ===
18
+ def upload_private_pdf(pdf_path: str) -> str:
19
+ """Upload to WordPress secure uploader and return signed fetch URL."""
20
+ with open(pdf_path, "rb") as f:
21
+ r = requests.post(
22
+ WP_UPLOAD_URL,
23
+ headers={"X-API-Key": WP_UPLOAD_KEY},
24
+ files={"file": f},
25
+ timeout=60
26
+ )
27
+ r.raise_for_status()
28
+ data = r.json()
29
+ if "fetch_url" not in data:
30
+ raise RuntimeError(f"Upload failed: {data}")
31
+ return data["fetch_url"]
32
+
33
+
34
+ # === MAIN WORKFLOW ===
35
  def call_kgx3(title, email, pdf_file, agreement):
 
36
  if not agreement:
37
  return "⚠️ You must accept the disclaimer before running KGX3.", ""
38
  if not pdf_file:
39
  return "Please select a PDF file.", ""
40
+ if not email or not re.match(r"[^@]+@[^@]+\\.[^@]+", email):
41
+ return "A valid email address is required to run KGX3.", ""
42
+
43
  pdf_path = pdf_file.name
44
  if not pdf_path.lower().endswith(".pdf"):
45
  return "Only PDF files are supported.", ""
46
 
47
+ # --- Upload to WP and get temporary signed URL ---
48
+ try:
49
+ file_url = upload_private_pdf(pdf_path)
50
+ except Exception as e:
51
+ return f"Upload error: {e}", ""
52
 
53
+ # --- Forward to KGX3 ---
54
  headers = {"X-API-Key": API_KEY, "Content-Type": "application/json"}
55
+ payload = {"title": title or pathlib.Path(pdf_path).stem, "pdf_url": file_url, "email": email}
 
 
 
 
 
56
 
57
  try:
58
  resp = requests.post(API_ENDPOINT, headers=headers, json=payload, timeout=180)
 
65
  return f"Request error: {e}", file_url
66
 
67
 
68
+ # === STYLE (high-contrast Matrix aesthetic) ===
69
  FUTURE_CSS = """
 
70
  html, body, .gradio-container {
71
+ background:#000 !important;
72
+ color:#00ff99 !important;
73
+ font-family:'Courier New',monospace !important;
74
  }
75
+ * { box-sizing:border-box; color:#00ff99 !important; }
76
+ h1,h2,h3,label,.label {
77
+ font-weight:700 !important; text-transform:uppercase;
78
+ border-bottom:1px solid #00ff99; padding-bottom:4px; margin-bottom:8px;
 
 
79
  }
80
+ .gr-box,.gr-panel,.gr-column,.gr-row {
81
+ background:#000 !important; border:1px solid #00ff99 !important;
82
+ border-radius:6px; padding:10px;
83
+ }
84
+ input,textarea,.upload-box,.file-preview,.wrap-inner {
85
+ background:#000 !important; border:1px solid #00ff99 !important;
86
+ color:#00ff99 !important; border-radius:4px; padding:6px 8px !important;
 
87
  }
 
 
 
88
  button {
89
+ background:#000 !important; color:#00ff99 !important;
90
+ border:2px solid #00ff99 !important; border-radius:4px;
91
+ text-transform:uppercase; font-weight:700; padding:8px 16px !important;
 
 
 
 
 
92
  }
93
+ button:hover { background:#00ff99 !important; color:#000 !important; }
94
+ pre,code,.cm-editor,.cm-content,.cm-line {
95
+ background:#000 !important; color:#00ff99 !important;
96
+ border:1px solid #00ff99 !important; border-radius:4px; padding:10px;
 
 
 
 
97
  }
98
+ input[type=checkbox]{accent-color:#00ff99 !important;transform:scale(1.1);}
99
+ ::-webkit-scrollbar{width:8px;background:#000;}
100
+ ::-webkit-scrollbar-thumb{background:#00ff99;border-radius:4px;}
101
+ footer,.footer,small{color:#00ff99 !important;opacity:.8;text-align:center;}
 
 
 
102
  """
103
 
104
+
105
+ # === BUILD INTERFACE ===
106
+ with gr.Blocks(css=FUTURE_CSS, title="KGX3 Research Analyzer") as demo:
107
  gr.Markdown("""
108
+ # **KGX3 Engine**
109
  Upload a scientific preprint PDF.
110
  The app sends it to the **PreprintWatch KGX3** API for automated paradigm-cycle analysis.
111
  """)
112
 
113
+ # --- Disclaimer ---
114
  gr.Markdown("""
115
  ### ⚠️ Disclaimer
116
  By using this interface, you confirm that:
117
  - You are solely responsible and accountable for the files you upload.
118
  - All uploaded files are automatically deleted within **24 hours**.
119
+ - System logs of KGX3 activity are retained securely for **up to 60 days** for auditing.
120
  - You must ensure that any material you upload is lawful, non-confidential, and free of third-party rights restrictions.
121
  """)
 
122
  agreement = gr.Checkbox(
123
  label="I have read and accept full responsibility and accountability for the files I upload.",
124
  value=False
125
  )
126
 
127
+ # --- Layout ---
128
  with gr.Row():
129
  with gr.Column(scale=1):
130
  title_box = gr.Textbox(label="Preprint Title", placeholder="Enter or paste the paper title")
131
+ email_box = gr.Textbox(label="Email (required)", placeholder="you@example.com")
132
+ file_box = gr.File(label="Upload PDF", file_types=[".pdf"], type="filepath")
133
+ run_btn = gr.Button("Run KGX3", variant="primary")
134
  with gr.Column(scale=1):
135
  output_json = gr.Code(label="KGX3 Response", language="json")
136
+ output_url = gr.Textbox(label="PDF Link Used", interactive=False)
137
 
138
+ run_btn.click(call_kgx3,
139
+ inputs=[title_box, email_box, file_box, agreement],
140
+ outputs=[output_json, output_url])
 
 
 
141
 
142
+ gr.Markdown("<small>Built on Gradio • Hosted on Hugging Face Spaces • Thomas Kuhn Foundation © 2025</small>")
 
 
143
 
144
  demo.launch()