Spaces:
Sleeping
Sleeping
Download app.py from samuelhim/AI_workshop: direct link, hf CLI and curl.
- Browser
- Download file 2.78 kB
-
https://huggingface.co/spaces/samuelhim/AI_workshop/resolve/main/app.py
- Command line
-
hf download hf://spaces/samuelhim/AI_workshop/app.py
-
curl -L -o app.py https://huggingface.co/spaces/samuelhim/AI_workshop/resolve/main/app.py
2.78 kB
| 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() |