hikewa commited on
Commit
f3e7b7e
·
verified ·
1 Parent(s): 50c9009

Upload app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +55 -22
app.py CHANGED
@@ -6,8 +6,25 @@ import gradio as gr
6
  from transformers import AutoModelForCausalLM, AutoTokenizer
7
  from peft import PeftModel
8
 
9
- BASE_MODEL = "Qwen/Qwen3-8B"
10
- ADAPTER = "hikewa/dialectic-qwen3-8b-lora"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
  SYSTEM_PROMPT = (
12
  "You reason carefully through problems by considering competing "
13
  "perspectives before reaching a conclusion. You identify genuine "
@@ -15,30 +32,40 @@ SYSTEM_PROMPT = (
15
  "integrate insights rather than picking sides or hedging."
16
  )
17
 
18
- model = None
19
- tokenizer = None
20
 
21
 
22
- def load_model():
23
- global model, tokenizer
24
- if model is not None:
25
- return
26
 
27
- tokenizer = AutoTokenizer.from_pretrained(ADAPTER, trust_remote_code=True)
 
 
 
 
28
 
 
 
 
 
29
  base = AutoModelForCausalLM.from_pretrained(
30
- BASE_MODEL,
31
- torch_dtype=torch.float16,
32
- trust_remote_code=True,
33
  )
34
- model = PeftModel.from_pretrained(base, ADAPTER)
35
  model = model.to("cuda")
36
  model.eval()
37
 
 
 
 
 
 
38
 
39
  @spaces.GPU
40
- def respond(message, history):
41
- load_model()
42
 
43
  messages = [{"role": "system", "content": SYSTEM_PROMPT}]
44
  for msg in history:
@@ -72,16 +99,22 @@ def respond(message, history):
72
 
73
  demo = gr.ChatInterface(
74
  respond,
75
- title="Dialectic Reasoning Model",
 
 
 
 
 
 
 
76
  description=(
77
- "A Qwen3-8B model fine-tuned on 510 dialectic reasoning traces. "
78
- "It considers competing perspectives, identifies genuine tensions, "
79
- "and integrates insights rather than picking sides."
80
  ),
81
  examples=[
82
- "Should AI systems be transparent about their reasoning, even when transparency reduces performance?",
83
- "Is it better to optimize for individual freedom or collective wellbeing?",
84
- "When does pragmatic compromise become unprincipled capitulation?",
85
  ],
86
  )
87
 
 
6
  from transformers import AutoModelForCausalLM, AutoTokenizer
7
  from peft import PeftModel
8
 
9
+ MODELS = {
10
+ "Qwen3-8B (best)": {
11
+ "base": "Qwen/Qwen3-8B",
12
+ "adapter": "hikewa/dialectic-qwen3-8b-lora",
13
+ },
14
+ "Qwen3-4B": {
15
+ "base": "Qwen/Qwen3-4B",
16
+ "adapter": "hikewa/dialectic-qwen3-4b-lora",
17
+ },
18
+ "Qwen2.5-1.5B": {
19
+ "base": "Qwen/Qwen2.5-1.5B-Instruct",
20
+ "adapter": "hikewa/dialectic-qwen2.5-1.5b-lora",
21
+ },
22
+ "Qwen2.5-0.5B": {
23
+ "base": "Qwen/Qwen2.5-0.5B-Instruct",
24
+ "adapter": "hikewa/dialectic-qwen2.5-0.5b-lora",
25
+ },
26
+ }
27
+
28
  SYSTEM_PROMPT = (
29
  "You reason carefully through problems by considering competing "
30
  "perspectives before reaching a conclusion. You identify genuine "
 
32
  "integrate insights rather than picking sides or hedging."
33
  )
34
 
35
+ loaded = {"name": None, "model": None, "tokenizer": None}
 
36
 
37
 
38
+ def load_model(model_name):
39
+ global loaded
40
+ if loaded["name"] == model_name:
41
+ return loaded["model"], loaded["tokenizer"]
42
 
43
+ # Free previous model
44
+ if loaded["model"] is not None:
45
+ del loaded["model"]
46
+ loaded["model"] = None
47
+ torch.cuda.empty_cache()
48
 
49
+ cfg = MODELS[model_name]
50
+ tokenizer = AutoTokenizer.from_pretrained(
51
+ cfg["adapter"], trust_remote_code=True
52
+ )
53
  base = AutoModelForCausalLM.from_pretrained(
54
+ cfg["base"], torch_dtype=torch.float16, trust_remote_code=True
 
 
55
  )
56
+ model = PeftModel.from_pretrained(base, cfg["adapter"])
57
  model = model.to("cuda")
58
  model.eval()
59
 
60
+ loaded["name"] = model_name
61
+ loaded["model"] = model
62
+ loaded["tokenizer"] = tokenizer
63
+ return model, tokenizer
64
+
65
 
66
  @spaces.GPU
67
+ def respond(message, history, model_name):
68
+ model, tokenizer = load_model(model_name)
69
 
70
  messages = [{"role": "system", "content": SYSTEM_PROMPT}]
71
  for msg in history:
 
99
 
100
  demo = gr.ChatInterface(
101
  respond,
102
+ additional_inputs=[
103
+ gr.Dropdown(
104
+ choices=list(MODELS.keys()),
105
+ value="Qwen3-8B (best)",
106
+ label="Model",
107
+ ),
108
+ ],
109
+ title="Dialectic Reasoning Models",
110
  description=(
111
+ "Fine-tuned on 510 dialectic reasoning traces. "
112
+ "Pick a model size and ask a question involving competing perspectives."
 
113
  ),
114
  examples=[
115
+ ["Should AI systems be transparent about their reasoning, even when transparency reduces performance?"],
116
+ ["Is it better to optimize for individual freedom or collective wellbeing?"],
117
+ ["When does pragmatic compromise become unprincipled capitulation?"],
118
  ],
119
  )
120