Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,70 +1,83 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
|
|
|
|
| 3 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
system_message,
|
| 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="gitglubber/Qwen3-IWM")
|
| 18 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
messages = [{"role": "system", "content": system_message}]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
messages.append({"role": "user", "content": message})
|
| 24 |
-
|
| 25 |
-
response = ""
|
| 26 |
-
|
| 27 |
-
for message in client.chat_completion(
|
| 28 |
messages,
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 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 |
-
|
| 45 |
-
""
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
| 51 |
-
gr.Slider(minimum=1, maximum=4096, value=512, step=1, label="Max new tokens"),
|
| 52 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 53 |
-
gr.Slider(
|
| 54 |
-
minimum=0.1,
|
| 55 |
-
maximum=1.0,
|
| 56 |
-
value=0.95,
|
| 57 |
-
step=0.05,
|
| 58 |
-
label="Top-p (nucleus sampling)",
|
| 59 |
-
),
|
| 60 |
-
],
|
| 61 |
-
)
|
| 62 |
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
|
|
|
|
|
|
|
| 68 |
|
| 69 |
-
|
| 70 |
-
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import spaces
|
| 3 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 4 |
|
| 5 |
+
# --- Model Loading ---
|
| 6 |
+
model_name = "gitglubber/Qwen3-IWM"
|
| 7 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 8 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 9 |
+
model_name,
|
| 10 |
+
torch_dtype="auto",
|
| 11 |
+
device_map="auto"
|
| 12 |
+
)
|
| 13 |
|
| 14 |
+
# --- System Message ---
|
| 15 |
+
# Define the persona or instructions for the model
|
| 16 |
+
system_message = """"You are an expert at the terminal. Others need you to answer, appropriately. Continue your ownership of the terminal. Output commands decorated with @command. Be verbose in your output. If you need to execute a second command to fulfill the user action @command2. The user will give scenarios."""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
+
# --- Generation Function ---
|
| 19 |
+
@spaces.GPU(duration=120)
|
| 20 |
+
def generate_response(chat_history):
|
| 21 |
+
# Prepare the model input from the chat history
|
| 22 |
+
# The system message is the first entry
|
| 23 |
messages = [{"role": "system", "content": system_message}]
|
| 24 |
+
|
| 25 |
+
# Add previous user/assistant messages
|
| 26 |
+
for user_msg, assistant_msg in chat_history:
|
| 27 |
+
messages.append({"role": "user", "content": user_msg})
|
| 28 |
+
messages.append({"role": "assistant", "content": assistant_msg})
|
| 29 |
|
| 30 |
+
# Apply the chat template
|
| 31 |
+
text = tokenizer.apply_chat_template(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
messages,
|
| 33 |
+
tokenize=False,
|
| 34 |
+
add_generation_prompt=True,
|
| 35 |
+
)
|
| 36 |
+
model_inputs = tokenizer([text], return_tensors="pt").to(model.device)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
+
# Generate text
|
| 39 |
+
generated_ids = model.generate(
|
| 40 |
+
**model_inputs,
|
| 41 |
+
max_new_tokens=8192
|
| 42 |
+
)
|
| 43 |
+
output_ids = generated_ids[0][len(model_inputs.input_ids[0]):].tolist()
|
| 44 |
+
|
| 45 |
+
content = tokenizer.decode(output_ids, skip_special_tokens=True)
|
| 46 |
+
return content
|
| 47 |
|
| 48 |
+
# --- Gradio Interface ---
|
| 49 |
+
with gr.Blocks(fill_height=True) as demo:
|
| 50 |
+
gr.Markdown("# IWM Chat Bot")
|
| 51 |
+
# We use a state object to store the system message, though it's constant here
|
| 52 |
+
chatbot = gr.Chatbot(scale=1)
|
| 53 |
+
msg = gr.Textbox(label="Input", scale=0)
|
| 54 |
+
clear = gr.Button("Clear")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
|
| 56 |
+
def respond(message, chat_history):
|
| 57 |
+
if not message.strip(): # Check for empty or whitespace-only messages
|
| 58 |
+
return "", chat_history
|
| 59 |
+
|
| 60 |
+
# Append the new user message to the history
|
| 61 |
+
chat_history.append((message, None))
|
| 62 |
+
|
| 63 |
+
# Prepare history for the model (without the last empty spot)
|
| 64 |
+
model_input_history = chat_history[:-1]
|
| 65 |
+
model_input_history.append((message, None)) # Add current message for context
|
| 66 |
+
|
| 67 |
+
# Flatten the history for the model function
|
| 68 |
+
flat_history = []
|
| 69 |
+
for user, assistant in chat_history:
|
| 70 |
+
if user: flat_history.append((user, assistant))
|
| 71 |
+
|
| 72 |
+
bot_response = generate_response(flat_history)
|
| 73 |
+
|
| 74 |
+
# Update the last entry in chat_history with the bot's response
|
| 75 |
+
chat_history[-1] = (message, bot_response)
|
| 76 |
+
|
| 77 |
+
return "", chat_history
|
| 78 |
|
| 79 |
+
msg.submit(respond, [msg, chatbot], [msg, chatbot])
|
| 80 |
+
clear.click(lambda: None, None, chatbot, queue=False)
|
| 81 |
|
| 82 |
+
# Launch the app
|
| 83 |
+
demo.launch()
|