Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,10 +5,8 @@ from transformers import AutoTokenizer, AutoModelForCausalLM
|
|
| 5 |
from peft import PeftModel
|
| 6 |
|
| 7 |
# βββββββββββββββββββββββββββββββββββββββββββββ
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
BASE_MODEL = "Qwen/Qwen3-8B"
|
| 11 |
-
LORA_MODEL = "crambrodev/dragonvineAI-qwen3-hytale"
|
| 12 |
|
| 13 |
SYSTEM_PROMPT = """You are DragonvineAI β an expert Hytale modding assistant.
|
| 14 |
You help developers create plugins and mods for Hytale servers.
|
|
@@ -24,8 +22,6 @@ Key facts about Hytale modding:
|
|
| 24 |
|
| 25 |
Always provide working, well-commented code examples."""
|
| 26 |
|
| 27 |
-
# βββββββββββββββββββββββββββββββββββββββββββββ
|
| 28 |
-
# ΠΠ°Π³ΡΡΠ·ΠΊΠ° ΠΌΠΎΠ΄Π΅Π»ΠΈ
|
| 29 |
# βββββββββββββββββββββββββββββββββββββββββββββ
|
| 30 |
print("Loading tokenizer...")
|
| 31 |
tokenizer = AutoTokenizer.from_pretrained(BASE_MODEL, trust_remote_code=True)
|
|
@@ -43,22 +39,14 @@ model = PeftModel.from_pretrained(base_model, LORA_MODEL)
|
|
| 43 |
model.eval()
|
| 44 |
print("Model ready!")
|
| 45 |
|
| 46 |
-
# βββββββββββββββββββββββββββββββββββββββββββββ
|
| 47 |
-
# ΠΠ΅Π½Π΅ΡΠ°ΡΠΈΡ ΠΎΡΠ²Π΅ΡΠ°
|
| 48 |
# βββββββββββββββββββββββββββββββββββββββββββββ
|
| 49 |
def respond(message, history, thinking_mode, max_tokens, temperature):
|
| 50 |
-
# Π‘ΠΎΠ±ΠΈΡΠ°Π΅ΠΌ ΠΈΡΡΠΎΡΠΈΡ
|
| 51 |
messages = [{"role": "system", "content": SYSTEM_PROMPT}]
|
|
|
|
| 52 |
|
| 53 |
-
for user_msg, assistant_msg in history:
|
| 54 |
-
messages.append({"role": "user", "content": user_msg})
|
| 55 |
-
messages.append({"role": "assistant", "content": assistant_msg})
|
| 56 |
-
|
| 57 |
-
# Thinking mode Qwen3
|
| 58 |
prefix = "/think " if thinking_mode else "/no_think "
|
| 59 |
messages.append({"role": "user", "content": prefix + message})
|
| 60 |
|
| 61 |
-
# Π’ΠΎΠΊΠ΅Π½ΠΈΠ·ΠΈΡΡΠ΅ΠΌ
|
| 62 |
text = tokenizer.apply_chat_template(
|
| 63 |
messages,
|
| 64 |
tokenize=False,
|
|
@@ -66,7 +54,6 @@ def respond(message, history, thinking_mode, max_tokens, temperature):
|
|
| 66 |
)
|
| 67 |
inputs = tokenizer(text, return_tensors="pt").to(model.device)
|
| 68 |
|
| 69 |
-
# ΠΠ΅Π½Π΅ΡΠΈΡΡΠ΅ΠΌ
|
| 70 |
with torch.no_grad():
|
| 71 |
outputs = model.generate(
|
| 72 |
**inputs,
|
|
@@ -81,23 +68,16 @@ def respond(message, history, thinking_mode, max_tokens, temperature):
|
|
| 81 |
skip_special_tokens=True,
|
| 82 |
)
|
| 83 |
|
| 84 |
-
# Π£Π±ΠΈΡΠ°Π΅ΠΌ thinking Π±Π»ΠΎΠΊ ΠΈΠ· ΠΎΡΠ²Π΅ΡΠ° Π΅ΡΠ»ΠΈ ΠΎΠ½ Π΅ΡΡΡ
|
| 85 |
if "<think>" in response and "</think>" in response:
|
| 86 |
response = response.split("</think>")[-1].strip()
|
| 87 |
|
| 88 |
return response
|
| 89 |
|
| 90 |
# βββββββββββββββββββββββββββββββββββββββββββββ
|
| 91 |
-
|
| 92 |
-
#
|
| 93 |
-
with gr.Blocks(title="DragonvineAI β Hytale Modding Assistant") as demo:
|
| 94 |
-
gr.Markdown("""
|
| 95 |
-
# π DragonvineAI β Hytale Modding Assistant
|
| 96 |
-
Ask anything about creating Hytale plugins and mods!
|
| 97 |
-
""")
|
| 98 |
|
| 99 |
-
|
| 100 |
-
chatbot = gr.Chatbot(height=500, label="Chat", type="tuples")
|
| 101 |
|
| 102 |
with gr.Row():
|
| 103 |
msg = gr.Textbox(
|
|
@@ -107,53 +87,42 @@ with gr.Blocks(title="DragonvineAI β Hytale Modding Assistant") as demo:
|
|
| 107 |
)
|
| 108 |
submit = gr.Button("Send π", scale=1, variant="primary")
|
| 109 |
|
| 110 |
-
with gr.Accordion("
|
| 111 |
-
thinking = gr.Checkbox(
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
)
|
| 115 |
-
max_tok = gr.Slider(128, 1024, value=512, step=64, label="Max tokens")
|
| 116 |
-
temp = gr.Slider(0.1, 1.0, value=0.7, step=0.1, label="Temperature")
|
| 117 |
|
| 118 |
gr.Examples(
|
| 119 |
examples=[
|
| 120 |
"How do I create a simple Hytale plugin with a /hello command?",
|
| 121 |
"Show me how to listen to player join events in Hytale",
|
| 122 |
-
"How do I create a custom NPC in Hytale?",
|
| 123 |
"What does a basic manifest.json look like for a Hytale plugin?",
|
| 124 |
"How do I register a command in Hytale?",
|
| 125 |
],
|
| 126 |
inputs=msg,
|
| 127 |
)
|
| 128 |
|
| 129 |
-
def user_submit(message, history
|
| 130 |
-
history = history + [
|
| 131 |
return "", history
|
| 132 |
|
| 133 |
def bot_respond(history, thinking, max_tok, temp):
|
| 134 |
-
user_message = history[-1][
|
| 135 |
-
|
| 136 |
-
|
|
|
|
| 137 |
return history
|
| 138 |
|
| 139 |
submit.click(
|
| 140 |
-
user_submit,
|
| 141 |
-
inputs=[msg, chatbot, thinking, max_tok, temp],
|
| 142 |
-
outputs=[msg, chatbot],
|
| 143 |
).then(
|
| 144 |
-
bot_respond,
|
| 145 |
-
inputs=[chatbot, thinking, max_tok, temp],
|
| 146 |
-
outputs=chatbot,
|
| 147 |
)
|
| 148 |
|
| 149 |
msg.submit(
|
| 150 |
-
user_submit,
|
| 151 |
-
inputs=[msg, chatbot, thinking, max_tok, temp],
|
| 152 |
-
outputs=[msg, chatbot],
|
| 153 |
).then(
|
| 154 |
-
bot_respond,
|
| 155 |
-
inputs=[chatbot, thinking, max_tok, temp],
|
| 156 |
-
outputs=chatbot,
|
| 157 |
)
|
| 158 |
|
| 159 |
demo.launch()
|
|
|
|
| 5 |
from peft import PeftModel
|
| 6 |
|
| 7 |
# βββββββββββββββββββββββββββββββββββββββββββββ
|
| 8 |
+
BASE_MODEL = "Qwen/Qwen3-8B"
|
| 9 |
+
LORA_MODEL = "crambrodev/dragonvineAI-qwen3-hytale"
|
|
|
|
|
|
|
| 10 |
|
| 11 |
SYSTEM_PROMPT = """You are DragonvineAI β an expert Hytale modding assistant.
|
| 12 |
You help developers create plugins and mods for Hytale servers.
|
|
|
|
| 22 |
|
| 23 |
Always provide working, well-commented code examples."""
|
| 24 |
|
|
|
|
|
|
|
| 25 |
# βββββββββββββββββββββββββββββββββββββββββββββ
|
| 26 |
print("Loading tokenizer...")
|
| 27 |
tokenizer = AutoTokenizer.from_pretrained(BASE_MODEL, trust_remote_code=True)
|
|
|
|
| 39 |
model.eval()
|
| 40 |
print("Model ready!")
|
| 41 |
|
|
|
|
|
|
|
| 42 |
# βββββββββββββββββββββββββββββββββββββββββββββ
|
| 43 |
def respond(message, history, thinking_mode, max_tokens, temperature):
|
|
|
|
| 44 |
messages = [{"role": "system", "content": SYSTEM_PROMPT}]
|
| 45 |
+
messages += history
|
| 46 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
prefix = "/think " if thinking_mode else "/no_think "
|
| 48 |
messages.append({"role": "user", "content": prefix + message})
|
| 49 |
|
|
|
|
| 50 |
text = tokenizer.apply_chat_template(
|
| 51 |
messages,
|
| 52 |
tokenize=False,
|
|
|
|
| 54 |
)
|
| 55 |
inputs = tokenizer(text, return_tensors="pt").to(model.device)
|
| 56 |
|
|
|
|
| 57 |
with torch.no_grad():
|
| 58 |
outputs = model.generate(
|
| 59 |
**inputs,
|
|
|
|
| 68 |
skip_special_tokens=True,
|
| 69 |
)
|
| 70 |
|
|
|
|
| 71 |
if "<think>" in response and "</think>" in response:
|
| 72 |
response = response.split("</think>")[-1].strip()
|
| 73 |
|
| 74 |
return response
|
| 75 |
|
| 76 |
# βββββββββββββββββββββββββββββββββββββββββββββ
|
| 77 |
+
with gr.Blocks(title="DragonvineAI β Hytale Modding Assistant", theme=gr.themes.Soft()) as demo:
|
| 78 |
+
gr.Markdown("# π DragonvineAI β Hytale Modding Assistant\nAsk anything about creating Hytale plugins and mods!")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
|
| 80 |
+
chatbot = gr.Chatbot(height=500, label="Chat", type="messages")
|
|
|
|
| 81 |
|
| 82 |
with gr.Row():
|
| 83 |
msg = gr.Textbox(
|
|
|
|
| 87 |
)
|
| 88 |
submit = gr.Button("Send π", scale=1, variant="primary")
|
| 89 |
|
| 90 |
+
with gr.Accordion("Settings", open=False):
|
| 91 |
+
thinking = gr.Checkbox(label="Thinking mode (slower but smarter)", value=False)
|
| 92 |
+
max_tok = gr.Slider(128, 1024, value=512, step=64, label="Max tokens")
|
| 93 |
+
temp = gr.Slider(0.1, 1.0, value=0.7, step=0.1, label="Temperature")
|
|
|
|
|
|
|
|
|
|
| 94 |
|
| 95 |
gr.Examples(
|
| 96 |
examples=[
|
| 97 |
"How do I create a simple Hytale plugin with a /hello command?",
|
| 98 |
"Show me how to listen to player join events in Hytale",
|
|
|
|
| 99 |
"What does a basic manifest.json look like for a Hytale plugin?",
|
| 100 |
"How do I register a command in Hytale?",
|
| 101 |
],
|
| 102 |
inputs=msg,
|
| 103 |
)
|
| 104 |
|
| 105 |
+
def user_submit(message, history):
|
| 106 |
+
history = history + [{"role": "user", "content": message}]
|
| 107 |
return "", history
|
| 108 |
|
| 109 |
def bot_respond(history, thinking, max_tok, temp):
|
| 110 |
+
user_message = history[-1]["content"]
|
| 111 |
+
prev_history = history[:-1]
|
| 112 |
+
response = respond(user_message, prev_history, thinking, max_tok, temp)
|
| 113 |
+
history = history + [{"role": "assistant", "content": response}]
|
| 114 |
return history
|
| 115 |
|
| 116 |
submit.click(
|
| 117 |
+
user_submit, inputs=[msg, chatbot], outputs=[msg, chatbot]
|
|
|
|
|
|
|
| 118 |
).then(
|
| 119 |
+
bot_respond, inputs=[chatbot, thinking, max_tok, temp], outputs=chatbot
|
|
|
|
|
|
|
| 120 |
)
|
| 121 |
|
| 122 |
msg.submit(
|
| 123 |
+
user_submit, inputs=[msg, chatbot], outputs=[msg, chatbot]
|
|
|
|
|
|
|
| 124 |
).then(
|
| 125 |
+
bot_respond, inputs=[chatbot, thinking, max_tok, temp], outputs=chatbot
|
|
|
|
|
|
|
| 126 |
)
|
| 127 |
|
| 128 |
demo.launch()
|