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 @@ with gr.Blocks(theme=gr.themes.Default(primary_hue="blue", secondary_hue="neutra
|
|
| 64 |
# --- Core Chat Logic ---
|
| 65 |
# This function is the heart of the application.
|
| 66 |
def respond(history, system_prompt, stream_output):
|
| 67 |
-
|
| 68 |
-
This is the single function that handles the entire chat process.
|
| 69 |
-
It takes the history, prepends the system prompt, calls the Ollama API,
|
| 70 |
-
and streams the response back to the chatbot.
|
| 71 |
-
|
| 72 |
|
| 73 |
# --- FINAL FIX: Construct the API payload correctly ---
|
| 74 |
# The 'history' variable from Gradio contains the entire conversation.
|
|
@@ -95,10 +247,10 @@ with gr.Blocks(theme=gr.themes.Default(primary_hue="blue", secondary_hue="neutra
|
|
| 95 |
|
| 96 |
# This function handles the user's submission.
|
| 97 |
def user_submit(history, user_message):
|
| 98 |
-
|
| 99 |
-
Adds the user's message to the chat history and clears the input box.
|
| 100 |
-
This prepares the state for the main 'respond' function.
|
| 101 |
-
|
| 102 |
return history + [{"role": "user", "content": user_message}], ""
|
| 103 |
|
| 104 |
# Gradio Event Wiring
|
|
@@ -115,10 +267,13 @@ with gr.Blocks(theme=gr.themes.Default(primary_hue="blue", secondary_hue="neutra
|
|
| 115 |
|
| 116 |
# Launch the Gradio interface
|
| 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
|
| 124 |
|
|
|
|
| 1 |
+
import requests
|
| 2 |
+
|
| 3 |
+
check_ipinfo = requests.get("https://ipinfo.io").json()['country']
|
| 4 |
+
print("Run-Location-As: ",check_ipinfo)
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
import gradio as gr
|
| 8 |
+
import ollama
|
| 9 |
+
|
| 10 |
+
# List of available models for selection.
|
| 11 |
+
# IMPORTANT: These names must correspond to models that have been either
|
| 12 |
+
# 'ollama create'd from a Modelfile or 'ollama pull'ed within your Hugging Face Space.
|
| 13 |
+
#ollama pull hf.co/unsloth/gemma-3-4b-it-qat-GGUF:Q4_K_M
|
| 14 |
+
#ollama pull hf.co/Menlo/Jan-nano-128k-gguf:Q4_K_M
|
| 15 |
+
#ollama pull hf.co/bartowski/Qwen_Qwen3-4B-GGUF:Q4_K_M
|
| 16 |
+
#ollama pull hf.co/bartowski/Qwen_Qwen3-1.7B-GGUF:Q5_K_M
|
| 17 |
+
#ollama pull hf.co/bartowski/Llama-3.2-3B-Instruct-GGUF:Q4_K_M
|
| 18 |
+
|
| 19 |
+
AVAILABLE_MODELS = [
|
| 20 |
+
'hf.co/unsloth/gemma-3-4b-it-qat-GGUF:Q4_K_M', # This is the model created by run.sh
|
| 21 |
+
'hf.co/Menlo/Jan-nano-128k-gguf:Q4_K_M',
|
| 22 |
+
'hf.co/bartowski/Qwen_Qwen3-4B-GGUF:Q4_K_M',
|
| 23 |
+
'hf.co/bartowski/Qwen_Qwen3-1.7B-GGUF:Q5_K_M',
|
| 24 |
+
'hf.co/bartowski/Llama-3.2-3B-Instruct-GGUF:Q4_K_M'
|
| 25 |
+
]
|
| 26 |
+
|
| 27 |
+
# Default System Prompt
|
| 28 |
+
DEFAULT_SYSTEM_PROMPT = "You must response in zh-TW. Answer everything in simple, smart, relevant and accurate style. No chatty!"
|
| 29 |
+
|
| 30 |
+
# --- Gradio Interface ---
|
| 31 |
+
with gr.Blocks(theme=gr.themes.Default(primary_hue="blue", secondary_hue="neutral")) as demo:
|
| 32 |
+
gr.Markdown(f"## LLM GGUF Chat with Ollama") # Changed title to be more generic
|
| 33 |
+
gr.Markdown(f"(Run-Location-As: `{check_ipinfo}`)")
|
| 34 |
+
gr.Markdown("Chat with the model, customize its behavior with a system prompt, and toggle streaming output.")
|
| 35 |
+
|
| 36 |
+
# Model Selection
|
| 37 |
+
with gr.Row():
|
| 38 |
+
selected_model = gr.Radio(
|
| 39 |
+
choices=AVAILABLE_MODELS,
|
| 40 |
+
value=AVAILABLE_MODELS[0], # Default to the first model in the list
|
| 41 |
+
label="Select Model",
|
| 42 |
+
info="Choose the LLM model to chat with.",
|
| 43 |
+
interactive=True
|
| 44 |
+
)
|
| 45 |
+
|
| 46 |
+
chatbot = gr.Chatbot(
|
| 47 |
+
label="Conversation",
|
| 48 |
+
height=400,
|
| 49 |
+
type='messages',
|
| 50 |
+
layout="bubble"
|
| 51 |
+
)
|
| 52 |
+
|
| 53 |
+
with gr.Row():
|
| 54 |
+
msg = gr.Textbox(
|
| 55 |
+
show_label=False,
|
| 56 |
+
placeholder="Type your message here and press Enter...",
|
| 57 |
+
lines=1,
|
| 58 |
+
scale=4,
|
| 59 |
+
container=False
|
| 60 |
+
)
|
| 61 |
+
|
| 62 |
+
with gr.Accordion("Advanced Options", open=False):
|
| 63 |
+
with gr.Row():
|
| 64 |
+
stream_checkbox = gr.Checkbox(
|
| 65 |
+
label="Stream Output",
|
| 66 |
+
value=True,
|
| 67 |
+
info="Enable to see the response generate in real-time."
|
| 68 |
+
)
|
| 69 |
+
use_custom_prompt_checkbox = gr.Checkbox(
|
| 70 |
+
label="Use Custom System Prompt",
|
| 71 |
+
value=False,
|
| 72 |
+
info="Check this box to provide your own system prompt below."
|
| 73 |
+
)
|
| 74 |
+
|
| 75 |
+
system_prompt_textbox = gr.Textbox(
|
| 76 |
+
label="System Prompt",
|
| 77 |
+
value=DEFAULT_SYSTEM_PROMPT,
|
| 78 |
+
lines=3,
|
| 79 |
+
placeholder="Enter a system prompt to guide the model's behavior...",
|
| 80 |
+
interactive=False
|
| 81 |
+
)
|
| 82 |
+
|
| 83 |
+
# Function to toggle the interactivity of the system prompt textbox
|
| 84 |
+
def toggle_system_prompt(use_custom):
|
| 85 |
+
return gr.update(interactive=use_custom)
|
| 86 |
+
|
| 87 |
+
use_custom_prompt_checkbox.change(
|
| 88 |
+
fn=toggle_system_prompt,
|
| 89 |
+
inputs=use_custom_prompt_checkbox,
|
| 90 |
+
outputs=system_prompt_textbox,
|
| 91 |
+
queue=False
|
| 92 |
+
)
|
| 93 |
+
|
| 94 |
+
# --- Core Chat Logic ---
|
| 95 |
+
# This function is the heart of the application.
|
| 96 |
+
def respond(history, system_prompt, stream_output, current_selected_model): # Added current_selected_model
|
| 97 |
+
"""
|
| 98 |
+
This is the single function that handles the entire chat process.
|
| 99 |
+
It takes the history, prepends the system prompt, calls the Ollama API,
|
| 100 |
+
and streams the response back to the chatbot.
|
| 101 |
+
"""
|
| 102 |
+
|
| 103 |
+
# The 'history' variable from Gradio contains the entire conversation.
|
| 104 |
+
# We prepend the system prompt to this history to form the final payload.
|
| 105 |
+
messages = [{"role": "system", "content": system_prompt}] + history
|
| 106 |
+
|
| 107 |
+
# Add a placeholder for the assistant's response to the UI history.
|
| 108 |
+
# This creates the space where the streamed response will be displayed.
|
| 109 |
+
history.append({"role": "assistant", "content": ""})
|
| 110 |
+
|
| 111 |
+
# Stream the response from the Ollama API using the currently selected model
|
| 112 |
+
response_stream = ollama.chat(
|
| 113 |
+
model=current_selected_model, # Use the dynamically selected model
|
| 114 |
+
messages=messages,
|
| 115 |
+
stream=True
|
| 116 |
+
)
|
| 117 |
+
|
| 118 |
+
# Iterate through the stream, updating the placeholder with each new chunk.
|
| 119 |
+
for chunk in response_stream:
|
| 120 |
+
if chunk['message']['content']:
|
| 121 |
+
history[-1]['content'] += chunk['message']['content']
|
| 122 |
+
# Yield the updated history to the chatbot for a real-time effect.
|
| 123 |
+
yield history
|
| 124 |
+
|
| 125 |
+
# This function handles the user's submission.
|
| 126 |
+
def user_submit(history, user_message):
|
| 127 |
+
"""
|
| 128 |
+
Adds the user's message to the chat history and clears the input box.
|
| 129 |
+
This prepares the state for the main 'respond' function.
|
| 130 |
+
"""
|
| 131 |
+
return history + [{"role": "user", "content": user_message}], ""
|
| 132 |
+
|
| 133 |
+
# Gradio Event Wiring
|
| 134 |
+
msg.submit(
|
| 135 |
+
user_submit,
|
| 136 |
+
inputs=[chatbot, msg],
|
| 137 |
+
outputs=[chatbot, msg],
|
| 138 |
+
queue=False
|
| 139 |
+
).then(
|
| 140 |
+
respond,
|
| 141 |
+
inputs=[chatbot, system_prompt_textbox, stream_checkbox, selected_model], # Pass selected_model here
|
| 142 |
+
outputs=[chatbot]
|
| 143 |
+
)
|
| 144 |
+
|
| 145 |
+
# Launch the Gradio interface
|
| 146 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|
| 147 |
+
|
| 148 |
+
|
| 149 |
+
"""
|
| 150 |
+
#---------------------------------------------------------------
|
| 151 |
+
# v20250625, OK run with CPU, Gemma 3 4b it qat gguf, history support.
|
| 152 |
+
|
| 153 |
import gradio as gr
|
| 154 |
import ollama
|
| 155 |
|
|
|
|
| 216 |
# --- Core Chat Logic ---
|
| 217 |
# This function is the heart of the application.
|
| 218 |
def respond(history, system_prompt, stream_output):
|
| 219 |
+
|
| 220 |
+
#This is the single function that handles the entire chat process.
|
| 221 |
+
#It takes the history, prepends the system prompt, calls the Ollama API,
|
| 222 |
+
#and streams the response back to the chatbot.
|
| 223 |
+
|
| 224 |
|
| 225 |
# --- FINAL FIX: Construct the API payload correctly ---
|
| 226 |
# The 'history' variable from Gradio contains the entire conversation.
|
|
|
|
| 247 |
|
| 248 |
# This function handles the user's submission.
|
| 249 |
def user_submit(history, user_message):
|
| 250 |
+
|
| 251 |
+
#Adds the user's message to the chat history and clears the input box.
|
| 252 |
+
#This prepares the state for the main 'respond' function.
|
| 253 |
+
|
| 254 |
return history + [{"role": "user", "content": user_message}], ""
|
| 255 |
|
| 256 |
# Gradio Event Wiring
|
|
|
|
| 267 |
|
| 268 |
# Launch the Gradio interface
|
| 269 |
demo.launch(server_name="0.0.0.0", server_port=7860)
|
| 270 |
+
#---------------------------------------------------------------
|
| 271 |
"""
|
|
|
|
| 272 |
|
| 273 |
+
"""
|
| 274 |
+
#---------------------------------------------------------------
|
| 275 |
+
# Backup, OK: history, user sys prompt, cpu.:
|
| 276 |
+
#---------------------------------------------------------------
|
| 277 |
import gradio as gr
|
| 278 |
import ollama
|
| 279 |
|