Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,5 +1,10 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
|
| 5 |
def respond(
|
|
@@ -9,35 +14,65 @@ def respond(
|
|
| 9 |
max_tokens,
|
| 10 |
temperature,
|
| 11 |
top_p,
|
| 12 |
-
hf_token: gr.OAuthToken,
|
| 13 |
):
|
| 14 |
"""
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
"""
|
| 17 |
-
client = InferenceClient(token=hf_token.token, model="openai/gpt-oss-20b")
|
| 18 |
|
| 19 |
-
messages = [{"role": "system", "content": system_message}]
|
| 20 |
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
-
response = ""
|
| 26 |
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
if
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
-
response += token
|
| 40 |
-
yield response
|
| 41 |
|
| 42 |
|
| 43 |
"""
|
|
@@ -60,11 +95,14 @@ chatbot = gr.ChatInterface(
|
|
| 60 |
],
|
| 61 |
)
|
| 62 |
|
|
|
|
| 63 |
with gr.Blocks() as demo:
|
| 64 |
with gr.Sidebar():
|
| 65 |
gr.LoginButton()
|
| 66 |
chatbot.render()
|
| 67 |
|
| 68 |
|
|
|
|
|
|
|
| 69 |
if __name__ == "__main__":
|
| 70 |
-
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import requests # NEW: for calling the n8n webhook
|
| 3 |
+
|
| 4 |
+
|
| 5 |
+
WEBHOOK_URL = "PUT YOUR URL HERE"
|
| 6 |
+
|
| 7 |
+
|
| 8 |
|
| 9 |
|
| 10 |
def respond(
|
|
|
|
| 14 |
max_tokens,
|
| 15 |
temperature,
|
| 16 |
top_p,
|
| 17 |
+
hf_token: gr.OAuthToken, # still here so LoginButton works, but not used for auth downstream
|
| 18 |
):
|
| 19 |
"""
|
| 20 |
+
This function sends the chat state to an n8n webhook and streams
|
| 21 |
+
back the response text.
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
Expected n8n behaviour:
|
| 25 |
+
- Receive JSON with fields: message, history, system_message, max_tokens, temperature, top_p, user
|
| 26 |
+
- Return either:
|
| 27 |
+
{ "reply": "<assistant reply text>" }
|
| 28 |
+
or any JSON with a text-like field, or plain text.
|
| 29 |
"""
|
|
|
|
| 30 |
|
|
|
|
| 31 |
|
| 32 |
+
payload = {
|
| 33 |
+
"message": message,
|
| 34 |
+
"history": history,
|
| 35 |
+
"system_message": system_message,
|
| 36 |
+
"max_tokens": max_tokens,
|
| 37 |
+
"temperature": temperature,
|
| 38 |
+
"top_p": top_p,
|
| 39 |
+
# optional: pass along HF user info if you want to use it in n8n
|
| 40 |
+
"user": getattr(hf_token, "username", None) if hf_token is not None else None,
|
| 41 |
+
}
|
| 42 |
+
|
| 43 |
|
| 44 |
+
try:
|
| 45 |
+
response = requests.post(WEBHOOK_URL, json=payload, timeout=120)
|
| 46 |
+
response.raise_for_status()
|
| 47 |
+
except Exception as e:
|
| 48 |
+
error_text = f"Error contacting backend: {e}"
|
| 49 |
+
# stream the error as a single message
|
| 50 |
+
yield error_text
|
| 51 |
+
return
|
| 52 |
|
|
|
|
| 53 |
|
| 54 |
+
# Try to parse JSON, but fall back to raw text if needed
|
| 55 |
+
try:
|
| 56 |
+
data = response.json()
|
| 57 |
+
reply = (
|
| 58 |
+
data.get("reply")
|
| 59 |
+
or data.get("message")
|
| 60 |
+
or data.get("content")
|
| 61 |
+
or data.get("text")
|
| 62 |
+
)
|
| 63 |
+
if reply is None:
|
| 64 |
+
reply = str(data)
|
| 65 |
+
except Exception:
|
| 66 |
+
reply = response.text
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
# Stream reply token-by-token (character-by-character) to Gradio
|
| 70 |
+
accumulated = ""
|
| 71 |
+
for ch in reply:
|
| 72 |
+
accumulated += ch
|
| 73 |
+
yield accumulated
|
| 74 |
+
|
| 75 |
|
|
|
|
|
|
|
| 76 |
|
| 77 |
|
| 78 |
"""
|
|
|
|
| 95 |
],
|
| 96 |
)
|
| 97 |
|
| 98 |
+
|
| 99 |
with gr.Blocks() as demo:
|
| 100 |
with gr.Sidebar():
|
| 101 |
gr.LoginButton()
|
| 102 |
chatbot.render()
|
| 103 |
|
| 104 |
|
| 105 |
+
|
| 106 |
+
|
| 107 |
if __name__ == "__main__":
|
| 108 |
+
demo.queue().launch()
|