Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,128 +1,36 @@
|
|
| 1 |
-
|
| 2 |
-
import requests
|
| 3 |
-
import gradio as gr
|
| 4 |
-
|
| 5 |
-
#
|
| 6 |
-
# 1) Retrieve your Hugging Face token (with read or write access) for the private space
|
| 7 |
-
#
|
| 8 |
-
HF_TOKEN = os.getenv("HF_TOKEN", "").strip()
|
| 9 |
-
HEADERS = {"Authorization": f"Bearer {HF_TOKEN}"}
|
| 10 |
-
|
| 11 |
-
#
|
| 12 |
-
# 2) Private space endpoint
|
| 13 |
-
#
|
| 14 |
-
# Make sure this EXACT domain matches "Embed this Space" or "View App" from your private space.
|
| 15 |
-
# If your private space is "jtdearmon-rag-dal-budget",
|
| 16 |
-
# and the embed link is "https://jtdearmon-rag-dal-budget.hf.space",
|
| 17 |
-
# then use that domain here:
|
| 18 |
-
#
|
| 19 |
-
PRIVATE_SPACE_ROOT = "https://dearmonanalytics-rag-dallas-budget-private"
|
| 20 |
-
|
| 21 |
-
#
|
| 22 |
-
# 3) Utility to call a specific Gradio function in the private space.
|
| 23 |
-
# function_index=0 => /run/predict
|
| 24 |
-
# function_index=1 => /run/predict_1
|
| 25 |
-
#
|
| 26 |
-
def call_private_space(function_index, data_list):
|
| 27 |
-
"""
|
| 28 |
-
Sends a POST request to the private Space's Gradio endpoints:
|
| 29 |
-
- function_index=0 => /run/predict
|
| 30 |
-
- function_index=1 => /run/predict_1
|
| 31 |
-
data_list => the "data": [...] array matching that function’s inputs
|
| 32 |
-
"""
|
| 33 |
-
url = f"{PRIVATE_SPACE_ROOT}/run/predict"
|
| 34 |
-
if function_index > 0:
|
| 35 |
-
url += f"_{function_index}"
|
| 36 |
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
# If you'd like to enable normal SSL checks, set verify=True or remove it:
|
| 41 |
-
resp = requests.post(url, headers=HEADERS, json=payload, verify=False)
|
| 42 |
-
resp.raise_for_status() # Raises HTTPError on 4xx/5xx
|
| 43 |
-
json_resp = resp.json()
|
| 44 |
-
# Typically the function’s first return is in json_resp["data"][0]
|
| 45 |
-
return json_resp["data"][0]
|
| 46 |
-
except Exception as e:
|
| 47 |
-
return f"Error calling private space: {e}"
|
| 48 |
-
|
| 49 |
-
#
|
| 50 |
-
# 4) The two main interactive functions that mirror your private code:
|
| 51 |
-
#
|
| 52 |
-
def run_query(user_query, use_summary):
|
| 53 |
-
# This calls /run/predict (function_index=0)
|
| 54 |
-
return call_private_space(0, [user_query, use_summary])
|
| 55 |
-
|
| 56 |
-
def handle_feedback(user_query, answer, feedback_option):
|
| 57 |
-
# This calls /run/predict_1 (function_index=1)
|
| 58 |
-
return call_private_space(1, [user_query, answer, feedback_option])
|
| 59 |
|
| 60 |
-
#
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
# Top row
|
| 65 |
-
with gr.Row():
|
| 66 |
-
with gr.Column(scale=1, min_width=100):
|
| 67 |
-
gr.Image(
|
| 68 |
-
value="dal_logo.png",
|
| 69 |
-
show_label=False,
|
| 70 |
-
interactive=False,
|
| 71 |
-
width=80,
|
| 72 |
-
height=80
|
| 73 |
-
)
|
| 74 |
-
with gr.Column(scale=6):
|
| 75 |
-
gr.Markdown("## Dallas Budget RAG Model - Public Space")
|
| 76 |
-
gr.Markdown(
|
| 77 |
-
"Calls the private Space for actual processing."
|
| 78 |
-
)
|
| 79 |
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
use_summary_chk = gr.Checkbox(
|
| 86 |
-
value=False,
|
| 87 |
-
visible=False,
|
| 88 |
-
label="Use Graph Summaries?"
|
| 89 |
-
)
|
| 90 |
-
get_answer_btn = gr.Button("Get Answer")
|
| 91 |
|
| 92 |
-
#
|
| 93 |
-
|
| 94 |
|
| 95 |
-
#
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
value="⚖️",
|
| 99 |
-
label="Feedback"
|
| 100 |
-
)
|
| 101 |
-
feedback_btn = gr.Button("Submit Feedback")
|
| 102 |
-
feedback_result = gr.Markdown()
|
| 103 |
|
| 104 |
-
#
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
outputs=answer_output
|
| 109 |
-
)
|
| 110 |
-
feedback_btn.click(
|
| 111 |
-
fn=handle_feedback,
|
| 112 |
-
inputs=[user_query, answer_output, feedback_radio],
|
| 113 |
-
outputs=feedback_result
|
| 114 |
-
)
|
| 115 |
|
| 116 |
-
#
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
Built by
|
| 121 |
-
<a href="https://dearmonanalytics.com" target="_blank">Dearmon Analytics</a> |
|
| 122 |
-
<a href="https://huggingface.co/spaces" target="_blank">HF Spaces</a>
|
| 123 |
-
</div>
|
| 124 |
-
"""
|
| 125 |
-
)
|
| 126 |
|
| 127 |
if __name__ == "__main__":
|
| 128 |
-
|
|
|
|
| 1 |
+
# run_encrypted.py
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
+
import os
|
| 4 |
+
import base64
|
| 5 |
+
from cryptography.fernet import Fernet
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
+
# Replace this with the ciphertext from your local encryption step:
|
| 8 |
+
ENCRYPTED_CODE_B64 = """
|
| 9 |
+
<PASTE THAT MASSIVE BASE64 STRING HERE>
|
| 10 |
+
""".strip()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
+
def main():
|
| 13 |
+
# 1) Read the key from environment
|
| 14 |
+
secret_key = os.getenv("ENCRYPTION_KEY")
|
| 15 |
+
if not secret_key:
|
| 16 |
+
raise ValueError("Missing ENCRYPTION_KEY in environment variables!")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
+
# 2) Decode from Base64 -> bytes
|
| 19 |
+
ciphertext = base64.b64decode(ENCRYPTED_CODE_B64)
|
| 20 |
|
| 21 |
+
# 3) Decrypt with the provided key
|
| 22 |
+
fernet = Fernet(secret_key.encode())
|
| 23 |
+
decrypted_bytes = fernet.decrypt(ciphertext)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
+
# 4) Execute in memory
|
| 26 |
+
# If your code defines a Gradio demo, it should be launched here
|
| 27 |
+
exec_globals = {}
|
| 28 |
+
exec(decrypted_bytes, exec_globals)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
+
# If your decrypted script defines a 'demo' (gr.Blocks),
|
| 31 |
+
# it might automatically launch. If not, call:
|
| 32 |
+
if "demo" in exec_globals:
|
| 33 |
+
exec_globals["demo"].launch(server_name="0.0.0.0", server_port=7860)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
if __name__ == "__main__":
|
| 36 |
+
main()
|