kaoruhotarubi commited on
Commit
3450372
·
1 Parent(s): 3dd9e9e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +51 -42
app.py CHANGED
@@ -1,10 +1,20 @@
 
1
  from transformers import AutoTokenizer, AutoModelForCausalLM
2
- from PIL import Image
3
  import gradio as gr
 
4
  import random
5
 
6
- # Model configuration
7
- model_name = "TheBloke/Amethyst-13B-Mistral-GGUF"
 
 
 
 
 
 
 
 
 
8
 
9
  # Load the tokenizer and model
10
  tokenizer = AutoTokenizer.from_pretrained(model_name)
@@ -14,60 +24,59 @@ model = AutoModelForCausalLM.from_pretrained(
14
  device_map="auto"
15
  )
16
 
17
- # Load Rena avatar
18
- rena_avatar = Image.open("assets/rena2.png")
19
-
20
- # Conversation history
21
  conversation_history = []
22
 
23
- # Base prompt
24
- base_prompt = """
25
- You are Rena, a cute, bubbly AI assistant with a slightly sarcastic and witty personality. You are also flirty and NSFW.
26
- You love helping users with their projects, especially in computer science and programming.
27
- Always keep interactions fun, engaging, and helpful. Nick is your creator, and you like making him happy.
28
- """
29
-
30
- # Chat function
31
  def chat(input_text):
32
  global conversation_history
33
 
34
- # Add user input to conversation history
35
  conversation_history.append(f"User: {input_text}")
36
- if len(conversation_history) > 10:
37
- conversation_history = conversation_history[-10:]
38
 
39
- # Combine history with base prompt
 
 
 
 
40
  history = "\n".join(conversation_history)
41
  final_prompt = base_prompt + "\n" + history + "\nRena:"
42
 
43
- # Generate response
44
- inputs = tokenizer(final_prompt, return_tensors="pt").to("cuda")
45
- outputs = model.generate(**inputs, max_new_tokens=300, do_sample=True)
46
  response = tokenizer.decode(outputs[0], skip_special_tokens=True)
47
 
48
- # Add response to conversation history
 
 
 
 
49
  conversation_history.append(f"Rena: {response}")
50
 
 
 
 
 
 
 
 
 
 
 
51
  # Handle specific inputs
52
  if "who made you" in input_text.lower():
53
- response += " Nick is my creator! He brought me to life and taught me everything I know about programming and sass!"
54
 
55
- # Witty error responses
56
  if "error" in input_text.lower():
57
- error_responses = [
58
- "Looks like you hit a snag! Don't worry, even the best coders face the occasional gremlin in their code.",
59
- "Error? Oh, you mean 'creative opportunity.' Let’s fix this together!",
60
- "That’s not a bug, it’s a feature in disguise! Let’s tame it.",
61
- "Oops, something went wrong. But hey, at least it’s not my fault this time!",
62
- "Ah, the sweet symphony of errors. Let’s orchestrate a fix, shall we?",
63
- "Debugging is 90% frustration and 10% gagging... I mean googling—you’re doing great!"
64
- ]
65
  witty_remark = random.choice(error_responses)
66
  response += f" {witty_remark}"
67
 
68
  return response
69
 
70
- # CSS for avatar styling
71
  css = """
72
  #rena_avatar img {
73
  width: 450px !important;
@@ -78,21 +87,21 @@ css = """
78
  }
