Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,70 +1,61 @@
|
|
|
|
1 |
import transformers
|
2 |
import torch
|
3 |
|
|
|
4 |
model_id = "yodayo-ai/nephra_v1.0"
|
5 |
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
model_kwargs={"torch_dtype": torch.bfloat16},
|
10 |
device_map="auto",
|
11 |
-
offload_folder="offload"
|
12 |
)
|
13 |
|
14 |
-
|
15 |
-
characters = [
|
16 |
-
{"name": "Alex",
|
17 |
-
"description": "Alex is a young and ambitious adventurer, full of energy and a thirst for new discoveries. Always ready to face any challenge, he is driven by a desire to explore uncharted places.",
|
18 |
-
"traits": "brave, energetic, optimistic, determined"},
|
19 |
-
|
20 |
-
{"name": "Maya",
|
21 |
-
"description": "Maya is a wise and experienced sorceress, with deep knowledge in magic and ancient rituals. She is known for her calm demeanor, analytical mind, and ability to find solutions in difficult situations.",
|
22 |
-
"traits": "calm, thoughtful, intuitive, attentive"},
|
23 |
-
|
24 |
-
{"name": "Victor",
|
25 |
-
"description": "Victor is a former warrior who gave up fighting for inner peace and harmony. His life experience and pursuit of justice make him a reliable friend and mentor.",
|
26 |
-
"traits": "serious, thoughtful, fair, balanced"}
|
27 |
-
]
|
28 |
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
|
|
34 |
|
35 |
-
|
36 |
-
|
37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
38 |
|
39 |
-
# Generate response
|
40 |
outputs = pipeline(
|
41 |
-
|
42 |
-
max_new_tokens=
|
|
|
|
|
|
|
|
|
43 |
do_sample=True,
|
44 |
-
temperature=
|
45 |
-
|
46 |
-
repetition_penalty=repetition_penalty
|
47 |
)
|
48 |
|
49 |
-
return outputs[0][
|
50 |
|
51 |
# Gradio Interface
|
52 |
-
|
53 |
-
|
54 |
-
iface = gr.Interface(
|
55 |
fn=generate_response,
|
56 |
-
inputs=
|
57 |
-
gr.Dropdown([c["name"] for c in characters], label="Choose Character"),
|
58 |
-
gr.Textbox(lines=2, placeholder="Enter your text here..."),
|
59 |
-
gr.Slider(20, 200, step=1, value=100, label="Max Length"),
|
60 |
-
gr.Slider(0.1, 1.0, step=0.1, value=0.7, label="Temperature"),
|
61 |
-
gr.Slider(0.1, 1.0, step=0.05, value=0.85, label="Top-p"),
|
62 |
-
gr.Slider(1.0, 2.0, step=0.1, value=1.1, label="Repetition Penalty")
|
63 |
-
],
|
64 |
outputs="text",
|
65 |
-
title="
|
66 |
-
description="
|
67 |
)
|
68 |
|
69 |
-
|
70 |
-
|
|
|
1 |
+
import gradio as gr
|
2 |
import transformers
|
3 |
import torch
|
4 |
|
5 |
+
# Model and pipeline setup
|
6 |
model_id = "yodayo-ai/nephra_v1.0"
|
7 |
|
8 |
+
model = transformers.AutoModelForCausalLM.from_pretrained(
|
9 |
+
model_id,
|
10 |
+
torch_dtype=torch.bfloat16,
|
|
|
11 |
device_map="auto",
|
12 |
+
offload_folder="offload" # Ensure this folder is available or adjust the path
|
13 |
)
|
14 |
|
15 |
+
tokenizer = transformers.AutoTokenizer.from_pretrained(model_id)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
|
17 |
+
pipeline = transformers.pipeline(
|
18 |
+
"text-generation",
|
19 |
+
model=model,
|
20 |
+
tokenizer=tokenizer,
|
21 |
+
device_map="auto",
|
22 |
+
)
|
23 |
|
24 |
+
# Function to generate a response
|
25 |
+
def generate_response(user_input):
|
26 |
+
messages = [
|
27 |
+
{"role": "system", "content": "You are to play the role of a cheerful assistant."},
|
28 |
+
{"role": "user", "content": user_input},
|
29 |
+
]
|
30 |
+
|
31 |
+
prompt = pipeline.tokenizer.apply_chat_template(
|
32 |
+
messages,
|
33 |
+
tokenize=False,
|
34 |
+
add_generation_prompt=True
|
35 |
+
)
|
36 |
|
|
|
37 |
outputs = pipeline(
|
38 |
+
prompt,
|
39 |
+
max_new_tokens=512,
|
40 |
+
eos_token_id=[
|
41 |
+
pipeline.tokenizer.convert_tokens_to_ids(""),
|
42 |
+
pipeline.tokenizer.eos_token_id,
|
43 |
+
],
|
44 |
do_sample=True,
|
45 |
+
temperature=1.12,
|
46 |
+
min_p=0.075,
|
|
|
47 |
)
|
48 |
|
49 |
+
return outputs[0]["generated_text"][len(prompt):]
|
50 |
|
51 |
# Gradio Interface
|
52 |
+
interface = gr.Interface(
|
|
|
|
|
53 |
fn=generate_response,
|
54 |
+
inputs="text",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
55 |
outputs="text",
|
56 |
+
title="Chat with Nephra",
|
57 |
+
description="Interact with the Nephra model, a roleplaying and instruction-based AI.",
|
58 |
)
|
59 |
|
60 |
+
# Launch the Gradio app
|
61 |
+
interface.launch()
|