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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +73 -51
app.py CHANGED
@@ -1,82 +1,89 @@
1
  import os
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(
@@ -84,11 +91,9 @@ with gr.Blocks() as demo:
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="⚖️",
@@ -97,7 +102,7 @@ with gr.Blocks() as demo:
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],
@@ -109,4 +114,21 @@ with gr.Blocks() as demo:
109
  outputs=feedback_result
110
  )
111
 
112
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import os
2
  import requests
3
  import gradio as gr
4
+ import base64
5
+ import io
6
+ from PIL import Image
7
 
8
+ #
9
+ # 1) Retrieve your Hugging Face token (with read access) for the private space
10
+ #
11
  HF_TOKEN = os.environ.get("HF_TOKEN")
12
  HEADERS = {"Authorization": f"Bearer {HF_TOKEN}"}
13
 
14
+ #
15
+ # 2) Private space endpoint:
16
+ # The subdomain is typically "<username>--<space_name>.hf.space"
17
+ #
18
  PRIVATE_SPACE_ROOT = "https://jtdearmon--rag_dal_budget.hf.space"
19
 
20
+ #
21
+ # 3) Utility to call a specific Gradio function index (0, 1, 2...) in the private space.
22
+ #
23
  def call_private_space(function_index, data_list):
24
  """
25
+ Sends a POST request to the private Space's Gradio endpoint:
26
+ - function_index=0 => /run/predict
27
+ - function_index=1 => /run/predict_1
 
 
 
 
28
  etc.
29
+ data_list => the "data": [...] array matching that function’s inputs
 
 
 
 
 
 
30
  """
 
31
  url = f"{PRIVATE_SPACE_ROOT}/run/predict"
32
  if function_index > 0:
33
  url += f"_{function_index}"
34
 
 
35
  payload = {"data": data_list}
36
  try:
37
  resp = requests.post(url, headers=HEADERS, json=payload)
38
  resp.raise_for_status()
39
  json_resp = resp.json()
40
+ # Typically the function’s first return is json_resp["data"][0]
41
  return json_resp["data"][0]
42
  except Exception as e:
43
+ return f"Error calling private space: {e}"
44
 
45
+ #
46
+ # 4) The two main interactive functions from your private code:
47
+ # - run_query(user_query, use_summary)
48
+ # - handle_feedback(user_query, answer, feedback_option)
49
+ #
50
  def run_query(user_query, use_summary):
 
 
 
 
51
  return call_private_space(0, [user_query, use_summary])
52
 
53
  def handle_feedback(user_query, answer, feedback_option):
 
 
 
 
54
  return call_private_space(1, [user_query, answer, feedback_option])
55
 
56
+ #
57
+
58
+ #
59
+ # 6) Build the UI *layout* to mirror your private space.
60
+ #
61
+ demo = gr.Blocks()
62
+
63
+ with demo:
64
+ # A row with the logo on the left, and a title on the right
65
+ with gr.Row():
66
+ with gr.Column(scale=1, min_width=100):
67
+ if DAL_LOGO_IMG:
68
+ gr.Image(
69
+ value=DAL_LOGO_IMG,
70
+ show_label=False,
71
+ interactive=False,
72
+ width=80,
73
+ height=80
74
+ )
75
+ with gr.Column(scale=6):
76
+ gr.Markdown("## Dallas RAG + Graph Demo")
77
+ gr.Markdown(
78
+ "Loads node/edge data from a Dallas budget network. "
79
+ "You can **toggle** whether the subgraph is summarized via OpenAI "
80
+ "or used 'as is'. Only the last 5 queries are kept in the conversation."
81
+ )
82
+
83
+ # User input row
84
  user_query = gr.Textbox(
85
  label="Your Query",
86
+ lines=1,
87
  placeholder="Ask about the Dallas budget..."
88
  )
89
  use_summary_chk = gr.Checkbox(
 
91
  label="Use Graph Summaries?"
92
  )
93
  get_answer_btn = gr.Button("Get Answer")
 
 
94
  answer_output = gr.Markdown(label="AI Answer")
95
 
96
+ # Feedback row
97
  feedback_radio = gr.Radio(
98
  choices=["👍", "⚖️", "👎"],
99
  value="⚖️",
 
102
  feedback_btn = gr.Button("Submit Feedback")
103
  feedback_result = gr.Markdown()
104
 
105
+ # Wire up the .click() events to the private space calls
106
  get_answer_btn.click(
107
  fn=run_query,
108
  inputs=[user_query, use_summary_chk],
 
114
  outputs=feedback_result
115
  )
116
 
117
+ # Footer, same as private space
118
+ gr.HTML(
119
+ """
120
+ <div style="text-align: center; margin-top: 20px;">
121
+ Built with
122
+ <a href="https://gradio.app" target="_blank">Gradio</a>
123
+ &nbsp;|&nbsp;
124
+ Built by
125
+ <a href="https://dearmonanalytics.com" target="_blank">Dearmon Analytics</a>
126
+ </div>
127
+ """
128
+ )
129
+
130
+ #
131
+ # 7) Launch
132
+ #
133
+ if __name__ == "__main__":
134
+ demo.launch()