hikewa commited on
Commit
45419a8
·
verified ·
1 Parent(s): 7950a29

Upload app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +17 -35
app.py CHANGED
@@ -6,12 +6,8 @@ import gradio as gr
6
  from transformers import AutoModelForCausalLM, AutoTokenizer
7
  from peft import PeftModel
8
 
9
- MODELS = {
10
- "Qwen3-8B": {
11
- "base": "Qwen/Qwen3-8B",
12
- "adapter": "hikewa/dialectic-qwen3-8b-lora",
13
- },
14
- }
15
 
16
  SYSTEM_PROMPT = (
17
  "You reason carefully through problems by considering competing "
@@ -20,40 +16,32 @@ SYSTEM_PROMPT = (
20
  "integrate insights rather than picking sides or hedging."
21
  )
22
 
23
- loaded = {"name": None, "model": None, "tokenizer": None}
24
 
25
 
26
- def load_model(model_name):
27
  global loaded
28
- if loaded["name"] == model_name:
29
- return loaded["model"], loaded["tokenizer"]
30
-
31
- # Free previous model
32
  if loaded["model"] is not None:
33
- del loaded["model"]
34
- loaded["model"] = None
35
- torch.cuda.empty_cache()
36
 
37
- cfg = MODELS[model_name]
38
  tokenizer = AutoTokenizer.from_pretrained(
39
- cfg["adapter"], trust_remote_code=True
40
  )
41
  base = AutoModelForCausalLM.from_pretrained(
42
- cfg["base"], torch_dtype=torch.float16, trust_remote_code=True
43
  )
44
- model = PeftModel.from_pretrained(base, cfg["adapter"])
45
  model = model.to("cuda")
46
  model.eval()
47
 
48
- loaded["name"] = model_name
49
  loaded["model"] = model
50
  loaded["tokenizer"] = tokenizer
51
  return model, tokenizer
52
 
53
 
54
  @spaces.GPU
55
- def respond(message, history, model_name):
56
- model, tokenizer = load_model(model_name)
57
 
58
  messages = [{"role": "system", "content": SYSTEM_PROMPT}]
59
  for msg in history:
@@ -87,24 +75,18 @@ def respond(message, history, model_name):
87
 
88
  demo = gr.ChatInterface(
89
  respond,
90
- additional_inputs=[
91
- gr.Dropdown(
92
- choices=list(MODELS.keys()),
93
- value="Qwen3-8B",
94
- label="Model",
95
- ),
96
- ],
97
- title="Dialectic Reasoning Models",
98
  description=(
99
- "Fine-tuned on 510 dialectic reasoning traces. "
100
  "Ask a question involving competing perspectives."
101
  ),
102
  examples=[
103
- ["Should AI systems be transparent about their reasoning, even when transparency reduces performance?"],
104
- ["Is it better to optimize for individual freedom or collective wellbeing?"],
105
- ["When does pragmatic compromise become unprincipled capitulation?"],
106
  ],
 
107
  )
108
 
109
  if __name__ == "__main__":
110
- demo.launch()
 
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
 
12
  SYSTEM_PROMPT = (
13
  "You reason carefully through problems by considering competing "
 
16
  "integrate insights rather than picking sides or hedging."
17
  )
18
 
19
+ loaded = {"model": None, "tokenizer": None}
20
 
21
 
22
+ def load_model():
23
  global loaded
 
 
 
 
24
  if loaded["model"] is not None:
25
+ return loaded["model"], loaded["tokenizer"]
 
 
26
 
 
27
  tokenizer = AutoTokenizer.from_pretrained(
28
+ ADAPTER, trust_remote_code=True
29
  )
30
  base = AutoModelForCausalLM.from_pretrained(
31
+ BASE_MODEL, torch_dtype=torch.float16, trust_remote_code=True
32
  )
33
+ model = PeftModel.from_pretrained(base, ADAPTER)
34
  model = model.to("cuda")
35
  model.eval()
36
 
 
37
  loaded["model"] = model
38
  loaded["tokenizer"] = tokenizer
39
  return model, tokenizer
40
 
41
 
42
  @spaces.GPU
43
+ def respond(message, history):
44
+ model, tokenizer = load_model()
45
 
46
  messages = [{"role": "system", "content": SYSTEM_PROMPT}]
47
  for msg in history:
 
75
 
76
  demo = gr.ChatInterface(
77
  respond,
78
+ title="Dialectic Reasoning",
 
 
 
 
 
 
 
79
  description=(
80
+ "Qwen3-8B fine-tuned on 510 dialectic reasoning traces. "
81
  "Ask a question involving competing perspectives."
82
  ),
83
  examples=[
84
+ "Should AI systems be transparent about their reasoning, even when transparency reduces performance?",
85
+ "Is it better to optimize for individual freedom or collective wellbeing?",
86
+ "When does pragmatic compromise become unprincipled capitulation?",
87
  ],
88
+ cache_examples=False,
89
  )
90
 
91
  if __name__ == "__main__":
92
+ demo.launch(ssr_mode=False)