Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -9,24 +9,32 @@ from azure.ai.projects import AIProjectClient
|
|
| 9 |
from azure.identity import ClientSecretCredential
|
| 10 |
from azure.ai.agents.models import ListSortOrder
|
| 11 |
|
| 12 |
-
# Azure config
|
| 13 |
PROJECT_ENDPOINT = os.environ["PROJECT_ENDPOINT"]
|
| 14 |
AGENT_ID = os.environ["AGENT_ID"]
|
| 15 |
AZURE_TENANT_ID = os.environ["AZURE_TENANT_ID"]
|
| 16 |
AZURE_CLIENT_ID = os.environ["AZURE_CLIENT_ID"]
|
| 17 |
AZURE_CLIENT_SECRET = os.environ["AZURE_CLIENT_SECRET"]
|
| 18 |
|
| 19 |
-
# Azure client (singleton)
|
| 20 |
credential = ClientSecretCredential(
|
| 21 |
tenant_id=AZURE_TENANT_ID,
|
| 22 |
client_id=AZURE_CLIENT_ID,
|
| 23 |
client_secret=AZURE_CLIENT_SECRET,
|
| 24 |
)
|
| 25 |
project = AIProjectClient(credential=credential, endpoint=PROJECT_ENDPOINT)
|
| 26 |
-
agent = project.agents.get_agent(AGENT_ID)
|
| 27 |
|
|
|
|
|
|
|
| 28 |
|
| 29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
def respond(user_message: str, history: list, thread_state: dict | None):
|
| 31 |
"""
|
| 32 |
Gradio calls this for every user message.
|
|
@@ -44,6 +52,8 @@ def respond(user_message: str, history: list, thread_state: dict | None):
|
|
| 44 |
thread_id = thread_state["thread_id"]
|
| 45 |
|
| 46 |
try:
|
|
|
|
|
|
|
| 47 |
# Send user message
|
| 48 |
project.agents.messages.create(
|
| 49 |
thread_id=thread_id,
|
|
@@ -123,15 +133,13 @@ CSS = """
|
|
| 123 |
footer { display: none !important; }
|
| 124 |
"""
|
| 125 |
|
| 126 |
-
with gr.Blocks(
|
| 127 |
|
| 128 |
gr.Markdown(DESCRIPTION)
|
| 129 |
|
| 130 |
chatbot = gr.Chatbot(
|
| 131 |
label="Agent Ken",
|
| 132 |
height=500,
|
| 133 |
-
type="messages",
|
| 134 |
-
avatar_images=(None, "https://em-content.zobj.net/source/twitter/408/robot_1f916.png"),
|
| 135 |
show_copy_button=True,
|
| 136 |
)
|
| 137 |
|
|
@@ -157,7 +165,7 @@ with gr.Blocks(css=CSS, title="Agent Ken — PM Copilot", theme=gr.themes.Soft()
|
|
| 157 |
inputs=msg_input,
|
| 158 |
)
|
| 159 |
|
| 160 |
-
#
|
| 161 |
send_btn.click(
|
| 162 |
fn=respond,
|
| 163 |
inputs=[msg_input, chatbot, thread_state],
|
|
@@ -177,4 +185,4 @@ with gr.Blocks(css=CSS, title="Agent Ken — PM Copilot", theme=gr.themes.Soft()
|
|
| 177 |
|
| 178 |
# Launch
|
| 179 |
if __name__ == "__main__":
|
| 180 |
-
demo.launch(server_name="0.0.0.0", server_port=7860)
|
|
|
|
| 9 |
from azure.identity import ClientSecretCredential
|
| 10 |
from azure.ai.agents.models import ListSortOrder
|
| 11 |
|
| 12 |
+
# Azure config (from HF Space secrets)
|
| 13 |
PROJECT_ENDPOINT = os.environ["PROJECT_ENDPOINT"]
|
| 14 |
AGENT_ID = os.environ["AGENT_ID"]
|
| 15 |
AZURE_TENANT_ID = os.environ["AZURE_TENANT_ID"]
|
| 16 |
AZURE_CLIENT_ID = os.environ["AZURE_CLIENT_ID"]
|
| 17 |
AZURE_CLIENT_SECRET = os.environ["AZURE_CLIENT_SECRET"]
|
| 18 |
|
| 19 |
+
# Azure client (singleton)
|
| 20 |
credential = ClientSecretCredential(
|
| 21 |
tenant_id=AZURE_TENANT_ID,
|
| 22 |
client_id=AZURE_CLIENT_ID,
|
| 23 |
client_secret=AZURE_CLIENT_SECRET,
|
| 24 |
)
|
| 25 |
project = AIProjectClient(credential=credential, endpoint=PROJECT_ENDPOINT)
|
|
|
|
| 26 |
|
| 27 |
+
# Lazy agent fetch (so app boots even if Azure is slow)
|
| 28 |
+
_agent = None
|
| 29 |
|
| 30 |
+
def get_agent():
|
| 31 |
+
global _agent
|
| 32 |
+
if _agent is None:
|
| 33 |
+
_agent = project.agents.get_agent(AGENT_ID)
|
| 34 |
+
return _agent
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
# Chat logic
|
| 38 |
def respond(user_message: str, history: list, thread_state: dict | None):
|
| 39 |
"""
|
| 40 |
Gradio calls this for every user message.
|
|
|
|
| 52 |
thread_id = thread_state["thread_id"]
|
| 53 |
|
| 54 |
try:
|
| 55 |
+
agent = get_agent()
|
| 56 |
+
|
| 57 |
# Send user message
|
| 58 |
project.agents.messages.create(
|
| 59 |
thread_id=thread_id,
|
|
|
|
| 133 |
footer { display: none !important; }
|
| 134 |
"""
|
| 135 |
|
| 136 |
+
with gr.Blocks(title="Agent Ken — PM Copilot") as demo:
|
| 137 |
|
| 138 |
gr.Markdown(DESCRIPTION)
|
| 139 |
|
| 140 |
chatbot = gr.Chatbot(
|
| 141 |
label="Agent Ken",
|
| 142 |
height=500,
|
|
|
|
|
|
|
| 143 |
show_copy_button=True,
|
| 144 |
)
|
| 145 |
|
|
|
|
| 165 |
inputs=msg_input,
|
| 166 |
)
|
| 167 |
|
| 168 |
+
# Event bindings
|
| 169 |
send_btn.click(
|
| 170 |
fn=respond,
|
| 171 |
inputs=[msg_input, chatbot, thread_state],
|
|
|
|
| 185 |
|
| 186 |
# Launch
|
| 187 |
if __name__ == "__main__":
|
| 188 |
+
demo.launch(server_name="0.0.0.0", server_port=7860, css=CSS, theme=gr.themes.Soft())
|