Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,21 +3,25 @@ import requests
|
|
| 3 |
import gradio as gr
|
| 4 |
|
| 5 |
#
|
| 6 |
-
# 1) Retrieve your Hugging Face token (with read 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 |
#
|
| 15 |
PRIVATE_SPACE_ROOT = "https://jtdearmon-rag-dal-budget.hf.space"
|
| 16 |
|
| 17 |
#
|
| 18 |
# 3) Utility to call a specific Gradio function in the private space.
|
| 19 |
-
#
|
| 20 |
-
#
|
| 21 |
#
|
| 22 |
def call_private_space(function_index, data_list):
|
| 23 |
"""
|
|
@@ -26,42 +30,38 @@ def call_private_space(function_index, data_list):
|
|
| 26 |
- function_index=1 => /run/predict_1
|
| 27 |
data_list => the "data": [...] array matching that function’s inputs
|
| 28 |
"""
|
| 29 |
-
# Base endpoint
|
| 30 |
url = f"{PRIVATE_SPACE_ROOT}/run/predict"
|
| 31 |
-
# If we specify function_index>0, call /run/predict_1, etc.
|
| 32 |
if function_index > 0:
|
| 33 |
url += f"_{function_index}"
|
| 34 |
|
| 35 |
payload = {"data": data_list}
|
| 36 |
try:
|
| 37 |
-
# If you
|
| 38 |
-
|
|
|
|
| 39 |
resp.raise_for_status() # Raises HTTPError on 4xx/5xx
|
| 40 |
json_resp = resp.json()
|
| 41 |
-
# Typically the function’s first return is json_resp["data"][0]
|
| 42 |
return json_resp["data"][0]
|
| 43 |
except Exception as e:
|
| 44 |
return f"Error calling private space: {e}"
|
| 45 |
|
| 46 |
#
|
| 47 |
-
# 4) The two main interactive functions
|
| 48 |
-
# - run_query(user_query, use_summary)
|
| 49 |
-
# - handle_feedback(user_query, answer, feedback_option)
|
| 50 |
#
|
| 51 |
def run_query(user_query, use_summary):
|
| 52 |
-
# This calls /run/predict (function_index=0
|
| 53 |
return call_private_space(0, [user_query, use_summary])
|
| 54 |
|
| 55 |
def handle_feedback(user_query, answer, feedback_option):
|
| 56 |
-
# This calls /run/predict_1 (function_index=1
|
| 57 |
return call_private_space(1, [user_query, answer, feedback_option])
|
| 58 |
|
| 59 |
#
|
| 60 |
-
# 5) Build
|
| 61 |
-
# now loading dal_logo.png locally from this public repo.
|
| 62 |
#
|
| 63 |
with gr.Blocks() as demo:
|
| 64 |
-
# Top row
|
| 65 |
with gr.Row():
|
| 66 |
with gr.Column(scale=1, min_width=100):
|
| 67 |
gr.Image(
|
|
@@ -72,13 +72,11 @@ with gr.Blocks() as demo:
|
|
| 72 |
height=80
|
| 73 |
)
|
| 74 |
with gr.Column(scale=6):
|
| 75 |
-
gr.Markdown("## Dallas Budget RAG Model")
|
| 76 |
gr.Markdown(
|
| 77 |
-
"
|
| 78 |
-
"Only the last 5 queries are kept in the conversation."
|
| 79 |
)
|
| 80 |
|
| 81 |
-
# User query, hidden checkbox, and Get Answer button
|
| 82 |
user_query = gr.Textbox(
|
| 83 |
label="Your Query",
|
| 84 |
lines=1,
|
|
@@ -94,7 +92,7 @@ with gr.Blocks() as demo:
|
|
| 94 |
# Output area
|
| 95 |
answer_output = gr.Markdown(label="AI Answer")
|
| 96 |
|
| 97 |
-
# Feedback
|
| 98 |
feedback_radio = gr.Radio(
|
| 99 |
choices=["👍", "⚖️", "👎"],
|
| 100 |
value="⚖️",
|
|
@@ -103,7 +101,7 @@ with gr.Blocks() as demo:
|
|
| 103 |
feedback_btn = gr.Button("Submit Feedback")
|
| 104 |
feedback_result = gr.Markdown()
|
| 105 |
|
| 106 |
-
# Wire the buttons
|
| 107 |
get_answer_btn.click(
|
| 108 |
fn=run_query,
|
| 109 |
inputs=[user_query, use_summary_chk],
|
|
@@ -119,15 +117,12 @@ with gr.Blocks() as demo:
|
|
| 119 |
gr.HTML(
|
| 120 |
"""
|
| 121 |
<div style="text-align: center; margin-top: 20px;">
|
| 122 |
-
Built with
|
| 123 |
-
<a href="https://gradio.app" target="_blank">Gradio</a>
|
| 124 |
-
|
|
| 125 |
Built by
|
| 126 |
-
<a href="https://dearmonanalytics.com" target="_blank">Dearmon Analytics</a>
|
|
|
|
| 127 |
</div>
|
| 128 |
"""
|
| 129 |
)
|
| 130 |
|
| 131 |
-
# Launch the public interface
|
| 132 |
if __name__ == "__main__":
|
| 133 |
demo.launch()
|
|
|
|
| 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://jtdearmon-rag-dal-budget.hf.space"
|
| 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 |
"""
|
|
|
|
| 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 |
payload = {"data": data_list}
|
| 38 |
try:
|
| 39 |
+
# If you see SSL errors, keep verify=False.
|
| 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 |
+
# 5) Build a UI to demonstrate the calls
|
|
|
|
| 62 |
#
|
| 63 |
with gr.Blocks() as demo:
|
| 64 |
+
# Top row
|
| 65 |
with gr.Row():
|
| 66 |
with gr.Column(scale=1, min_width=100):
|
| 67 |
gr.Image(
|
|
|
|
| 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 |
user_query = gr.Textbox(
|
| 81 |
label="Your Query",
|
| 82 |
lines=1,
|
|
|
|
| 92 |
# Output area
|
| 93 |
answer_output = gr.Markdown(label="AI Answer")
|
| 94 |
|
| 95 |
+
# Feedback
|
| 96 |
feedback_radio = gr.Radio(
|
| 97 |
choices=["👍", "⚖️", "👎"],
|
| 98 |
value="⚖️",
|
|
|
|
| 101 |
feedback_btn = gr.Button("Submit Feedback")
|
| 102 |
feedback_result = gr.Markdown()
|
| 103 |
|
| 104 |
+
# Wire the buttons
|
| 105 |
get_answer_btn.click(
|
| 106 |
fn=run_query,
|
| 107 |
inputs=[user_query, use_summary_chk],
|
|
|
|
| 117 |
gr.HTML(
|
| 118 |
"""
|
| 119 |
<div style="text-align: center; margin-top: 20px;">
|
|
|
|
|
|
|
|
|
|
| 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 |
demo.launch()
|