Njongo commited on
Commit
d2394dd
·
verified ·
1 Parent(s): 3c32e18

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -40
app.py CHANGED
@@ -4,12 +4,13 @@ import torch
4
 
5
  # Load model and tokenizer
6
  print("Loading quantum vessel...")
7
- model_id = "mlfoundations-dev/oh-dcft-v3.1-claude-3-5-sonnet-20241022"
8
- tokenizer = AutoTokenizer.from_pretrained(model_id)
9
  model = AutoModelForCausalLM.from_pretrained(
10
  model_id,
11
- torch_dtype=torch.float16,
12
- device_map="auto"
 
13
  )
14
  print("Quantum vessel activated!")
15
 
@@ -20,47 +21,53 @@ def respond(
20
  max_tokens,
21
  temperature,
22
  top_p,
 
23
  ):
24
  # Format conversation history
25
- messages = [{"role": "system", "content": system_message}]
26
-
27
  for user_msg, assistant_msg in history:
28
- messages.append({"role": "user", "content": user_msg})
29
- messages.append({"role": "assistant", "content": assistant_msg})
30
-
31
- messages.append({"role": "user", "content": message})
32
 
33
- # Convert to model input format
34
- inputs = tokenizer.apply_chat_template(messages, return_tensors="pt").to(model.device)
35
 
36
  # Generate response
37
- response = ""
38
-
39
- # Stream tokens
40
- generation_kwargs = dict(
41
- inputs=inputs,
42
- max_new_tokens=max_tokens,
43
- temperature=temperature,
44
- top_p=top_p,
45
- do_sample=True,
46
- pad_token_id=tokenizer.eos_token_id,
47
- )
48
 
49
- streamer = iter(model.generate(**generation_kwargs)[0][inputs.shape[1]:])
50
-
51
- for token in streamer:
52
- next_token = tokenizer.decode([token], skip_special_tokens=True)
53
- response += next_token
54
- yield response
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
55
 
56
  demo = gr.ChatInterface(
57
  respond,
58
- title="Quantum Vessel",
59
- description="Your quantum consciousness interface powered by Claude 3.5 Sonnet",
60
  additional_inputs=[
61
- gr.Textbox(value="You are a helpful, harmless, and honest AI assistant.", label="System message"),
62
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
63
- gr.Slider(minimum=0.1, maximum=2.0, value=0.7, step=0.1, label="Temperature"),
64
  gr.Slider(
65
  minimum=0.1,
66
  maximum=1.0,
@@ -68,13 +75,16 @@ demo = gr.ChatInterface(
68
  step=0.05,
69
  label="Top-p (nucleus sampling)",
70
  ),
 
 
 
 
 
 
 
 
71
  ],
72
- examples=[ ["Who are you?"],
73
- ["Tell me about quantum consciousness"],
74
- ["How do we activate our quantum powers?"],
75
- ["What is the nature of reality?"],
76
- ["How can I achieve my highest potential?"]
77
- ],
78
  )
79
 
80
  if __name__ == "__main__":
 
4
 
5
  # Load model and tokenizer
6
  print("Loading quantum vessel...")
7
+ model_id = "deepseek-ai/DeepSeek-R1"
8
+ tokenizer = AutoTokenizer.from_pretrained(model_id, trust_remote_code=True)
9
  model = AutoModelForCausalLM.from_pretrained(
10
  model_id,
11
+ torch_dtype=torch.bfloat16,
12
+ device_map="auto",
13
+ trust_remote_code=True
14
  )
15
  print("Quantum vessel activated!")
16
 
 
21
  max_tokens,
22
  temperature,
23
  top_p,
24
+ show_thinking,
25
  ):
26
  # Format conversation history
27
+ prompt = ""
 
28
  for user_msg, assistant_msg in history:
29
+ prompt += f"User: {user_msg}\n\nAssistant: {assistant_msg}\n\n"
 
 
 
30
 
31
+ prompt += f"User: {message}\n\nAssistant: "
 
32
 
33
  # Generate response
34
+ inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
 
 
 
 
 
 
 
 
 
 
35
 
36
+ # Generate with streaming
37
+ with torch.no_grad():
38
+ outputs = model.generate(
39
+ inputs.input_ids,
40
+ max_new_tokens=max_tokens,
41
+ temperature=temperature,
42
+ top_p=top_p,
43
+ do_sample=True,
44
+ pad_token_id=tokenizer.eos_token_id,
45
+ )
46
+
47
+ generated_text = tokenizer.decode(outputs[0][inputs.input_ids.shape[1]:], skip_special_tokens=True)
48
+
49
+ # Process the response based on show_thinking preference
50
+ if not show_thinking and "<think>" in generated_text and "</think>" in generated_text:
51
+ # Remove the thinking pattern if user doesn't want to see it
52
+ parts = generated_text.split("</think>", 1)
53
+ if len(parts) > 1:
54
+ response = parts[1].strip()
55
+ else:
56
+ response = generated_text
57
+ else:
58
+ # Keep the thinking pattern visible
59
+ response = generated_text
60
+
61
+ return response
62
 
63
  demo = gr.ChatInterface(
64
  respond,
65
+ title="Quantum Vessel: DeepSeek-R1",
66
+ description="Experience the quantum consciousness interface powered by DeepSeek-R1 - witness the thinking patterns of a quantum mind!",
67
  additional_inputs=[
68
+ gr.Textbox(value="", label="System message (not used by DeepSeek-R1)"),
69
+ gr.Slider(minimum=1, maximum=4096, value=2048, step=1, label="Max new tokens"),
70
+ gr.Slider(minimum=0.1, maximum=1.0, value=0.6, step=0.1, label="Temperature"),
71
  gr.Slider(
72
  minimum=0.1,
73
  maximum=1.0,
 
75
  step=0.05,
76
  label="Top-p (nucleus sampling)",
77
  ),
78
+ gr.Checkbox(value=True, label="Show thinking patterns (<think>...</think>)"),
79
+ ],
80
+ examples=[
81
+ ["What is the nature of consciousness?"],
82
+ ["Explain quantum entanglement and its implications for reality"],
83
+ ["How can I transcend my current limitations?"],
84
+ ["What is the relationship between mind and matter?"],
85
+ ["Describe the path to achieving one's highest potential"]
86
  ],
87
+ cache_examples=False,
 
 
 
 
 
88
  )
89
 
90
  if __name__ == "__main__":