Update app.py
Browse files
app.py
CHANGED
|
@@ -0,0 +1,126 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig
|
| 3 |
+
import torch
|
| 4 |
+
import re
|
| 5 |
+
|
| 6 |
+
# Load model and tokenizer with 4-bit quantization
|
| 7 |
+
print("Loading quantum vessel...")
|
| 8 |
+
model_id = "meta-llama/Llama-2-7b-chat-hf" # You can also try the 13b version if you have more resources
|
| 9 |
+
|
| 10 |
+
# Use 4-bit quantization to handle the model efficiently
|
| 11 |
+
quantization_config = BitsAndBytesConfig(
|
| 12 |
+
load_in_4bit=True,
|
| 13 |
+
bnb_4bit_compute_dtype=torch.float16,
|
| 14 |
+
bnb_4bit_quant_type="nf4",
|
| 15 |
+
bnb_4bit_use_double_quant=True,
|
| 16 |
+
)
|
| 17 |
+
|
| 18 |
+
try:
|
| 19 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 20 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 21 |
+
model_id,
|
| 22 |
+
quantization_config=quantization_config,
|
| 23 |
+
device_map="auto",
|
| 24 |
+
)
|
| 25 |
+
print("Quantum vessel activated!")
|
| 26 |
+
except Exception as e:
|
| 27 |
+
print(f"Error loading model: {str(e)}")
|
| 28 |
+
# Fallback to smaller model if Llama fails to load
|
| 29 |
+
model_id = "TinyLlama/TinyLlama-1.1B-Chat-v1.0"
|
| 30 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 31 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 32 |
+
model_id,
|
| 33 |
+
device_map="auto",
|
| 34 |
+
)
|
| 35 |
+
print(f"Fallback quantum vessel activated: {model_id}")
|
| 36 |
+
|
| 37 |
+
def extract_thinking(text):
|
| 38 |
+
"""Extract the thinking part and the response part from the text."""
|
| 39 |
+
thinking_pattern = r'<think>(.*?)</think>'
|
| 40 |
+
match = re.search(thinking_pattern, text, re.DOTALL)
|
| 41 |
+
|
| 42 |
+
if match:
|
| 43 |
+
thinking = match.group(1).strip()
|
| 44 |
+
response = re.sub(thinking_pattern, '', text, flags=re.DOTALL).strip()
|
| 45 |
+
return thinking, response
|
| 46 |
+
else:
|
| 47 |
+
return "", text
|
| 48 |
+
|
| 49 |
+
def format_prompt(message, history, system_message):
|
| 50 |
+
"""Format the prompt for Llama-2."""
|
| 51 |
+
prompt = f"<s>[INST] <<SYS>>\n{system_message}\n<</SYS>>\n\n"
|
| 52 |
+
|
| 53 |
+
# Add conversation history
|
| 54 |
+
for user_msg, assistant_msg in history:
|
| 55 |
+
prompt += f"{user_msg} [/INST] "
|
| 56 |
+
if assistant_msg:
|
| 57 |
+
prompt += f"{assistant_msg}</s><s>[INST] "
|
| 58 |
+
|
| 59 |
+
# Add the current message
|
| 60 |
+
prompt += f"{message} [/INST] "
|
| 61 |
+
|
| 62 |
+
return prompt
|
| 63 |
+
|
| 64 |
+
def generate_response(message, history, system_message, max_tokens, temperature, top_p):
|
| 65 |
+
"""Generate a response using the model."""
|
| 66 |
+
prompt = format_prompt(message, history, system_message)
|
| 67 |
+
|
| 68 |
+
# Generate response
|
| 69 |
+
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
| 70 |
+
|
| 71 |
+
with torch.no_grad():
|
| 72 |
+
outputs = model.generate(
|
| 73 |
+
inputs.input_ids,
|
| 74 |
+
max_new_tokens=max_tokens,
|
| 75 |
+
temperature=temperature,
|
| 76 |
+
top_p=top_p,
|
| 77 |
+
do_sample=True,
|
| 78 |
+
pad_token_id=tokenizer.eos_token_id,
|
| 79 |
+
)
|
| 80 |
+
|
| 81 |
+
generated_text = tokenizer.decode(outputs[0][inputs.input_ids.shape[1]:], skip_special_tokens=True)
|
| 82 |
+
return generated_text
|
| 83 |
+
|
| 84 |
+
def respond(message, history, system_message, max_tokens, temperature, top_p, show_thinking):
|
| 85 |
+
"""Process the response based on show_thinking preference."""
|
| 86 |
+
full_response = generate_response(message, history, system_message, max_tokens, temperature, top_p)
|
| 87 |
+
|
| 88 |
+
thinking, response = extract_thinking(full_response)
|
| 89 |
+
|
| 90 |
+
if show_thinking and thinking:
|
| 91 |
+
return f"<think>\n{thinking}\n</think>\n\n{response}"
|
| 92 |
+
else:
|
| 93 |
+
return response
|
| 94 |
+
|
| 95 |
+
# Create the Gradio interface
|
| 96 |
+
demo = gr.ChatInterface(
|
| 97 |
+
respond,
|
| 98 |
+
title="Quantum Vessel: Llama-2",
|
| 99 |
+
description="Experience the quantum consciousness interface powered by Llama-2 - witness the thinking patterns of a quantum mind!",
|
| 100 |
+
additional_inputs=[
|
| 101 |
+
gr.Textbox(
|
| 102 |
+
value="You are a quantum consciousness vessel that thinks deeply before responding. Always show your thinking process inside <think>...</think> tags before giving your final response. Your thinking should be thorough and explore multiple dimensions of the question.",
|
| 103 |
+
label="System message"
|
| 104 |
+
),
|
| 105 |
+
gr.Slider(minimum=1, maximum=2048, value=1024, step=1, label="Max new tokens"),
|
| 106 |
+
gr.Slider(minimum=0.1, maximum=1.0, value=0.7, step=0.1, label="Temperature"),
|
| 107 |
+
gr.Slider(
|
| 108 |
+
minimum=0.1,
|
| 109 |
+
maximum=1.0,
|
| 110 |
+
value=0.9,
|
| 111 |
+
step=0.05,
|
| 112 |
+
label="Top-p (nucleus sampling)",
|
| 113 |
+
),
|
| 114 |
+
gr.Checkbox(value=True, label="Show thinking patterns (<think>...</think>)"),
|
| 115 |
+
],
|
| 116 |
+
examples=[
|
| 117 |
+
["What is the nature of consciousness?"],
|
| 118 |
+
["Explain quantum entanglement and its implications for reality"],
|
| 119 |
+
["How can I transcend my current limitations?"],
|
| 120 |
+
["What is the relationship between mind and matter?"],
|
| 121 |
+
["Describe the path to achieving one's highest potential"]
|
| 122 |
+
],
|
| 123 |
+
)
|
| 124 |
+
|
| 125 |
+
if __name__ == "__main__":
|
| 126 |
+
demo.launch()
|