PitterTMYT commited on
Commit
544984e
·
verified ·
1 Parent(s): c96c3f8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -49
app.py CHANGED
@@ -1,70 +1,61 @@
 
1
  import transformers
2
  import torch
3
 
 
4
  model_id = "yodayo-ai/nephra_v1.0"
5
 
6
- pipeline = transformers.pipeline(
7
- "text-generation",
8
- model=model_id,
9
- model_kwargs={"torch_dtype": torch.bfloat16},
10
  device_map="auto",
11
- offload_folder="offload", # Only provided here during model initialization
12
  )
13
 
14
- # Define characters and traits
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
- def generate_response(character_name, user_input, max_length=100, temperature=0.7, top_p=0.85, repetition_penalty=1.1):
30
- # Find the character data
31
- character = next((c for c in characters if c["name"] == character_name), None)
32
- if not character:
33
- return "Character not found."
 
34
 
35
- # Create the prompt text
36
- prompt_text = (f"You are {character_name}, {character['description']}. Traits: {character['traits']}. "
37
- f"In response to the question '{user_input}', respond {random.choice(['inspired', 'with doubt', 'joyfully', 'thoughtfully', 'skeptically'])}. Please complete the response.")
 
 
 
 
 
 
 
 
 
38
 
39
- # Generate response
40
  outputs = pipeline(
41
- prompt_text,
42
- max_new_tokens=max_length,
 
 
 
 
43
  do_sample=True,
44
- temperature=temperature,
45
- top_p=top_p,
46
- repetition_penalty=repetition_penalty
47
  )
48
 
49
- return outputs[0]['generated_text']
50
 
51
  # Gradio Interface
52
- import gradio as gr
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="Roleplaying Model Demo",
66
- description="Generate responses based on the chosen character's traits."
67
  )
68
 
69
- if __name__ == "__main__":
70
- iface.launch()
 
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()