Spaces:
Sleeping
Sleeping
File size: 2,776 Bytes
8144db8 fc854bf 475d212 fc854bf 8144db8 fc854bf 8144db8 fc854bf 8144db8 fc854bf 8144db8 fc854bf 8144db8 fc854bf 8144db8 fc854bf 8144db8 fc854bf 8144db8 fc854bf | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 | import gradio as gr
import requests # NEW: for calling the n8n webhook
WEBHOOK_URL = "https://samuelhim.app.n8n.cloud/webhook/12be7b71-1add-428f-be0c-e31bb84aa556"
def respond(
message,
history: list[dict[str, str]],
system_message,
max_tokens,
temperature,
top_p,
hf_token: gr.OAuthToken, # still here so LoginButton works, but not used for auth downstream
):
"""
This function sends the chat state to an n8n webhook and streams
back the response text.
Expected n8n behaviour:
- Receive JSON with fields: message, history, system_message, max_tokens, temperature, top_p, user
- Return either:
{ "reply": "<assistant reply text>" }
or any JSON with a text-like field, or plain text.
"""
payload = {
"message": message,
"history": history,
"system_message": system_message,
"max_tokens": max_tokens,
"temperature": temperature,
"top_p": top_p,
# optional: pass along HF user info if you want to use it in n8n
"user": getattr(hf_token, "username", None) if hf_token is not None else None,
}
try:
response = requests.post(WEBHOOK_URL, json=payload, timeout=120)
response.raise_for_status()
except Exception as e:
error_text = f"Error contacting backend: {e}"
# stream the error as a single message
yield error_text
return
# Try to parse JSON, but fall back to raw text if needed
try:
data = response.json()
reply = (
data.get("reply")
or data.get("message")
or data.get("content")
or data.get("text")
)
if reply is None:
reply = str(data)
except Exception:
reply = response.text
# Stream reply token-by-token (character-by-character) to Gradio
accumulated = ""
for ch in reply:
accumulated += ch
yield accumulated
"""
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
"""
chatbot = gr.ChatInterface(
respond,
type="messages",
additional_inputs=[
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
gr.Slider(
minimum=0.1,
maximum=1.0,
value=0.95,
step=0.05,
label="Top-p (nucleus sampling)",
),
],
)
with gr.Blocks() as demo:
with gr.Sidebar():
gr.LoginButton()
chatbot.render()
if __name__ == "__main__":
demo.queue().launch() |