Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,47 +2,111 @@ import os
|
|
| 2 |
import requests
|
| 3 |
import gradio as gr
|
| 4 |
|
| 5 |
-
# 1
|
| 6 |
HF_TOKEN = os.environ.get("HF_TOKEN")
|
|
|
|
| 7 |
|
| 8 |
-
# 2
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
"""
|
| 11 |
-
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
"""
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
"
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import requests
|
| 3 |
import gradio as gr
|
| 4 |
|
| 5 |
+
# 1) Grab your personal (read-access) token from HF Secrets
|
| 6 |
HF_TOKEN = os.environ.get("HF_TOKEN")
|
| 7 |
+
HEADERS = {"Authorization": f"Bearer {HF_TOKEN}"}
|
| 8 |
|
| 9 |
+
# 2) Root URL for your PRIVATE space (jtdearmon/rag_dal_budget)
|
| 10 |
+
# Note how the subdomain is: jtdearmon--rag_dal_budget.hf.space
|
| 11 |
+
PRIVATE_SPACE_ROOT = "https://jtdearmon--rag_dal_budget.hf.space"
|
| 12 |
+
|
| 13 |
+
def call_private_space(function_index, data_list):
|
| 14 |
+
"""
|
| 15 |
+
A helper that sends a POST request to one of the Gradio endpoints
|
| 16 |
+
in the private rag_dal_budget space.
|
| 17 |
+
|
| 18 |
+
By default:
|
| 19 |
+
- The 1st .click() function is at /run/predict
|
| 20 |
+
- The 2nd .click() function is at /run/predict_1
|
| 21 |
+
- 3rd at /run/predict_2
|
| 22 |
+
etc.
|
| 23 |
+
|
| 24 |
+
Inputs:
|
| 25 |
+
function_index: 0 for run_query, 1 for handle_feedback, etc.
|
| 26 |
+
data_list: The 'data' array to send, matching the function’s inputs.
|
| 27 |
+
|
| 28 |
+
Returns:
|
| 29 |
+
The first output in the JSON payload (result["data"][0]) or an error message.
|
| 30 |
+
"""
|
| 31 |
+
# Build the correct endpoint: /run/predict, /run/predict_1, /run/predict_2...
|
| 32 |
+
url = f"{PRIVATE_SPACE_ROOT}/run/predict"
|
| 33 |
+
if function_index > 0:
|
| 34 |
+
url += f"_{function_index}"
|
| 35 |
+
|
| 36 |
+
# Gradio expects a JSON body of the form {"data": [...]}
|
| 37 |
+
payload = {"data": data_list}
|
| 38 |
+
try:
|
| 39 |
+
resp = requests.post(url, headers=HEADERS, json=payload)
|
| 40 |
+
resp.raise_for_status()
|
| 41 |
+
json_resp = resp.json()
|
| 42 |
+
# Typically, the first (and often only) return value is in json_resp["data"][0]
|
| 43 |
+
return json_resp["data"][0]
|
| 44 |
+
except Exception as e:
|
| 45 |
+
return f"Error: {e}"
|
| 46 |
+
|
| 47 |
+
# -------------------------------------------------------------------------
|
| 48 |
+
# Correspond to the private Space’s two main interactive functions:
|
| 49 |
+
# 1) run_query(user_query, use_summary)
|
| 50 |
+
# 2) handle_feedback(user_query, answer, feedback_option)
|
| 51 |
+
# -------------------------------------------------------------------------
|
| 52 |
+
def run_query(user_query, use_summary):
|
| 53 |
+
"""
|
| 54 |
+
Calls the first Gradio function (index 0) in the private space,
|
| 55 |
+
which is the 'run_query' pipeline.
|
| 56 |
"""
|
| 57 |
+
return call_private_space(0, [user_query, use_summary])
|
| 58 |
+
|
| 59 |
+
def handle_feedback(user_query, answer, feedback_option):
|
| 60 |
+
"""
|
| 61 |
+
Calls the second Gradio function (index 1) in the private space,
|
| 62 |
+
which is 'handle_feedback'.
|
| 63 |
"""
|
| 64 |
+
return call_private_space(1, [user_query, answer, feedback_option])
|
| 65 |
+
|
| 66 |
+
# -------------------------------------------------------------------------
|
| 67 |
+
# Build a minimal Gradio UI that calls the above 2 endpoints
|
| 68 |
+
# -------------------------------------------------------------------------
|
| 69 |
+
with gr.Blocks() as demo:
|
| 70 |
+
gr.Markdown("## Public Interface → Private `rag_dal_budget`")
|
| 71 |
+
|
| 72 |
+
gr.Markdown(
|
| 73 |
+
"This public Space relays requests to a **private** Gradio app "
|
| 74 |
+
"(`rag_dal_budget`) so the underlying code is hidden."
|
| 75 |
+
)
|
| 76 |
+
|
| 77 |
+
# Inputs for the first function
|
| 78 |
+
user_query = gr.Textbox(
|
| 79 |
+
label="Your Query",
|
| 80 |
+
placeholder="Ask about the Dallas budget..."
|
| 81 |
+
)
|
| 82 |
+
use_summary_chk = gr.Checkbox(
|
| 83 |
+
value=False,
|
| 84 |
+
label="Use Graph Summaries?"
|
| 85 |
+
)
|
| 86 |
+
get_answer_btn = gr.Button("Get Answer")
|
| 87 |
+
|
| 88 |
+
# Output from the first function
|
| 89 |
+
answer_output = gr.Markdown(label="AI Answer")
|
| 90 |
+
|
| 91 |
+
# Feedback components for the second function
|
| 92 |
+
feedback_radio = gr.Radio(
|
| 93 |
+
choices=["👍", "⚖️", "👎"],
|
| 94 |
+
value="⚖️",
|
| 95 |
+
label="Feedback"
|
| 96 |
+
)
|
| 97 |
+
feedback_btn = gr.Button("Submit Feedback")
|
| 98 |
+
feedback_result = gr.Markdown()
|
| 99 |
+
|
| 100 |
+
# Wire up the .click() events
|
| 101 |
+
get_answer_btn.click(
|
| 102 |
+
fn=run_query,
|
| 103 |
+
inputs=[user_query, use_summary_chk],
|
| 104 |
+
outputs=answer_output
|
| 105 |
+
)
|
| 106 |
+
feedback_btn.click(
|
| 107 |
+
fn=handle_feedback,
|
| 108 |
+
inputs=[user_query, answer_output, feedback_radio],
|
| 109 |
+
outputs=feedback_result
|
| 110 |
+
)
|
| 111 |
+
|
| 112 |
+
demo.launch()
|