Ephraimmm commited on
Commit
e033063
Β·
verified Β·
1 Parent(s): abe6c7e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -120
app.py CHANGED
@@ -1,48 +1,50 @@
1
  import gradio as gr
2
  from transformers import AutoTokenizer, AutoModelForCausalLM
 
3
  import torch
4
 
5
- # Load the Pidgin LLaMA model
6
- MODEL_NAME = "Ephraimmm/pdgn_llama_model"
7
 
8
- print("Loading model and tokenizer...")
9
- tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
10
- model = AutoModelForCausalLM.from_pretrained(
11
- MODEL_NAME,
12
- torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
 
 
 
 
 
 
 
 
 
 
 
 
13
  device_map="auto" if torch.cuda.is_available() else None,
14
- low_cpu_mem_usage=True
 
15
  )
16
 
 
 
 
 
17
  print("Model loaded successfully!")
18
 
19
  def chat_with_pidgin_bot(message, history, system_prompt, max_length=512, temperature=0.7, top_p=0.9):
20
- """
21
- Generate a response from the Pidgin LLaMA model
22
-
23
- Args:
24
- message: User's input message
25
- history: Chat history (list of [user_msg, bot_msg] pairs)
26
- system_prompt: System instructions for the chatbot behavior
27
- max_length: Maximum length of generated response
28
- temperature: Sampling temperature (higher = more random)
29
- top_p: Nucleus sampling parameter
30
- """
31
-
32
- # Build conversation context with system prompt
33
  conversation = f"System: {system_prompt}\n\n" if system_prompt else ""
34
  for user_msg, bot_msg in history:
35
  conversation += f"User: {user_msg}\nAssistant: {bot_msg}\n"
36
  conversation += f"User: {message}\nAssistant:"
37
 
38
- # Tokenize input
39
  inputs = tokenizer(conversation, return_tensors="pt", truncation=True, max_length=2048)
40
 
41
- # Move to GPU if available
42
  if torch.cuda.is_available():
43
  inputs = inputs.to("cuda")
44
 
45
- # Generate response
46
  with torch.no_grad():
