Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,82 +1,89 @@
|
|
| 1 |
import os
|
| 2 |
import requests
|
| 3 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
-
#
|
|
|
|
|
|
|
| 6 |
HF_TOKEN = os.environ.get("HF_TOKEN")
|
| 7 |
HEADERS = {"Authorization": f"Bearer {HF_TOKEN}"}
|
| 8 |
|
| 9 |
-
#
|
| 10 |
-
#
|
|
|
|
|
|
|
| 11 |
PRIVATE_SPACE_ROOT = "https://jtdearmon--rag_dal_budget.hf.space"
|
| 12 |
|
|
|
|
|
|
|
|
|
|
| 13 |
def call_private_space(function_index, data_list):
|
| 14 |
"""
|
| 15 |
-
|
| 16 |
-
|
| 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
|
| 43 |
return json_resp["data"][0]
|
| 44 |
except Exception as e:
|
| 45 |
-
return f"Error: {e}"
|
| 46 |
|
| 47 |
-
#
|
| 48 |
-
#
|
| 49 |
-
#
|
| 50 |
-
#
|
| 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 |
-
|
| 68 |
-
#
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
)
|
| 76 |
-
|
| 77 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
|
|
| 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()
|