Arathi5 commited on
Commit
b537865
·
verified ·
1 Parent(s): 17c4afb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -24
app.py CHANGED
@@ -1,5 +1,7 @@
1
  import gradio as gr
2
- from huggingface_hub import InferenceClient
 
 
3
 
4
 
5
  def respond(
@@ -9,35 +11,58 @@ 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
  """
@@ -67,4 +92,4 @@ with gr.Blocks() as demo:
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
+ WEBHOOK_URL = "PUT YOUR URL HERE"
5
 
6
 
7
  def respond(
 
11
  max_tokens,
12
  temperature,
13
  top_p,
14
+ hf_token: gr.OAuthToken, # still here so LoginButton works, but not used for auth downstream
15
  ):
16
  """
17
+ This function sends the chat state to an n8n webhook and streams
18
+ back the response text.
 
19
 
20
+ Expected n8n behaviour:
21
+ - Receive JSON with fields: message, history, system_message, max_tokens, temperature, top_p, user
22
+ - Return either:
23
+ { "reply": "<assistant reply text>" }
24
+ or any JSON with a text-like field, or plain text.
25
+ """
26
 
27
+ payload = {
28
+ "message": message,
29
+ "history": history,
30
+ "system_message": system_message,
31
+ "max_tokens": max_tokens,
32
+ "temperature": temperature,
33
+ "top_p": top_p,
34
+ # optional: pass along HF user info if you want to use it in n8n
35
+ "user": getattr(hf_token, "username", None) if hf_token is not None else None,
36
+ }
37
 
38
+ try:
39
+ response = requests.post(WEBHOOK_URL, json=payload, timeout=120)
40
+ response.raise_for_status()
41
+ except Exception as e:
42
+ error_text = f"Error contacting backend: {e}"
43
+ # stream the error as a single message
44
+ yield error_text
45
+ return
46
 
47
+ # Try to parse JSON, but fall back to raw text if needed
48
+ try:
49
+ data = response.json()
50
+ reply = (
51
+ data.get("reply")
52
+ or data.get("message")
53
+ or data.get("content")
54
+ or data.get("text")
55
+ )
56
+ if reply is None:
57
+ reply = str(data)
58
+ except Exception:
59
+ reply = response.text
60
 
61
+ # Stream reply token-by-token (character-by-character) to Gradio
62
+ accumulated = ""
63
+ for ch in reply:
64
+ accumulated += ch
65
+ yield accumulated
66
 
67
 
68
  """
 
92
 
93
 
94
  if __name__ == "__main__":
95
+ demo.queue().launch()