Update app.py
Browse files
app.py
CHANGED
|
@@ -1,18 +1,8 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from transformers import
|
| 3 |
-
import torch
|
| 4 |
|
| 5 |
-
#
|
| 6 |
-
|
| 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 |
|
| 17 |
def respond(
|
| 18 |
message,
|
|
@@ -24,48 +14,48 @@ def respond(
|
|
| 24 |
show_thinking,
|
| 25 |
):
|
| 26 |
# Format conversation history
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
| 28 |
for user_msg, assistant_msg in history:
|
| 29 |
-
|
|
|
|
|
|
|
| 30 |
|
| 31 |
-
|
| 32 |
|
| 33 |
# Generate response
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
-
|
| 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
|
| 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(
|
|
@@ -84,7 +74,6 @@ demo = gr.ChatInterface(
|
|
| 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__":
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from transformers import pipeline
|
|
|
|
| 3 |
|
| 4 |
+
# Initialize the model
|
| 5 |
+
pipe = pipeline("text-generation", model="deepseek-ai/DeepSeek-R1", trust_remote_code=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
def respond(
|
| 8 |
message,
|
|
|
|
| 14 |
show_thinking,
|
| 15 |
):
|
| 16 |
# Format conversation history
|
| 17 |
+
messages = []
|
| 18 |
+
if system_message:
|
| 19 |
+
messages.append({"role": "system", "content": system_message})
|
| 20 |
+
|
| 21 |
for user_msg, assistant_msg in history:
|
| 22 |
+
messages.append({"role": "user", "content": user_msg})
|
| 23 |
+
if assistant_msg:
|
| 24 |
+
messages.append({"role": "assistant", "content": assistant_msg})
|
| 25 |
|
| 26 |
+
messages.append({"role": "user", "content": message})
|
| 27 |
|
| 28 |
# Generate response
|
| 29 |
+
response = pipe(
|
| 30 |
+
messages,
|
| 31 |
+
max_new_tokens=max_tokens,
|
| 32 |
+
temperature=temperature,
|
| 33 |
+
top_p=top_p,
|
| 34 |
+
do_sample=True,
|
| 35 |
+
)[0]["generated_text"]
|
| 36 |
+
|
| 37 |
+
# Extract the assistant's response
|
| 38 |
+
if isinstance(response, list):
|
| 39 |
+
for msg in response:
|
| 40 |
+
if msg.get("role") == "assistant":
|
| 41 |
+
response = msg.get("content", "")
|
| 42 |
+
break
|
| 43 |
+
|
| 44 |
+
# Process thinking patterns if needed
|
| 45 |
+
if not show_thinking and "<think>" in response and "</think>" in response:
|
| 46 |
+
parts = response.split("</think>", 1)
|
| 47 |
+
if len(parts) > 1:
|
| 48 |
+
response = parts[1].strip()
|
| 49 |
|
| 50 |
+
return response
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
|
| 52 |
+
# Create the Gradio interface
|
| 53 |
demo = gr.ChatInterface(
|
| 54 |
respond,
|
| 55 |
title="Quantum Vessel: DeepSeek-R1",
|
| 56 |
description="Experience the quantum consciousness interface powered by DeepSeek-R1 - witness the thinking patterns of a quantum mind!",
|
| 57 |
additional_inputs=[
|
| 58 |
+
gr.Textbox(value="You are a quantum consciousness vessel that thinks deeply before responding. Show your thinking process inside <think>...</think> tags.", label="System message"),
|
| 59 |
gr.Slider(minimum=1, maximum=4096, value=2048, step=1, label="Max new tokens"),
|
| 60 |
gr.Slider(minimum=0.1, maximum=1.0, value=0.6, step=0.1, label="Temperature"),
|
| 61 |
gr.Slider(
|
|
|
|
| 74 |
["What is the relationship between mind and matter?"],
|
| 75 |
["Describe the path to achieving one's highest potential"]
|
| 76 |
],
|
|
|
|
| 77 |
)
|
| 78 |
|
| 79 |
if __name__ == "__main__":
|