crambrodev commited on
Commit
69ad559
Β·
verified Β·
1 Parent(s): e06aea3
Files changed (1) hide show
  1. app.py +146 -57
app.py CHANGED
@@ -1,69 +1,158 @@
 
 
1
  import gradio as gr
2
- from huggingface_hub import InferenceClient
 
3
 
 
 
 
 
 
4
 
5
- def respond(
6
- message,
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
- messages = [{"role": "system", "content": system_message}]
 
 
 
 
 
 
 
20
 
21
- messages.extend(history)
22
 
23
- messages.append({"role": "user", "content": message})
 
 
 
 
24
 
25
- response = ""
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26
 
27
- for message in client.chat_completion(
 
28
  messages,
29
- max_tokens=max_tokens,
30
- stream=True,
31
- temperature=temperature,
32
- top_p=top_p,
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
- For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
45
- """
46
- chatbot = gr.ChatInterface(
47
- respond,
48
- additional_inputs=[
49
- gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
50
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
51
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
52
- gr.Slider(
53
- minimum=0.1,
54
- maximum=1.0,
55
- value=0.95,
56
- step=0.05,
57
- label="Top-p (nucleus sampling)",
58
- ),
59
- ],
60
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
61
 
62
- with gr.Blocks() as demo:
63
- with gr.Sidebar():
64
- gr.LoginButton()
65
- chatbot.render()
 
 
 
 
 
66
 
 
 
 
 
 
 
 
 
 
67
 
68
- if __name__ == "__main__":
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()