Spaces:
Runtime error
Runtime error
42
Browse files
app.py
CHANGED
|
@@ -1,69 +1,158 @@
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from
|
|
|
|
| 3 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
history: list[dict[str, str]],
|
| 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="openai/gpt-oss-20b")
|
| 18 |
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
-
|
| 22 |
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
-
|
|
|
|
| 28 |
messages,
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
""
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
|
| 68 |
-
|
| 69 |
-
demo.launch()
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import torch
|
| 3 |
import gradio as gr
|
| 4 |
+
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.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
+
Key facts about Hytale modding:
|
| 17 |
+
- Plugins are written in Java or Kotlin
|
| 18 |
+
- Entry point: extend JavaPlugin, implement setup() method
|
| 19 |
+
- Manifest file: manifest.json defines plugin metadata
|
| 20 |
+
- Build system: Gradle with the hytale-mod plugin
|
| 21 |
+
- Commands: extend CommandBase, override executeSync()
|
| 22 |
+
- Events: use event listener system to hook into game events
|
| 23 |
+
- API package: com.hypixel.hytale.server.core.*
|
| 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)
|
| 32 |
|
| 33 |
+
print("Loading base model...")
|
| 34 |
+
base_model = AutoModelForCausalLM.from_pretrained(
|
| 35 |
+
BASE_MODEL,
|
| 36 |
+
torch_dtype=torch.float16,
|
| 37 |
+
device_map="auto",
|
| 38 |
+
trust_remote_code=True,
|
| 39 |
+
)
|
| 40 |
+
|
| 41 |
+
print("Loading LoRA adapter...")
|
| 42 |
+
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,
|
| 65 |
+
add_generation_prompt=True,
|
| 66 |
+
)
|
| 67 |
+
inputs = tokenizer(text, return_tensors="pt").to(model.device)
|
| 68 |
+
|
| 69 |
+
# ΠΠ΅Π½Π΅ΡΠΈΡΡΠ΅ΠΌ
|
| 70 |
+
with torch.no_grad():
|
| 71 |
+
outputs = model.generate(
|
| 72 |
+
**inputs,
|
| 73 |
+
max_new_tokens=max_tokens,
|
| 74 |
+
temperature=temperature,
|
| 75 |
+
do_sample=temperature > 0,
|
| 76 |
+
pad_token_id=tokenizer.eos_token_id,
|
| 77 |
+
)
|
| 78 |
+
|
| 79 |
+
response = tokenizer.decode(
|
| 80 |
+
outputs[0][inputs["input_ids"].shape[1]:],
|
| 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 |
+
# Gradio UI
|
| 92 |
+
# βββββββββββββββββββββββββββββββββββββββββββββ
|
| 93 |
+
with gr.Blocks(title="DragonvineAI β Hytale Modding Assistant", theme=gr.themes.Soft()) as demo:
|
| 94 |
+
gr.Markdown("""
|
| 95 |
+
# π DragonvineAI β Hytale Modding Assistant
|
| 96 |
+
Ask anything about creating Hytale plugins and mods!
|
| 97 |
+
""")
|
| 98 |
+
|
| 99 |
+
chatbot = gr.Chatbot(height=500, label="Chat")
|
| 100 |
+
|
| 101 |
+
with gr.Row():
|
| 102 |
+
msg = gr.Textbox(
|
| 103 |
+
placeholder="Ask about Hytale modding... e.g. 'How do I create a custom command?'",
|
| 104 |
+
label="Your question",
|
| 105 |
+
scale=4,
|
| 106 |
+
)
|
| 107 |
+
submit = gr.Button("Send π", scale=1, variant="primary")
|
| 108 |
+
|
| 109 |
+
with gr.Accordion("βοΈ Settings", open=False):
|
| 110 |
+
thinking = gr.Checkbox(
|
| 111 |
+
label="π§ Thinking mode (slower but smarter)",
|
| 112 |
+
value=False,
|
| 113 |
+
)
|
| 114 |
+
max_tok = gr.Slider(128, 1024, value=512, step=64, label="Max tokens")
|
| 115 |
+
temp = gr.Slider(0.1, 1.0, value=0.7, step=0.1, label="Temperature")
|
| 116 |
+
|
| 117 |
+
gr.Examples(
|
| 118 |
+
examples=[
|
| 119 |
+
"How do I create a simple Hytale plugin with a /hello command?",
|
| 120 |
+
"Show me how to listen to player join events in Hytale",
|
| 121 |
+
"How do I create a custom NPC in Hytale?",
|
| 122 |
+
"What does a basic manifest.json look like for a Hytale plugin?",
|
| 123 |
+
"How do I register a command in Hytale?",
|
| 124 |
+
],
|
| 125 |
+
inputs=msg,
|
| 126 |
+
)
|
| 127 |
+
|
| 128 |
+
def user_submit(message, history, thinking, max_tok, temp):
|
| 129 |
+
history = history + [[message, None]]
|
| 130 |
+
return "", history
|
| 131 |
+
|
| 132 |
+
def bot_respond(history, thinking, max_tok, temp):
|
| 133 |
+
user_message = history[-1][0]
|
| 134 |
+
response = respond(user_message, history[:-1], thinking, max_tok, temp)
|
| 135 |
+
history[-1][1] = response
|
| 136 |
+
return history
|
| 137 |
|
| 138 |
+
submit.click(
|
| 139 |
+
user_submit,
|
| 140 |
+
inputs=[msg, chatbot, thinking, max_tok, temp],
|
| 141 |
+
outputs=[msg, chatbot],
|
| 142 |
+
).then(
|
| 143 |
+
bot_respond,
|
| 144 |
+
inputs=[chatbot, thinking, max_tok, temp],
|
| 145 |
+
outputs=chatbot,
|
| 146 |
+
)
|
| 147 |
|
| 148 |
+
msg.submit(
|
| 149 |
+
user_submit,
|
| 150 |
+
inputs=[msg, chatbot, thinking, max_tok, temp],
|
| 151 |
+
outputs=[msg, chatbot],
|
| 152 |
+
).then(
|
| 153 |
+
bot_respond,
|
| 154 |
+
inputs=[chatbot, thinking, max_tok, temp],
|
| 155 |
+
outputs=chatbot,
|
| 156 |
+
)
|
| 157 |
|
| 158 |
+
demo.launch()
|
|
|