staghado commited on
Commit
2fcfad9
Β·
verified Β·
1 Parent(s): b38e046

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +83 -29
app.py CHANGED
@@ -1,9 +1,13 @@
1
  #!/usr/bin/env python3
2
  import os
3
  import json
 
4
  import requests
5
  import gradio as gr
 
 
6
 
 
7
  ENDPOINT = os.environ.get("VLLM_ENDPOINT")
8
  MODEL = os.environ.get("VLLM_MODEL")
9
 
@@ -11,6 +15,13 @@ if not ENDPOINT or not MODEL:
11
  raise ValueError("VLLM_ENDPOINT and VLLM_MODEL environment variables must be set. Please add them as secrets in your Space settings.")
12
 
13
 
 
 
 
 
 
 
 
14
  def respond(
15
  message,
16
  history: list[dict[str, str]],
@@ -20,11 +31,40 @@ def respond(
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,
@@ -70,34 +110,48 @@ def respond(
70
  yield f"Error: {str(e)}"
71
 
72
 
73
- """
74
- For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
75
- """
76
- chatbot = gr.ChatInterface(
77
- respond,
78
- type="messages",
79
- additional_inputs=[
80
- gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
81
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
82
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
83
- gr.Slider(
84
- minimum=0.1,
85
- maximum=1.0,
86
- value=0.95,
87
- step=0.05,
88
- label="Top-p (nucleus sampling)",
89
- ),
90
- ],
91
- )
92
-
93
- with gr.Blocks(title="vLLM Chatbot") as demo:
94
- gr.Markdown("# πŸ’¬ Chat Interface")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
95
  gr.Markdown("""
96
- Configure the endpoint via environment variables:
97
- - `VLLM_ENDPOINT`: vLLM server URL
98
- - `VLLM_MODEL`: Model name
99
  """)
100
- chatbot.render()
101
 
102
 
103
  if __name__ == "__main__":
 
1
  #!/usr/bin/env python3
2
  import os
3
  import json
4
+ import base64
5
  import requests
6
  import gradio as gr
7
+ from PIL import Image
8
+ from io import BytesIO
9
 
10
+ # Get environment variables from HF Spaces secrets
11
  ENDPOINT = os.environ.get("VLLM_ENDPOINT")
12
  MODEL = os.environ.get("VLLM_MODEL")
13
 
 
15
  raise ValueError("VLLM_ENDPOINT and VLLM_MODEL environment variables must be set. Please add them as secrets in your Space settings.")
16
 
17
 
18
+ def image_to_base64(image):
19
+ """Convert PIL Image to base64 string."""
20
+ buffered = BytesIO()
21
+ image.save(buffered, format="PNG")
22
+ return base64.b64encode(buffered.getvalue()).decode("utf-8")
23
+
24
+
25
  def respond(
26
  message,
27
  history: list[dict[str, str]],
 
31
  top_p,
32
  ):
33
  """
34
+ Send messages (with optional images) to vLLM endpoint and stream the response.
35
  """
36
  messages = [{"role": "system", "content": system_message}]
37
+
38
+ # Add conversation history
39
+ for msg in history:
40
+ messages.append(msg)
41
+
42
+ # Process the current message - check if it contains an image
43
+ if message and "files" in message and message["files"]:
44
+ # Message has image(s)
45
+ content = []
46
+
47
+ # Add text if present
48
+ if message.get("text", "").strip():
49
+ content.append({"type": "text", "text": message["text"]})
50
+
51
+ # Add all images
52
+ for file_info in message["files"]:
53
+ try:
54
+ image = Image.open(file_info)
55
+ b64_image = image_to_base64(image)
56
+ content.append({
57
+ "type": "image_url",
58
+ "image_url": {"url": f"data:image/png;base64,{b64_image}"}
59
+ })
60
+ except Exception as e:
61
+ print(f"Error processing image: {e}")
62
+
63
+ messages.append({"role": "user", "content": content})
64
+ else:
65
+ # Text-only message
66
+ text_content = message if isinstance(message, str) else message.get("text", "")
67
+ messages.append({"role": "user", "content": text_content})
68
 
69
  payload = {
70
  "model": MODEL,
 
110
  yield f"Error: {str(e)}"
111
 
112
 
113
+ # Build the Gradio Interface
114
+ with gr.Blocks(title="πŸ’¬ Vision Chat", theme=gr.themes.Soft()) as demo:
115
+ gr.Markdown(
116
+ """
117
+ # πŸ’¬ Vision-Enabled Chat Interface
118
+ **πŸ’‘ How to use:**
119
+ 1. Type your message in the chat box
120
+ 2. Optionally upload images by clicking the πŸ“Ž icon
121
+ 3. Adjust parameters in the accordion below if needed
122
+ 4. Press Enter or click Send
123
+
124
+ The model can understand both text and images!
125
+ """
126
+ )
127
+
128
+ chatbot = gr.ChatInterface(
129
+ respond,
130
+ type="messages",
131
+ multimodal=True,
132
+ additional_inputs=[
133
+ gr.Textbox(
134
+ value="You are a helpful AI assistant with vision capabilities. You can understand and analyze images.",
135
+ label="System message"
136
+ ),
137
+ gr.Slider(minimum=1, maximum=4096, value=2048, step=1, label="Max new tokens"),
138
+ gr.Slider(minimum=0.1, maximum=2.0, value=0.7, step=0.1, label="Temperature"),
139
+ gr.Slider(
140
+ minimum=0.1,
141
+ maximum=1.0,
142
+ value=0.95,
143
+ step=0.05,
144
+ label="Top-p (nucleus sampling)",
145
+ ),
146
+ ],
147
+ )
148
+
149
+ chatbot.render()
150
+
151
  gr.Markdown("""
152
+ ---
153
+ **Note:** Configure endpoint via `VLLM_ENDPOINT` and `VLLM_MODEL` environment variables.
 
154
  """)
 
155
 
156
 
157
  if __name__ == "__main__":