Stanley03 commited on
Commit
3b2416f
·
verified ·
1 Parent(s): 6bc8b68

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +266 -0
app.py ADDED
@@ -0,0 +1,266 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # =====================
2
+ # 🦁 SIMBA AI - First African LLM
3
+ # =====================
4
+
5
+ import gradio as gr
6
+ import torch
7
+ from transformers import AutoModelForCausalLM, AutoTokenizer
8
+ from sentence_transformers import SentenceTransformer
9
+ import faiss
10
+ import numpy as np
11
+ import os
12
+
13
+ print("🚀 Initializing Simba AI - First African LLM...")
14
+
15
+ # =====================
16
+ # LOAD AI MODEL
17
+ # =====================
18
+
19
+ model_name = "mistralai/Mistral-7B-Instruct-v0.2"
20
+
21
+ try:
22
+ # Load tokenizer and model
23
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
24
+ tokenizer.pad_token = tokenizer.eos_token
25
+
26
+ model = AutoModelForCausalLM.from_pretrained(
27
+ model_name,
28
+ torch_dtype=torch.float16,
29
+ device_map="auto",
30
+ )
31
+ print("✅ Simba AI Model Loaded Successfully!")
32
+ except Exception as e:
33
+ print(f"❌ Model loading error: {e}")
34
+ # Fallback to smaller model if needed
35
+ model_name = "microsoft/DialoGPT-large"
36
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
37
+ tokenizer.pad_token = tokenizer.eos_token
38
+ model = AutoModelForCausalLM.from_pretrained(model_name)
39
+ print("✅ Fallback model loaded!")
40
+
41
+ # =====================
42
+ # AFRICAN KNOWLEDGE BASE
43
+ # =====================
44
+
45
+ simba_knowledge_base = [
46
+ # CODING
47
+ {"question": "Python add function", "answer": "def add(a, b): return a + b"},
48
+ {"question": "Factorial function", "answer": "def factorial(n): return 1 if n == 0 else n * factorial(n-1)"},
49
+ {"question": "Reverse string function", "answer": "def reverse_string(s): return s[::-1]"},
50
+ {"question": "Check even number", "answer": "def is_even(n): return n % 2 == 0"},
51
+ {"question": "Multiply function", "answer": "def multiply(x, y): return x * y"},
52
+ {"question": "Yoruba greeting function", "answer": "def yoruba_greeting(): return 'Báwo ni'"},
53
+
54
+ # MATH
55
+ {"question": "15 + 27", "answer": "42"},
56
+ {"question": "8 × 7", "answer": "56"},
57
+ {"question": "100 - 45", "answer": "55"},
58
+ {"question": "12 × 12", "answer": "144"},
59
+ {"question": "25% of 200", "answer": "50"},
60
+
61
+ # YORUBA
62
+ {"question": "Hello in Yoruba", "answer": "Báwo ni"},
63
+ {"question": "Thank you in Yoruba", "answer": "Ẹ sé"},
64
+ {"question": "How are you in Yoruba", "answer": "Ṣe daadaa ni"},
65
+ {"question": "Good morning in Yoruba", "answer": "Ẹ káàrọ̀"},
66
+ {"question": "Good night in Yoruba", "answer": "O dàárọ̀"},
67
+ {"question": "Please in Yoruba", "answer": "Jọ̀wọ́"},
68
+
69
+ # SWAHILI
70
+ {"question": "Hello in Swahili", "answer": "Hujambo"},
71
+ {"question": "Thank you in Swahili", "answer": "Asante"},
72
+
73
+ # IGBO
74
+ {"question": "Hello in Igbo", "answer": "Nnọọ"},
75
+ {"question": "Thank you in Igbo", "answer": "Daalụ"},
76
+
77
+ # HAUSA
78
+ {"question": "Hello in Hausa", "answer": "Sannu"},
79
+ {"question": "Thank you in Hausa", "answer": "Na gode"},
80
+
81
+ # AFRICAN INNOVATION
82
+ {"question": "M-Pesa", "answer": "Mobile money service launched in Kenya in 2007"},
83
+ {"question": "Andela", "answer": "Trains African software developers for global companies"},
84
+ ]
85
+
86
+ print(f"✅ African Knowledge Base: {len(simba_knowledge_base)} entries")
87
+
88
+ # =====================
89
+ # SEARCH SYSTEM
90
+ # =====================
91
+
92
+ try:
93
+ embedder = SentenceTransformer('all-MiniLM-L6-v2')
94
+
95
+ # Build search index
96
+ questions = [item["question"] for item in simba_knowledge_base]
97
+ question_embeddings = embedder.encode(questions)
98
+
99
+ dimension = question_embeddings.shape[1]
100
+ index = faiss.IndexFlatIP(dimension)
101
+ faiss.normalize_L2(question_embeddings)
102
+ index.add(question_embeddings)
103
+
104
+ print("✅ Smart Search System Ready!")
105
+ except Exception as e:
106
+ print(f"❌ Search system error: {e}")
107
+ index = None
108
+
109
+ def simba_search(query, top_k=2):
110
+ """Search African knowledge base"""
111
+ if index is None:
112
+ return simba_knowledge_base[:top_k] # Fallback
113
+
114
+ try:
115
+ query_embedding = embedder.encode([query])
116
+ faiss.normalize_L2(query_embedding)
117
+
118
+ scores, indices = index.search(query_embedding, top_k)
119
+
120
+ results = []
121
+ for i, idx in enumerate(indices[0]):
122
+ if idx < len(simba_knowledge_base):
123
+ results.append({
124
+ "question": simba_knowledge_base[idx]["question"],
125
+ "answer": simba_knowledge_base[idx]["answer"],
126
+ "score": scores[0][i]
127
+ })
128
+
129
+ return results
130
+ except:
131
+ return simba_knowledge_base[:top_k] # Fallback
132
+
133
+ # =====================
134
+ # SIMBA AI CHAT FUNCTION
135
+ # =====================
136
+
137
+ def simba_ai_chat(message, history):
138
+ """Main chat function for Simba AI"""
139
+
140
+ try:
141
+ # Search for relevant knowledge
142
+ search_results = simba_search(message, top_k=2)
143
+
144
+ # Build context
145
+ context = "📚 African Knowledge Reference:\n"
146
+ for i, result in enumerate(search_results, 1):
147
+ context += f"{i}. {result['question']}: {result['answer']}\n"
148
+
149
+ # Build prompt
150
+ prompt = f"""<s>[INST] 🦁 You are SIMBA AI - the First African Large Language Model.
151
+
152
+ You specialize in African languages, coding, mathematics, and African innovation.
153
+
154
+ Use this knowledge:
155
+ {context}
156
+
157
+ Question: {message}
158
+
159
+ Provide an accurate, helpful response that showcases African excellence. [/INST] 🦁 Simba AI:"""
160
+
161
+ # Generate response
162
+ inputs = tokenizer(prompt, return_tensors="pt", truncation=True, max_length=1024).to(model.device)
163
+
164
+ with torch.no_grad():
165
+ outputs = model.generate(
166
+ **inputs,
167
+ max_new_tokens=150,
168
+ temperature=0.7,
169
+ do_sample=True,
170
+ pad_token_id=tokenizer.eos_token_id,
171
+ eos_token_id=tokenizer.eos_token_id,
172
+ )
173
+
174
+ full_response = tokenizer.decode(outputs[0], skip_special_tokens=True)
175
+
176
+ # Extract response
177
+ if "🦁 Simba AI:" in full_response:
178
+ response = full_response.split("🦁 Simba AI:")[-1].strip()
179
+ else:
180
+ response = full_response
181
+
182
+ return response
183
+
184
+ except Exception as e:
185
+ return f"🦁 Simba AI is currently learning... (Error: {str(e)})"
186
+
187
+ # =====================
188
+ # GRADIO INTERFACE
189
+ # =====================
190
+
191
+ # Custom CSS for African theme
192
+ css = """
193
+ .gradio-container {
194
+ font-family: 'Arial', sans-serif;
195
+ }
196
+ .header {
197
+ text-align: center;
198
+ padding: 20px;
199
+ background: linear-gradient(135deg, #ff7e5f, #feb47b);
200
+ color: white;
201
+ border-radius: 10px;
202
+ margin-bottom: 20px;
203
+ }
204
+ """
205
+
206
+ # Create chat interface
207
+ with gr.Blocks(css=css, theme=gr.themes.Soft()) as demo:
208
+
209
+ gr.HTML("""
210
+ <div class="header">
211
+ <h1>🦁 Simba AI - First African LLM</h1>
212
+ <h3>Specializing in African Languages, Coding & Mathematics</h3>
213
+ <p>Ask about Yoruba, Swahili, Igbo, Hausa, Python programming, math problems, and African innovation!</p>
214
+ </div>
215
+ """)
216
+
217
+ chatbot = gr.Chatbot(
218
+ label="🦁 Chat with Simba AI",
219
+ height=500,
220
+ show_copy_button=True,
221
+ placeholder="Ask me anything about African languages, coding, or mathematics..."
222
+ )
223
+
224
+ with gr.Row():
225
+ msg = gr.Textbox(
226
+ label="Your message",
227
+ placeholder="Type your question here...",
228
+ lines=2,
229
+ scale=4
230
+ )
231
+ send_btn = gr.Button("🚀 Ask Simba AI", variant="primary", scale=1)
232
+
233
+ with gr.Row():
234
+ clear_btn = gr.Button("🧹 Clear Chat")
235
+
236
+ # Examples
237
+ gr.Examples(
238
+ examples=[
239
+ "Write a Python function to add two numbers",
240
+ "How do you say hello in Yoruba?",
241
+ "What is 15 + 27?",
242
+ "Create a factorial function",
243
+ "Thank you in Swahili",
244
+ "Calculate 8 × 7",
245
+ "What is M-Pesa?"
246
+ ],
247
+ inputs=msg,
248
+ label="💡 Try these examples:"
249
+ )
250
+
251
+ # Event handlers
252
+ def respond(message, chat_history):
253
+ bot_message = simba_ai_chat(message, chat_history)
254
+ chat_history.append((message, bot_message))
255
+ return "", chat_history
256
+
257
+ msg.submit(respond, [msg, chatbot], [msg, chatbot])
258
+ send_btn.click(respond, [msg, chatbot], [msg, chatbot])
259
+ clear_btn.click(lambda: None, None, chatbot, queue=False)
260
+
261
+ # =====================
262
+ # LAUNCH
263
+ # =====================
264
+
265
+ if __name__ == "__main__":
266
+ demo.launch(debug=True)