samuelhim commited on
Commit
fc854bf
·
verified ·
1 Parent(s): 098d43a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +60 -22
app.py CHANGED
@@ -1,5 +1,10 @@
1
  import gradio as gr
2
- from huggingface_hub import InferenceClient
 
 
 
 
 
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
- For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
 
 
 
 
 
 
 
 
16
  """
17
- client = InferenceClient(token=hf_token.token, model="openai/gpt-oss-20b")
18
 
19
- messages = [{"role": "system", "content": system_message}]
20
 
21
- messages.extend(history)
 
 
 
 
 
 
 
 
 
 
22
 
23
- messages.append({"role": "user", "content": message})
 
 
 
 
 
 
 
24
 
25
- response = ""
26
 
27
- for message in client.chat_completion(
28
- messages,
29
- max_tokens=max_tokens,
30
- stream=True,
31
- temperature=temperature,
32
- top_p=top_p,
33
- ):
34
- choices = message.choices
35
- token = ""
36
- if len(choices) and choices[0].delta.content:
37
- token = choices[0].delta.content
 
 
 
 
 
 
 
 
 
 
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()