staghado commited on
Commit
111a99e
·
verified ·
1 Parent(s): cb4b97e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +59 -29
app.py CHANGED
@@ -1,5 +1,14 @@
 
 
 
 
1
  import gradio as gr
2
- from huggingface_hub import InferenceClient
 
 
 
 
 
3
 
4
 
5
  def respond(
@@ -9,40 +18,57 @@ 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
- """
44
- For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
45
- """
46
  chatbot = gr.ChatInterface(
47
  respond,
48
  type="messages",
@@ -60,11 +86,15 @@ 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
+ #!/usr/bin/env python3
2
+ import os
3
+ import json
4
+ import requests
5
  import gradio as gr
6
+
7
+ ENDPOINT = os.getenv("VLLM_ENDPOINT")
8
+ MODEL = os.getenv("VLLM_MODEL")
9
+
10
+ if not ENDPOINT or not MODEL:
11
+ raise ValueError("VLLM_ENDPOINT and VLLM_MODEL environment variables must be set")
12
 
13
 
14
  def respond(
 
18
  max_tokens,
19
  temperature,
20
  top_p,
 
21
  ):
22
  """
23
+ Send messages to vLLM endpoint and stream the response.
24
  """
 
 
25
  messages = [{"role": "system", "content": system_message}]
 
26
  messages.extend(history)
 
27
  messages.append({"role": "user", "content": message})
28
 
29
+ payload = {
30
+ "model": MODEL,
31
+ "messages": messages,
32
+ "max_tokens": max_tokens,
33
+ "temperature": temperature,
34
+ "top_p": top_p,
35
+ "stream": True
36
+ }
 
 
 
 
 
37
 
38
+ try:
39
+ response = requests.post(
40
+ ENDPOINT,
41
+ headers={"Content-Type": "application/json"},
42
+ data=json.dumps(payload),
43
+ stream=True
44
+ )
45
+ response.raise_for_status()
46
 
47
+ accumulated_response = ""
48
+
49
+ for line in response.iter_lines():
50
+ if line:
51
+ line = line.decode('utf-8')
52
+ if line.startswith('data: '):
53
+ line = line[6:] # Remove 'data: ' prefix
54
+
55
+ if line.strip() == '[DONE]':
56
+ break
57
+
58
+ try:
59
+ chunk = json.loads(line)
60
+ if 'choices' in chunk and len(chunk['choices']) > 0:
61
+ delta = chunk['choices'][0].get('delta', {})
62
+ content = delta.get('content', '')
63
+ if content:
64
+ accumulated_response += content
65
+ yield accumulated_response
66
+ except json.JSONDecodeError:
67
+ continue
68
+
69
+ except Exception as e:
70
+ yield f"Error: {str(e)}"
71
 
 
 
 
72
  chatbot = gr.ChatInterface(
73
  respond,
74
  type="messages",
 
86
  ],
87
  )
88
 
89
+ with gr.Blocks(title="vLLM Chatbot") as demo:
90
+ gr.Markdown("# 💬 Chat Interface")
91
+ gr.Markdown("""
92
+ Configure the endpoint via environment variables:
93
+ - `VLLM_ENDPOINT`: vLLM server URL
94
+ - `VLLM_MODEL`: Model name
95
+ """)
96
  chatbot.render()
97
 
98
 
99
  if __name__ == "__main__":
100
+ demo.launch()