Ephraimmm commited on
Commit
6404755
Β·
verified Β·
1 Parent(s): 82f76ea

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +204 -0
app.py ADDED
@@ -0,0 +1,204 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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,
49
+ max_new_tokens=max_length,
50
+ temperature=temperature,
51
+ top_p=top_p,
52
+ do_sample=True,
53
+ pad_token_id=tokenizer.eos_token_id,
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))
168
+ return "", chat_history
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)
193
+ preset3.click(lambda: set_preset("friend"), None, system_prompt)
194
+ preset4.click(lambda: set_preset("professional"), None, system_prompt)
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
+ )