Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -117,8 +117,7 @@ with gr.Blocks(theme=gr.themes.Default(primary_hue="blue", secondary_hue="neutra
|
|
117 |
demo.launch(server_name="0.0.0.0", server_port=7860)
|
118 |
|
119 |
"""
|
120 |
-
#
|
121 |
-
|
122 |
|
123 |
import gradio as gr
|
124 |
import ollama
|
@@ -127,29 +126,27 @@ import ollama
|
|
127 |
MODEL_NAME = 'hf.co/unsloth/gemma-3-4b-it-qat-GGUF:Q4_K_M'
|
128 |
|
129 |
# Default System Prompt
|
130 |
-
DEFAULT_SYSTEM_PROMPT = "You
|
131 |
|
132 |
# --- Gradio Interface ---
|
133 |
with gr.Blocks(theme=gr.themes.Default(primary_hue="blue", secondary_hue="neutral")) as demo:
|
134 |
-
gr.Markdown(f"
|
135 |
gr.Markdown("Chat with the model, customize its behavior with a system prompt, and toggle streaming output.")
|
136 |
|
137 |
-
#
|
138 |
-
# This resolves the UserWarning and simplifies history management.
|
139 |
chatbot = gr.Chatbot(
|
140 |
label="Conversation",
|
141 |
height=500,
|
142 |
-
type='messages',
|
143 |
layout="bubble"
|
144 |
)
|
145 |
|
146 |
with gr.Row():
|
147 |
msg = gr.Textbox(
|
148 |
-
|
149 |
placeholder="Type your message here and press Enter...",
|
150 |
lines=1,
|
151 |
scale=4,
|
152 |
-
show_label=False,
|
153 |
container=False
|
154 |
)
|
155 |
|
@@ -174,7 +171,7 @@ with gr.Blocks(theme=gr.themes.Default(primary_hue="blue", secondary_hue="neutra
|
|
174 |
interactive=False
|
175 |
)
|
176 |
|
177 |
-
# Function to
|
178 |
def toggle_system_prompt(use_custom):
|
179 |
return gr.update(interactive=use_custom)
|
180 |
|
@@ -185,58 +182,58 @@ with gr.Blocks(theme=gr.themes.Default(primary_hue="blue", secondary_hue="neutra
|
|
185 |
queue=False
|
186 |
)
|
187 |
|
188 |
-
# ---
|
189 |
-
# This is the
|
190 |
-
|
191 |
-
|
192 |
-
|
193 |
-
|
194 |
-
#
|
195 |
-
|
196 |
-
#
|
197 |
-
|
198 |
-
|
199 |
-
# Step 2: Get the bot's response.
|
200 |
-
# This function runs after the user's message has been added.
|
201 |
-
def get_bot_response(history, system_prompt, stream_output):
|
202 |
-
# Prepend the system prompt to the conversation history for the API call.
|
203 |
messages = [{"role": "system", "content": system_prompt}] + history
|
204 |
|
205 |
-
# Add a placeholder for the assistant's response.
|
|
|
206 |
history.append({"role": "assistant", "content": ""})
|
207 |
|
208 |
-
|
209 |
-
|
210 |
-
|
211 |
-
|
212 |
-
|
213 |
-
|
214 |
-
|
215 |
-
|
216 |
-
|
217 |
-
|
218 |
-
|
219 |
-
|
220 |
-
|
221 |
-
model=MODEL_NAME,
|
222 |
-
messages=messages,
|
223 |
-
stream=False
|
224 |
-
)
|
225 |
-
history[-1]['content'] = response['message']['content']
|
226 |
-
yield history
|
227 |
|
228 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
229 |
msg.submit(
|
230 |
-
|
231 |
inputs=[chatbot, msg],
|
232 |
outputs=[chatbot, msg],
|
233 |
-
queue=False
|
234 |
).then(
|
235 |
-
|
236 |
inputs=[chatbot, system_prompt_textbox, stream_checkbox],
|
237 |
outputs=[chatbot]
|
238 |
)
|
239 |
|
240 |
# Launch the Gradio interface
|
241 |
demo.launch(server_name="0.0.0.0", server_port=7860)
|
|
|
242 |
"""
|
|
|
117 |
demo.launch(server_name="0.0.0.0", server_port=7860)
|
118 |
|
119 |
"""
|
120 |
+
# Backup, OK: history, user sys prompt, cpu.:
|
|
|
121 |
|
122 |
import gradio as gr
|
123 |
import ollama
|
|
|
126 |
MODEL_NAME = 'hf.co/unsloth/gemma-3-4b-it-qat-GGUF:Q4_K_M'
|
127 |
|
128 |
# Default System Prompt
|
129 |
+
DEFAULT_SYSTEM_PROMPT = "You must response in zh-TW. Answer everything in simple, smart, relevant and accurate style. No chatty!"
|
130 |
|
131 |
# --- Gradio Interface ---
|
132 |
with gr.Blocks(theme=gr.themes.Default(primary_hue="blue", secondary_hue="neutral")) as demo:
|
133 |
+
gr.Markdown(f"## LLM GGUF Chat with `{MODEL_NAME}`")
|
134 |
gr.Markdown("Chat with the model, customize its behavior with a system prompt, and toggle streaming output.")
|
135 |
|
136 |
+
# Use the modern 'messages' type for the Chatbot component
|
|
|
137 |
chatbot = gr.Chatbot(
|
138 |
label="Conversation",
|
139 |
height=500,
|
140 |
+
type='messages',
|
141 |
layout="bubble"
|
142 |
)
|
143 |
|
144 |
with gr.Row():
|
145 |
msg = gr.Textbox(
|
146 |
+
show_label=False,
|
147 |
placeholder="Type your message here and press Enter...",
|
148 |
lines=1,
|
149 |
scale=4,
|
|
|
150 |
container=False
|
151 |
)
|
152 |
|
|
|
171 |
interactive=False
|
172 |
)
|
173 |
|
174 |
+
# Function to toggle the interactivity of the system prompt textbox
|
175 |
def toggle_system_prompt(use_custom):
|
176 |
return gr.update(interactive=use_custom)
|
177 |
|
|
|
182 |
queue=False
|
183 |
)
|
184 |
|
185 |
+
# --- Core Chat Logic ---
|
186 |
+
# This function is the heart of the application.
|
187 |
+
def respond(history, system_prompt, stream_output):
|
188 |
+
|
189 |
+
#This is the single function that handles the entire chat process.
|
190 |
+
#It takes the history, prepends the system prompt, calls the Ollama API,
|
191 |
+
#and streams the response back to the chatbot.
|
192 |
+
|
193 |
+
# --- FINAL FIX: Construct the API payload correctly ---
|
194 |
+
# The 'history' variable from Gradio contains the entire conversation.
|
195 |
+
# We prepend the system prompt to this history to form the final payload.
|
|
|
|
|
|
|
|
|
196 |
messages = [{"role": "system", "content": system_prompt}] + history
|
197 |
|
198 |
+
# Add a placeholder for the assistant's response to the UI history.
|
199 |
+
# This creates the space where the streamed response will be displayed.
|
200 |
history.append({"role": "assistant", "content": ""})
|
201 |
|
202 |
+
# Stream the response from the Ollama API
|
203 |
+
response_stream = ollama.chat(
|
204 |
+
model=MODEL_NAME,
|
205 |
+
messages=messages,
|
206 |
+
stream=True
|
207 |
+
)
|
208 |
+
|
209 |
+
# Iterate through the stream, updating the placeholder with each new chunk.
|
210 |
+
for chunk in response_stream:
|
211 |
+
if chunk['message']['content']:
|
212 |
+
history[-1]['content'] += chunk['message']['content']
|
213 |
+
# Yield the updated history to the chatbot for a real-time effect.
|
214 |
+
yield history
|
|
|
|
|
|
|
|
|
|
|
|
|
215 |
|
216 |
+
# This function handles the user's submission.
|
217 |
+
def user_submit(history, user_message):
|
218 |
+
|
219 |
+
#Adds the user's message to the chat history and clears the input box.
|
220 |
+
#This prepares the state for the main 'respond' function.
|
221 |
+
|
222 |
+
return history + [{"role": "user", "content": user_message}], ""
|
223 |
+
|
224 |
+
# Gradio Event Wiring
|
225 |
msg.submit(
|
226 |
+
user_submit,
|
227 |
inputs=[chatbot, msg],
|
228 |
outputs=[chatbot, msg],
|
229 |
+
queue=False
|
230 |
).then(
|
231 |
+
respond,
|
232 |
inputs=[chatbot, system_prompt_textbox, stream_checkbox],
|
233 |
outputs=[chatbot]
|
234 |
)
|
235 |
|
236 |
# Launch the Gradio interface
|
237 |
demo.launch(server_name="0.0.0.0", server_port=7860)
|
238 |
+
|
239 |
"""
|