jtdearmon commited on
Commit
88821ba
·
verified ·
1 Parent(s): b7efb69

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +104 -40
app.py CHANGED
@@ -2,47 +2,111 @@ import os
2
  import requests
3
  import gradio as gr
4
 
5
- # 1. Retrieve the token from the environment
6
  HF_TOKEN = os.environ.get("HF_TOKEN")
 
7
 
8
- # 2. Function that calls the private Space
9
- def call_private_space(user_input):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
  """
11
- Sends a POST request to the private Space's Gradio endpoint.
12
- Adjust the URL or payload if your private Space uses something different.
 
 
 
 
13
  """
14
- # The private Space's URL - note 'jtdearmon--rag_dal_budget' matches the subdomain pattern
15
- private_url = "https://jtdearmon--rag_dal_budget.hf.space/run/predict"
16
-
17
- headers = {
18
- "Authorization": f"Bearer {HF_TOKEN}"
19
- }
20
-
21
- # Typical Gradio JSON payload for a single text input
22
- payload = {
23
- "data": [user_input]
24
- }
25
-
26
- # Send request
27
- response = requests.post(private_url, headers=headers, json=payload)
28
- response_data = response.json()
29
-
30
- # This depends on how your Gradio app in the private space returns output
31
- # Usually it might be response_data["data"][0] if it returns one text output
32
- result = response_data["data"][0]
33
-
34
- return result
35
-
36
-
37
- # 3. Create a Gradio interface
38
- interface = gr.Interface(
39
- fn=call_private_space,
40
- inputs="text",
41
- outputs="text",
42
- title="Public Demo for rag_dal_budget (Private)",
43
- description="This public Space calls the private `rag_dal_budget` behind the scenes."
44
- )
45
-
46
- # 4. Launch the interface
47
- if __name__ == "__main__":
48
- interface.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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()