Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,75 +2,52 @@ import gradio as gr
|
|
| 2 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 3 |
import torch
|
| 4 |
|
| 5 |
-
# Load model
|
| 6 |
-
model_name = "
|
| 7 |
-
tokenizer = AutoTokenizer.from_pretrained(model_name
|
| 8 |
-
model = AutoModelForCausalLM.from_pretrained(
|
| 9 |
-
model_name,
|
| 10 |
-
trust_remote_code=True,
|
| 11 |
-
torch_dtype="auto"
|
| 12 |
-
).to("cuda" if torch.cuda.is_available() else "cpu")
|
| 13 |
|
| 14 |
-
# Chat function
|
| 15 |
def chat(message, history):
|
| 16 |
history = history or []
|
| 17 |
|
| 18 |
-
# Build
|
| 19 |
-
messages = [{"role": "system", "content": "You are Isaac, a helpful AI assistant."}]
|
| 20 |
-
for user_msg, bot_msg in history:
|
| 21 |
-
messages.append({"role": "user", "content": user_msg})
|
| 22 |
-
if bot_msg:
|
| 23 |
-
messages.append({"role": "assistant", "content": bot_msg})
|
| 24 |
-
messages.append({"role": "user", "content": message})
|
| 25 |
-
|
| 26 |
-
# --- FIXED: safe prompt construction without apply_chat_template ---
|
| 27 |
prompt = "You are Isaac, a helpful AI assistant.\n"
|
| 28 |
for user_msg, bot_msg in history:
|
| 29 |
prompt += f"User: {user_msg}\n"
|
| 30 |
if bot_msg:
|
| 31 |
-
prompt += f"
|
| 32 |
-
prompt += f"User: {message}\
|
| 33 |
|
| 34 |
# Tokenize prompt
|
| 35 |
inputs_dict = tokenizer(prompt, return_tensors="pt")
|
| 36 |
for k in inputs_dict:
|
| 37 |
inputs_dict[k] = inputs_dict[k].to(model.device)
|
| 38 |
|
| 39 |
-
# Generate output
|
| 40 |
-
outputs = model.generate(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
-
# Decode
|
| 43 |
reply = tokenizer.decode(outputs[0][inputs_dict["input_ids"].shape[-1]:], skip_special_tokens=True)
|
| 44 |
history.append((message, reply))
|
| 45 |
return history, history
|
| 46 |
|
| 47 |
-
#
|
| 48 |
custom_css = """
|
| 49 |
-
body {
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
font-family: "Courier New", monospace;
|
| 54 |
-
}
|
| 55 |
-
#chatbot {
|
| 56 |
-
border: 2px solid #00ff00 !important;
|
| 57 |
-
background-color: #001100 !important;
|
| 58 |
-
box-shadow: 0px 0px 15px #00ff00;
|
| 59 |
-
}
|
| 60 |
-
input, textarea {
|
| 61 |
-
background-color: black !important;
|
| 62 |
-
color: #00ff00 !important;
|
| 63 |
-
border: 1px solid #00ff00 !important;
|
| 64 |
-
}
|
| 65 |
-
button {
|
| 66 |
-
background-color: #001100 !important;
|
| 67 |
-
color: #00ff00 !important;
|
| 68 |
-
border: 1px solid #00ff00 !important;
|
| 69 |
-
box-shadow: 0px 0px 8px #00ff00;
|
| 70 |
-
}
|
| 71 |
"""
|
| 72 |
|
| 73 |
-
# Build Gradio UI
|
| 74 |
with gr.Blocks(css=custom_css) as demo:
|
| 75 |
gr.HTML("<h1 style='color:#00ff00; text-align:center;'>π Isaac-0.1 Hacker Chat π</h1>")
|
| 76 |
chatbot = gr.Chatbot(elem_id="chatbot", height=500)
|
|
@@ -78,15 +55,12 @@ with gr.Blocks(css=custom_css) as demo:
|
|
| 78 |
clear = gr.Button("Clear Chat")
|
| 79 |
state = gr.State([])
|
| 80 |
|
| 81 |
-
# Handle user input
|
| 82 |
def user_input(user_message, history):
|
| 83 |
return "", history + [[user_message, None]]
|
| 84 |
|
| 85 |
-
msg.submit(user_input, [msg, state], [msg, state]).then(
|
| 86 |
-
chat, [msg, state], [chatbot, state]
|
| 87 |
-
)
|
| 88 |
clear.click(lambda: ([], []), None, [chatbot, state])
|
| 89 |
|
| 90 |
-
# Launch
|
| 91 |
if __name__ == "__main__":
|
| 92 |
demo.launch()
|
|
|
|
| 2 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 3 |
import torch
|
| 4 |
|
| 5 |
+
# ------------------- Load PHI-2 model -------------------
|
| 6 |
+
model_name = "microsoft/phi-2"
|
| 7 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 8 |
+
model = AutoModelForCausalLM.from_pretrained(model_name).to("cuda" if torch.cuda.is_available() else "cpu")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
+
# ------------------- Chat function -------------------
|
| 11 |
def chat(message, history):
|
| 12 |
history = history or []
|
| 13 |
|
| 14 |
+
# Build prompt for PHI-2
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
prompt = "You are Isaac, a helpful AI assistant.\n"
|
| 16 |
for user_msg, bot_msg in history:
|
| 17 |
prompt += f"User: {user_msg}\n"
|
| 18 |
if bot_msg:
|
| 19 |
+
prompt += f"Assistant: {bot_msg}\n"
|
| 20 |
+
prompt += f"User: {message}\nAssistant:"
|
| 21 |
|
| 22 |
# Tokenize prompt
|
| 23 |
inputs_dict = tokenizer(prompt, return_tensors="pt")
|
| 24 |
for k in inputs_dict:
|
| 25 |
inputs_dict[k] = inputs_dict[k].to(model.device)
|
| 26 |
|
| 27 |
+
# Generate output
|
| 28 |
+
outputs = model.generate(
|
| 29 |
+
**inputs_dict,
|
| 30 |
+
max_new_tokens=150,
|
| 31 |
+
do_sample=False, # Greedy for speed & coherence
|
| 32 |
+
pad_token_id=tokenizer.eos_token_id,
|
| 33 |
+
repetition_penalty=1.2, # Avoids loops
|
| 34 |
+
early_stopping=True
|
| 35 |
+
)
|
| 36 |
|
| 37 |
+
# Decode reply
|
| 38 |
reply = tokenizer.decode(outputs[0][inputs_dict["input_ids"].shape[-1]:], skip_special_tokens=True)
|
| 39 |
history.append((message, reply))
|
| 40 |
return history, history
|
| 41 |
|
| 42 |
+
# ------------------- Neon Hacker UI -------------------
|
| 43 |
custom_css = """
|
| 44 |
+
body { background-color: black; background-image: radial-gradient(circle, #001100, #000000 80%); color: #00ff00; font-family: "Courier New", monospace; }
|
| 45 |
+
#chatbot { border: 2px solid #00ff00 !important; background-color: #001100 !important; box-shadow: 0px 0px 15px #00ff00; }
|
| 46 |
+
input, textarea { background-color: black !important; color: #00ff00 !important; border: 1px solid #00ff00 !important; }
|
| 47 |
+
button { background-color: #001100 !important; color: #00ff00 !important; border: 1px solid #00ff00 !important; box-shadow: 0px 0px 8px #00ff00; }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
"""
|
| 49 |
|
| 50 |
+
# ------------------- Build Gradio UI -------------------
|
| 51 |
with gr.Blocks(css=custom_css) as demo:
|
| 52 |
gr.HTML("<h1 style='color:#00ff00; text-align:center;'>π Isaac-0.1 Hacker Chat π</h1>")
|
| 53 |
chatbot = gr.Chatbot(elem_id="chatbot", height=500)
|
|
|
|
| 55 |
clear = gr.Button("Clear Chat")
|
| 56 |
state = gr.State([])
|
| 57 |
|
|
|
|
| 58 |
def user_input(user_message, history):
|
| 59 |
return "", history + [[user_message, None]]
|
| 60 |
|
| 61 |
+
msg.submit(user_input, [msg, state], [msg, state]).then(chat, [msg, state], [chatbot, state])
|
|
|
|
|
|
|
| 62 |
clear.click(lambda: ([], []), None, [chatbot, state])
|
| 63 |
|
| 64 |
+
# ------------------- Launch -------------------
|
| 65 |
if __name__ == "__main__":
|
| 66 |
demo.launch()
|