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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -51
app.py CHANGED
@@ -1,6 +1,5 @@
1
  import transformers
2
  import torch
3
- import gradio as gr
4
 
5
  model_id = "yodayo-ai/nephra_v1.0"
6
 
@@ -9,81 +8,63 @@ pipeline = transformers.pipeline(
9
  model=model_id,
10
  model_kwargs={"torch_dtype": torch.bfloat16},
11
  device_map="auto",
12
- offload_folder="offload", # Add this line to specify the offload folder
13
  )
14
 
15
- # Define characters
16
  characters = [
17
- {
18
- "name": "Alex",
19
- "description": "Alex is a young and ambitious adventurer, full of energy and eager to discover new places. He is always ready to face any challenges and strives to explore the unknown.",
20
- "traits": "brave, energetic, optimistic, determined"
21
- },
22
- {
23
- "name": "Maya",
24
- "description": "Maya is a wise and experienced sorceress, possessing deep knowledge of magic and ancient rituals. She is known for her calm demeanor, analytical mind, and ability to find solutions in difficult situations.",
25
- "traits": "calm, thoughtful, intuitive, attentive"
26
- },
27
- {
28
- "name": "Victor",
29
- "description": "Victor is a former warrior who left behind his fighting days to seek inner peace and harmony. His life experience and sense of justice make him a reliable friend and mentor.",
30
- "traits": "serious, judicious, fair, balanced"
31
- }
32
  ]
33
 
34
- # Function to generate response
35
- def generate_response(character_name, user_input, max_length=100, temperature=1.12, min_p=0.075, repetition_penalty=1.1):
36
- # Find the character
37
  character = next((c for c in characters if c["name"] == character_name), None)
38
  if not character:
39
  return "Character not found."
40
 
41
- # Prepare the message based on the selected character's personality and description
42
- messages = [
43
- {"role": "system", "content": f"You are {character_name}, {character['description']}. Personality traits: {character['traits']}."},
44
- {"role": "user", "content": user_input},
45
- ]
46
-
47
- # Prepare the prompt using the chat template from the pipeline's tokenizer
48
- prompt = pipeline.tokenizer.apply_chat_template(
49
- messages,
50
- tokenize=False,
51
- add_generation_prompt=True
52
- )
53
 
54
- # Generate response using the pipeline
55
  outputs = pipeline(
56
- prompt,
57
  max_new_tokens=max_length,
58
- eos_token_id=[
59
- pipeline.tokenizer.convert_tokens_to_ids(""),
60
- pipeline.tokenizer.eos_token_id,
61
- ],
62
  do_sample=True,
63
  temperature=temperature,
64
- min_p=min_p,
65
- repetition_penalty=repetition_penalty,
66
  )
67
-
68
- # Extract and return the generated response
69
- generated_text = outputs[0]["generated_text"][len(prompt):].strip()
70
- return generated_text
71
 
72
  # Gradio Interface
 
 
73
  iface = gr.Interface(
74
  fn=generate_response,
75
  inputs=[
76
- gr.Dropdown([c["name"] for c in characters], label="Choose a character"),
77
  gr.Textbox(lines=2, placeholder="Enter your text here..."),
78
  gr.Slider(20, 200, step=1, value=100, label="Max Length"),
79
- gr.Slider(0.1, 1.0, step=0.1, value=1.12, label="Temperature"),
80
- gr.Slider(0.01, 1.0, step=0.01, value=0.075, label="min-p"),
81
  gr.Slider(1.0, 2.0, step=0.1, value=1.1, label="Repetition Penalty")
82
  ],
83
  outputs="text",
84
- title="Nephra v1 LLM Roleplaying Chatbot",
85
- description="Enter a text prompt to generate a response using the Nephra v1 model, based on the selected character."
86
  )
87
 
88
  if __name__ == "__main__":
89
- iface.launch(share=True, debug=True)
 
1
  import transformers
2
  import torch
 
3
 
4
  model_id = "yodayo-ai/nephra_v1.0"
5
 
 
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()