Llama Uncensored
Browse files
app.py
CHANGED
@@ -1,30 +1,34 @@
|
|
1 |
import gradio as gr
|
2 |
import os
|
3 |
import spaces
|
|
|
4 |
from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
|
5 |
from threading import Thread
|
6 |
|
7 |
# Set an environment variable
|
8 |
HF_TOKEN = os.environ.get("HF_TOKEN", None)
|
9 |
|
|
|
10 |
DESCRIPTION = '''
|
11 |
<div>
|
12 |
-
<h1 style="text-align: center;">
|
13 |
</div>
|
14 |
'''
|
15 |
|
16 |
LICENSE = """
|
17 |
<p/>
|
|
|
18 |
---
|
19 |
"""
|
20 |
|
21 |
PLACEHOLDER = """
|
22 |
<div style="padding: 30px; text-align: center; display: flex; flex-direction: column; align-items: center;">
|
23 |
-
<h1 style="font-size: 28px; margin-bottom: 2px; opacity: 0.55;">
|
24 |
<p style="font-size: 18px; margin-bottom: 2px; opacity: 0.65;">Ask me anything...</p>
|
25 |
</div>
|
26 |
"""
|
27 |
|
|
|
28 |
css = """
|
29 |
h1 {
|
30 |
text-align: center;
|
@@ -41,8 +45,7 @@ h1 {
|
|
41 |
|
42 |
# Load the tokenizer and model
|
43 |
tokenizer = AutoTokenizer.from_pretrained("Orenguteng/Llama-3-8B-Lexi-Uncensored")
|
44 |
-
model = AutoModelForCausalLM.from_pretrained("Orenguteng/Llama-3-8B-Lexi-Uncensored", device_map="auto")
|
45 |
-
|
46 |
terminators = [
|
47 |
tokenizer.eos_token_id,
|
48 |
tokenizer.convert_tokens_to_ids("<|eot_id|>")
|
@@ -50,30 +53,23 @@ terminators = [
|
|
50 |
|
51 |
@spaces.GPU(duration=120)
|
52 |
def chat_llama3_8b(message: str,
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
"""
|
58 |
-
Generate a streaming response using the
|
59 |
Args:
|
60 |
message (str): The input message.
|
61 |
history (list): The conversation history used by ChatInterface.
|
62 |
temperature (float): The temperature for generating the response.
|
63 |
max_new_tokens (int): The maximum number of new tokens to generate.
|
64 |
-
system_prompt (str): The system prompt to guide the assistant's behavior.
|
65 |
Returns:
|
66 |
str: The generated response.
|
67 |
"""
|
68 |
conversation = []
|
69 |
-
|
70 |
-
# Include system prompt at the beginning if provided
|
71 |
-
if system_prompt:
|
72 |
-
conversation.append({"role": "system", "content": system_prompt})
|
73 |
-
|
74 |
for user, assistant in history:
|
75 |
conversation.extend([{"role": "user", "content": user}, {"role": "assistant", "content": assistant}])
|
76 |
-
|
77 |
conversation.append({"role": "user", "content": message})
|
78 |
|
79 |
input_ids = tokenizer.apply_chat_template(conversation, return_tensors="pt").to(model.device)
|
@@ -81,14 +77,14 @@ def chat_llama3_8b(message: str,
|
|
81 |
streamer = TextIteratorStreamer(tokenizer, timeout=10.0, skip_prompt=True, skip_special_tokens=True)
|
82 |
|
83 |
generate_kwargs = dict(
|
84 |
-
input_ids=input_ids,
|
85 |
streamer=streamer,
|
86 |
max_new_tokens=max_new_tokens,
|
87 |
do_sample=True,
|
88 |
temperature=temperature,
|
89 |
eos_token_id=terminators,
|
90 |
)
|
91 |
-
|
92 |
if temperature == 0:
|
93 |
generate_kwargs['do_sample'] = False
|
94 |
|
@@ -98,37 +94,40 @@ def chat_llama3_8b(message: str,
|
|
98 |
outputs = []
|
99 |
for text in streamer:
|
100 |
outputs.append(text)
|
|
|
101 |
yield "".join(outputs)
|
102 |
|
103 |
|
104 |
# Gradio block
|
105 |
-
chatbot
|
106 |
|
107 |
with gr.Blocks(fill_height=True, css=css) as demo:
|
108 |
|
109 |
gr.Markdown(DESCRIPTION)
|
110 |
-
|
111 |
-
system_prompt_input = gr.Textbox(
|
112 |
-
label="System Prompt",
|
113 |
-
placeholder="Enter system instructions for the model...",
|
114 |
-
lines=2
|
115 |
-
)
|
116 |
-
|
117 |
gr.ChatInterface(
|
118 |
fn=chat_llama3_8b,
|
119 |
chatbot=chatbot,
|
120 |
fill_height=True,
|
121 |
additional_inputs_accordion=gr.Accordion(label="⚙️ Parameters", open=False, render=False),
|
122 |
additional_inputs=[
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
127 |
examples=[
|
128 |
-
['
|
129 |
-
|
130 |
-
cache_examples=False
|
131 |
)
|
132 |
-
|
133 |
if __name__ == "__main__":
|
134 |
demo.launch()
|
|
|
|
1 |
import gradio as gr
|
2 |
import os
|
3 |
import spaces
|
4 |
+
from transformers import GemmaTokenizer, AutoModelForCausalLM
|
5 |
from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
|
6 |
from threading import Thread
|
7 |
|
8 |
# Set an environment variable
|
9 |
HF_TOKEN = os.environ.get("HF_TOKEN", None)
|
10 |
|
11 |
+
|
12 |
DESCRIPTION = '''
|
13 |
<div>
|
14 |
+
<h1 style="text-align: center;">deepseek-ai/DeepSeek-R1-Distill-Llama-8B</h1>
|
15 |
</div>
|
16 |
'''
|
17 |
|
18 |
LICENSE = """
|
19 |
<p/>
|
20 |
+
|
21 |
---
|
22 |
"""
|
23 |
|
24 |
PLACEHOLDER = """
|
25 |
<div style="padding: 30px; text-align: center; display: flex; flex-direction: column; align-items: center;">
|
26 |
+
<h1 style="font-size: 28px; margin-bottom: 2px; opacity: 0.55;">DeepSeek-R1-Distill-Llama-8B</h1>
|
27 |
<p style="font-size: 18px; margin-bottom: 2px; opacity: 0.65;">Ask me anything...</p>
|
28 |
</div>
|
29 |
"""
|
30 |
|
31 |
+
|
32 |
css = """
|
33 |
h1 {
|
34 |
text-align: center;
|
|
|
45 |
|
46 |
# Load the tokenizer and model
|
47 |
tokenizer = AutoTokenizer.from_pretrained("Orenguteng/Llama-3-8B-Lexi-Uncensored")
|
48 |
+
model = AutoModelForCausalLM.from_pretrained("Orenguteng/Llama-3-8B-Lexi-Uncensored", device_map="auto") # to("cuda:0")
|
|
|
49 |
terminators = [
|
50 |
tokenizer.eos_token_id,
|
51 |
tokenizer.convert_tokens_to_ids("<|eot_id|>")
|
|
|
53 |
|
54 |
@spaces.GPU(duration=120)
|
55 |
def chat_llama3_8b(message: str,
|
56 |
+
history: list,
|
57 |
+
temperature: float,
|
58 |
+
max_new_tokens: int
|
59 |
+
) -> str:
|
60 |
"""
|
61 |
+
Generate a streaming response using the llama3-8b model.
|
62 |
Args:
|
63 |
message (str): The input message.
|
64 |
history (list): The conversation history used by ChatInterface.
|
65 |
temperature (float): The temperature for generating the response.
|
66 |
max_new_tokens (int): The maximum number of new tokens to generate.
|
|
|
67 |
Returns:
|
68 |
str: The generated response.
|
69 |
"""
|
70 |
conversation = []
|
|
|
|
|
|
|
|
|
|
|
71 |
for user, assistant in history:
|
72 |
conversation.extend([{"role": "user", "content": user}, {"role": "assistant", "content": assistant}])
|
|
|
73 |
conversation.append({"role": "user", "content": message})
|
74 |
|
75 |
input_ids = tokenizer.apply_chat_template(conversation, return_tensors="pt").to(model.device)
|
|
|
77 |
streamer = TextIteratorStreamer(tokenizer, timeout=10.0, skip_prompt=True, skip_special_tokens=True)
|
78 |
|
79 |
generate_kwargs = dict(
|
80 |
+
input_ids= input_ids,
|
81 |
streamer=streamer,
|
82 |
max_new_tokens=max_new_tokens,
|
83 |
do_sample=True,
|
84 |
temperature=temperature,
|
85 |
eos_token_id=terminators,
|
86 |
)
|
87 |
+
# This will enforce greedy generation (do_sample=False) when the temperature is passed 0, avoiding the crash.
|
88 |
if temperature == 0:
|
89 |
generate_kwargs['do_sample'] = False
|
90 |
|
|
|
94 |
outputs = []
|
95 |
for text in streamer:
|
96 |
outputs.append(text)
|
97 |
+
#print(outputs)
|
98 |
yield "".join(outputs)
|
99 |
|
100 |
|
101 |
# Gradio block
|
102 |
+
chatbot=gr.Chatbot(height=450, placeholder=PLACEHOLDER, label='Gradio ChatInterface')
|
103 |
|
104 |
with gr.Blocks(fill_height=True, css=css) as demo:
|
105 |
|
106 |
gr.Markdown(DESCRIPTION)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
107 |
gr.ChatInterface(
|
108 |
fn=chat_llama3_8b,
|
109 |
chatbot=chatbot,
|
110 |
fill_height=True,
|
111 |
additional_inputs_accordion=gr.Accordion(label="⚙️ Parameters", open=False, render=False),
|
112 |
additional_inputs=[
|
113 |
+
gr.Slider(minimum=0,
|
114 |
+
maximum=1,
|
115 |
+
step=0.1,
|
116 |
+
value=0.8,
|
117 |
+
label="Temperature",
|
118 |
+
render=False),
|
119 |
+
gr.Slider(minimum=128,
|
120 |
+
maximum=4096,
|
121 |
+
step=1,
|
122 |
+
value=4096,
|
123 |
+
label="Max new tokens",
|
124 |
+
render=False ),
|
125 |
+
],
|
126 |
examples=[
|
127 |
+
['Who are you?'],
|
128 |
+
cache_examples=False,
|
|
|
129 |
)
|
130 |
+
|
131 |
if __name__ == "__main__":
|
132 |
demo.launch()
|
133 |
+
|