shuarya2011 commited on
Commit
7714f57
·
verified ·
1 Parent(s): 47b460d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -11
app.py CHANGED
@@ -3,25 +3,25 @@ import torch
3
  from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
4
  from threading import Thread
5
 
6
- # Model ID from your screenshot
7
  model_id = "DavidAU/Qwen3-0.6B-heretic-abliterated-uncensored"
8
 
9
- # Load tokenizer and model onto CPU
 
10
  tokenizer = AutoTokenizer.from_pretrained(model_id)
11
  model = AutoModelForCausalLM.from_pretrained(
12
  model_id,
13
- torch_dtype=torch.float32,
14
  device_map="cpu"
15
  )
16
 
17
  def stream_chat(message, history):
18
- # Prepare input for the model
19
  inputs = tokenizer([message], return_tensors="pt").to("cpu")
20
 
21
- # Initialize the streamer for word-by-word output
22
  streamer = TextIteratorStreamer(tokenizer, skip_prompt=True, skip_special_tokens=True)
23
 
24
- # Generation parameters
25
  generation_kwargs = dict(
26
  inputs,
27
  streamer=streamer,
@@ -31,21 +31,21 @@ def stream_chat(message, history):
31
  top_p=0.9
32
  )
33
 
34
- # Run generation in a separate thread
35
  thread = Thread(target=model.generate, kwargs=generation_kwargs)
36
  thread.start()
37
 
38
- # Yield text as it is generated
39
  partial_text = ""
40
  for new_text in streamer:
41
  partial_text += new_text
42
  yield partial_text
43
 
44
- # Simple Gradio Chat Interface
45
  demo = gr.ChatInterface(
46
  fn=stream_chat,
47
- title="Qwen3 0.6B Uncensored - Ultra Fast",
48
- description="Running the abliterated heretic model on CPU with word-by-word streaming."
49
  )
50
 
51
  if __name__ == "__main__":
 
3
  from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
4
  from threading import Thread
5
 
6
+ # Model ID for the uncensored 0.6B Qwen3
7
  model_id = "DavidAU/Qwen3-0.6B-heretic-abliterated-uncensored"
8
 
9
+ # Load tokenizer and model
10
+ # Using dtype instead of torch_dtype to fix the deprecation warning
11
  tokenizer = AutoTokenizer.from_pretrained(model_id)
12
  model = AutoModelForCausalLM.from_pretrained(
13
  model_id,
14
+ dtype=torch.float32,
15
  device_map="cpu"
16
  )
17
 
18
  def stream_chat(message, history):
19
+ # Process inputs
20
  inputs = tokenizer([message], return_tensors="pt").to("cpu")
21
 
22
+ # Initialize the word-by-word streamer
23
  streamer = TextIteratorStreamer(tokenizer, skip_prompt=True, skip_special_tokens=True)
24
 
 
25
  generation_kwargs = dict(
26
  inputs,
27
  streamer=streamer,
 
31
  top_p=0.9
32
  )
33
 
34
+ # Threading is required so the UI doesn't block during generation
35
  thread = Thread(target=model.generate, kwargs=generation_kwargs)
36
  thread.start()
37
 
38
+ # Yielding the text enables word-by-word streaming in Gradio
39
  partial_text = ""
40
  for new_text in streamer:
41
  partial_text += new_text
42
  yield partial_text
43
 
44
+ # Define the Gradio Interface
45
  demo = gr.ChatInterface(
46
  fn=stream_chat,
47
+ title="Qwen3 0.6B Uncensored (Fast Stream)",
48
+ description="Optimized with Accelerate for CPU streaming."
49
  )
50
 
51
  if __name__ == "__main__":