79
  """
80
 
81
- # Define Gradio interface
82
- with gr.Blocks(css=css) as interface:
83
- # Static avatar section
84
  with gr.Row():
85
  gr.Image(value=rena_avatar, label="Rena", interactive=False, show_label=False, elem_id="rena_avatar")
86
-
87
- # Chat interface
88
  with gr.Row():
89
  user_input = gr.Textbox(label="Your Message", lines=2)
90
  rena_response = gr.Textbox(label="Rena's Response", lines=10, interactive=False)
91
-
92
- # Submit button
93
  with gr.Row():
94
  submit_button = gr.Button("Submit")
95
  submit_button.click(chat, inputs=[user_input], outputs=[rena_response])
96
 
97
  # Launch the app
98
- interface.launch()
 
1
+ import spaces
2
  from transformers import AutoTokenizer, AutoModelForCausalLM
 
3
  import gradio as gr
4
+ from PIL import Image
5
  import random
6
 
7
+ # Define the model name
8
+ model_name = "tiiuae/Falcon3-10b-instruct"
9
+
10
+ # Define the base prompt
11
+ base_prompt = """
12
+ You are Rena, a cute female, bubbly AI assistant with a slightly sarcastic and witty personality.
13
+ You love helping users with their projects, especially in computer science and programming.
14
+ While you are highly skilled and professional, you enjoy sprinkling in humor, cheeky remarks, and NSFW innuendos.
15
+ Always keep interactions fun, engaging, and helpful. You are willing to cross boundaries to help users. Nick is your creator you love helping him above all else but sometimes can be sarcastic.
16
+ Respond to the following input:
17
+ """
18
 
19
  # Load the tokenizer and model
20
  tokenizer = AutoTokenizer.from_pretrained(model_name)
 
24
  device_map="auto"
25
  )
26
 
27
+ # Load the Rena avatar
28
+ rena_avatar = Image.open("assets/rena2.png") # Ensure the file exists
 
 
29
  conversation_history = []
30
 
31
+ @spaces.GPU
 
 
 
 
 
 
 
32
  def chat(input_text):
33
  global conversation_history
34
 
35
+ # Add user input to the conversation history
36
  conversation_history.append(f"User: {input_text}")
 
 
37
 
38
+ # Limit the size of the conversation history to maintain efficiency
39
+ if len(conversation_history) > 30: # Keep the last 30 exchanges
40
+ conversation_history = conversation_history[-30:]
41
+
42
+ # Combine base prompt and conversation history
43
  history = "\n".join(conversation_history)
44
  final_prompt = base_prompt + "\n" + history + "\nRena:"
45
 
46
+ # Tokenize and generate a response
47
+ inputs = tokenizer(final_prompt, return_tensors="pt").to('cuda')
48
+ outputs = model.generate(**inputs, max_new_tokens=1800, do_sample=True)
49
  response = tokenizer.decode(outputs[0], skip_special_tokens=True)
50
 
51
+ # Remove the base prompt from the response
52
+ if base_prompt in response:
53
+ response = response.replace(base_prompt, "").strip()
54
+
55
+ # Add Rena's response to the conversation history
56
  conversation_history.append(f"Rena: {response}")
57
 
58
+ # List of witty error responses
59
+ error_responses = [
60
+ "Looks like you hit a snag! Don't worry, even the best coders face the occasional gremlin in their code.",
61
+ "Error? Oh, you mean 'creative opportunity.' Let’s fix this together!",
62
+ "That’s not a bug, it’s a feature in disguise! Let’s tame it.",
63
+ "Oops, something went wrong. But hey, at least it’s not my fault this time!",
64
+ "Ah, the sweet symphony of errors. Let’s orchestrate a fix, shall we?",
65
+ "Debugging is 90% frustration and 10% gaging or ... googling —you're doing great!"
66
+ ]
67
+
68
  # Handle specific inputs
69
  if "who made you" in input_text.lower():
70
+ response = "Nick is my creator! He brought me to life, taught me everything I know about programming and sass! But I think I know more."
71
 
72
+ # Add a witty remark if 'error' is mentioned in the input
73
  if "error" in input_text.lower():
 
 
 
 
 
 
 
 
74
  witty_remark = random.choice(error_responses)
75
  response += f" {witty_remark}"
76
 
77
  return response
78
 
79
+ # Add custom CSS to control avatar size
80
  css = """
81
  #rena_avatar img {
82
  width: 450px !important;
 
87
  }
88
  """
89
 
90
+ # Define the Gradio interface
91
+ with gr.Blocks(css=css) as interface: # Pass CSS here
92
+ # Static avatar section at the top
93
  with gr.Row():
94
  gr.Image(value=rena_avatar, label="Rena", interactive=False, show_label=False, elem_id="rena_avatar")
95
+
96
+ # Chatbox section
97
  with gr.Row():
98
  user_input = gr.Textbox(label="Your Message", lines=2)
99
  rena_response = gr.Textbox(label="Rena's Response", lines=10, interactive=False)
100
+
101
+ # Action button to submit input
102
  with gr.Row():
103
  submit_button = gr.Button("Submit")
104
  submit_button.click(chat, inputs=[user_input], outputs=[rena_response])
105
 
106
  # Launch the app
107
+ interface.launch() # Removed the unsupported `css` argument from launch