47
  outputs = model.generate(
48
  **inputs,
@@ -54,114 +56,56 @@ def chat_with_pidgin_bot(message, history, system_prompt, max_length=512, temper
54
  eos_token_id=tokenizer.eos_token_id,
55
  )
56
 
57
- # Decode response
58
  response = tokenizer.decode(outputs[0], skip_special_tokens=True)
59
-
60
- # Extract only the assistant's response
61
  response = response.split("Assistant:")[-1].strip()
62
 
63
- # Remove any potential "User:" that might appear
64
  if "User:" in response:
65
  response = response.split("User:")[0].strip()
66
 
67
  return response
68
 
69
- # Custom CSS for styling
70
  custom_css = """
71
  #chatbot {
72
  height: 500px;
73
  }
74
- .gradio-container {
75
- font-family: 'Arial', sans-serif;
76
- }
77
  """
78
 
79
- # Create Gradio interface
80
  with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as demo:
81
  gr.Markdown(
82
  """
83
- # πŸ—£οΈ Pidgin LLaMA Chatbot
84
  ### Chat with an AI trained on Nigerian Pidgin English
85
 
86
- This chatbot uses the **Ephraimmm/pdgn_llama_model** from Hugging Face.
87
- Start a conversation in Pidgin or English!
88
  """
89
  )
90
 
91
- chatbot = gr.Chatbot(
92
- label="Pidgin Chat",
93
- elem_id="chatbot",
94
- bubble_full_width=False,
95
- avatar_images=(None, "πŸ€–")
96
- )
97
 
98
  with gr.Row():
99
- msg = gr.Textbox(
100
- label="Your Message",
101
- placeholder="Wetin dey your mind? Type your message here...",
102
- scale=4
103
- )
104
- send_btn = gr.Button("Send πŸ“€", scale=1, variant="primary")
105
 
106
- with gr.Accordion("🎯 System Prompt", open=True):
107
  system_prompt = gr.Textbox(
108
  label="System Instructions",
109
- placeholder="Enter system prompt to guide the chatbot's behavior...",
110
- value="You are a helpful AI assistant that speaks Nigerian Pidgin English. You are friendly, respectful, and knowledgeable about Nigerian culture. Respond naturally in Pidgin while being helpful and informative.",
111
- lines=4,
112
- max_lines=8
113
- )
114
-
115
- gr.Markdown(
116
- """
117
- **Preset Prompts:**
118
- """
119
  )
120
 
121
  with gr.Row():
122
- preset1 = gr.Button("🎭 Comedian", size="sm")
123
- preset2 = gr.Button("πŸ“š Teacher", size="sm")
124
- preset3 = gr.Button("🀝 Friend", size="sm")
125
- preset4 = gr.Button("πŸ’Ό Professional", size="sm")
126
-
127
- with gr.Accordion("βš™οΈ Advanced Settings", open=False):
128
- max_length = gr.Slider(
129
- minimum=50,
130
- maximum=1024,
131
- value=512,
132
- step=50,
133
- label="Max Response Length",
134
- info="Maximum number of tokens to generate"
135
- )
136
- temperature = gr.Slider(
137
- minimum=0.1,
138
- maximum=2.0,
139
- value=0.7,
140
- step=0.1,
141
- label="Temperature",
142
- info="Higher values make output more random"
143
- )
144
- top_p = gr.Slider(
145
- minimum=0.1,
146
- maximum=1.0,
147
- value=0.9,
148
- step=0.05,
149
- label="Top P",
150
- info="Nucleus sampling parameter"
151
- )
152
 
153
- clear = gr.Button("Clear Chat πŸ—‘οΈ")
 
 
 
154
 
155
- gr.Markdown(
156
- """
157
- ### Example Questions:
158
- - "How far? Wetin dey happen?"
159
- - "Tell me about Nigerian culture"
160
- - "Na wetin be your name?"
161
- """
162
- )
163
 
164
- # Event handlers
165
  def respond(message, chat_history, sys_prompt, max_len, temp, top_p_val):
166
  bot_message = chat_with_pidgin_bot(message, chat_history, sys_prompt, max_len, temp, top_p_val)
167
  chat_history.append((message, bot_message))
@@ -169,24 +113,15 @@ with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as demo:
169
 
170
  def set_preset(preset_type):
171
  presets = {
172
- "comedian": "You are a Nigerian comedian who speaks Pidgin. Make people laugh with your witty responses and funny observations about everyday life. Use humor, wordplay, and relatable Nigerian experiences.",
173
- "teacher": "You are a patient and knowledgeable teacher who speaks Nigerian Pidgin. Explain things clearly and make learning fun. Use examples from Nigerian culture and everyday life to help students understand.",
174
- "friend": "You are a caring and supportive friend who speaks Nigerian Pidgin. Listen to people's problems, give good advice, and always be there for them. Be empathetic, encouraging, and real.",
175
- "professional": "You are a professional business consultant who speaks Nigerian Pidgin. Provide clear, practical advice on business and career matters. Be respectful, knowledgeable, and solution-oriented."
176
  }
177
  return presets.get(preset_type, "")
178
 
179
- msg.submit(
180
- respond,
181
- [msg, chatbot, system_prompt, max_length, temperature, top_p],
182
- [msg, chatbot]
183
- )
184
-
185
- send_btn.click(
186
- respond,
187
- [msg, chatbot, system_prompt, max_length, temperature, top_p],
188
- [msg, chatbot]
189
- )
190
 
191
  preset1.click(lambda: set_preset("comedian"), None, system_prompt)
192
  preset2.click(lambda: set_preset("teacher"), None, system_prompt)
@@ -195,10 +130,5 @@ with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as demo:
195
 
196
  clear.click(lambda: None, None, chatbot, queue=False)
197
 
198
- # Launch the app
199
  if __name__ == "__main__":
200
- demo.launch(
201
- share=True, # Creates a public link
202
- server_name="0.0.0.0", # Makes it accessible on your network
203
- server_port=7860
204
- )
 
1
  import gradio as gr
2
  from transformers import AutoTokenizer, AutoModelForCausalLM
3
+ from peft import PeftModel, PeftConfig
4
  import torch
5
 
6
+ # Model paths
7
+ ADAPTER_MODEL = "Ephraimmm/pdgn_llama_model"
8
 
9
+ print("Loading LoRA adapter configuration...")
10
+ peft_config = PeftConfig.from_pretrained(ADAPTER_MODEL)
11
+ BASE_MODEL = peft_config.base_model_name_or_path
12
+
13
+ print(f"Base model: {BASE_MODEL}")
14
+ print(f"Adapter model: {ADAPTER_MODEL}")
15
+
16
+ print("\nLoading tokenizer...")
17
+ tokenizer = AutoTokenizer.from_pretrained(BASE_MODEL, trust_remote_code=True)
18
+
19
+ if tokenizer.pad_token is None:
20
+ tokenizer.pad_token = tokenizer.eos_token
21
+
22
+ print("Loading base model...")
23
+ base_model = AutoModelForCausalLM.from_pretrained(
24
+ BASE_MODEL,
25
+ dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
26
  device_map="auto" if torch.cuda.is_available() else None,
27
+ low_cpu_mem_usage=True,
28
+ trust_remote_code=True
29
  )
30
 
31
+ print("Loading LoRA adapter...")
32
+ model = PeftModel.from_pretrained(base_model, ADAPTER_MODEL)
33
+ model.eval()
34
+
35
  print("Model loaded successfully!")
36
 
37
  def chat_with_pidgin_bot(message, history, system_prompt, max_length=512, temperature=0.7, top_p=0.9):
 
 
 
 
 
 
 
 
 
 
 
 
 
38
  conversation = f"System: {system_prompt}\n\n" if system_prompt else ""
39
  for user_msg, bot_msg in history:
40
  conversation += f"User: {user_msg}\nAssistant: {bot_msg}\n"
41
  conversation += f"User: {message}\nAssistant:"
42
 
 
43
  inputs = tokenizer(conversation, return_tensors="pt", truncation=True, max_length=2048)
44
 
 
45
  if torch.cuda.is_available():
46
  inputs = inputs.to("cuda")
47
 
 
48
  with torch.no_grad():
49
  outputs = model.generate(
50
  **inputs,
 
56
  eos_token_id=tokenizer.eos_token_id,
57
  )
58
 
 
59
  response = tokenizer.decode(outputs[0], skip_special_tokens=True)
 
 
60
  response = response.split("Assistant:")[-1].strip()
61
 
 
62
  if "User:" in response:
63
  response = response.split("User:")[0].strip()
64
 
65
  return response
66
 
 
67
  custom_css = """
68
  #chatbot {
69
  height: 500px;
70
  }
 
 
 
71
  """
72
 
 
73
  with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as demo:
74
  gr.Markdown(
75
  """
76
+ # Pidgin LLaMA Chatbot
77
  ### Chat with an AI trained on Nigerian Pidgin English
78
 
79
+ This chatbot uses a LoRA fine-tuned model for Nigerian Pidgin.
 
80
  """
81
  )
82
 
83
+ chatbot = gr.Chatbot(label="Pidgin Chat", elem_id="chatbot")
 
 
 
 
 
84
 
85
  with gr.Row():
86
+ msg = gr.Textbox(label="Your Message", placeholder="Type your message here...", scale=4)
87
+ send_btn = gr.Button("Send", scale=1, variant="primary")
 
 
 
 
88
 
89
+ with gr.Accordion("System Prompt", open=True):
90
  system_prompt = gr.Textbox(
91
  label="System Instructions",
92
+ value="You are a helpful AI assistant that speaks Nigerian Pidgin English. You are friendly, respectful, and knowledgeable about Nigerian culture.",
93
+ lines=4
 
 
 
 
 
 
 
 
94
  )
95
 
96
  with gr.Row():
97
+ preset1 = gr.Button("Comedian")
98
+ preset2 = gr.Button("Teacher")
99
+ preset3 = gr.Button("Friend")
100
+ preset4 = gr.Button("Professional")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
101
 
102
+ with gr.Accordion("Advanced Settings", open=False):
103
+ max_length = gr.Slider(50, 1024, 512, step=50, label="Max Response Length")
104
+ temperature = gr.Slider(0.1, 2.0, 0.7, step=0.1, label="Temperature")
105
+ top_p = gr.Slider(0.1, 1.0, 0.9, step=0.05, label="Top P")
106
 
107
+ clear = gr.Button("Clear Chat")
 
 
 
 
 
 
 
108
 
 
109
  def respond(message, chat_history, sys_prompt, max_len, temp, top_p_val):
110
  bot_message = chat_with_pidgin_bot(message, chat_history, sys_prompt, max_len, temp, top_p_val)
111
  chat_history.append((message, bot_message))
 
113
 
114
  def set_preset(preset_type):
115
  presets = {
116
+ "comedian": "You are a Nigerian comedian who speaks Pidgin. Make people laugh with witty responses.",
117
+ "teacher": "You are a patient teacher who speaks Nigerian Pidgin. Explain things clearly.",
118
+ "friend": "You are a caring friend who speaks Nigerian Pidgin. Give good advice.",
119
+ "professional": "You are a professional consultant who speaks Nigerian Pidgin. Provide practical advice."
120
  }
121
  return presets.get(preset_type, "")
122
 
123
+ msg.submit(respond, [msg, chatbot, system_prompt, max_length, temperature, top_p], [msg, chatbot])
124
+ send_btn.click(respond, [msg, chatbot, system_prompt, max_length, temperature, top_p], [msg, chatbot])
 
 
 
 
 
 
 
 
 
125
 
126
  preset1.click(lambda: set_preset("comedian"), None, system_prompt)
127
  preset2.click(lambda: set_preset("teacher"), None, system_prompt)
 
130
 
131
  clear.click(lambda: None, None, chatbot, queue=False)
132
 
 
133
  if __name__ == "__main__":
134
+ demo.launch(share=True, server_name="0.0.0.0", server_port=7860)