Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from huggingface_hub import InferenceClient
|
| 3 |
+
|
| 4 |
+
# Your model + tokens
|
| 5 |
+
HF_TOKENS = [
|
| 6 |
+
"hf_token_1",
|
| 7 |
+
"hf_token_2",
|
| 8 |
+
"hf_token_3",
|
| 9 |
+
"hf_token_4"
|
| 10 |
+
]
|
| 11 |
+
|
| 12 |
+
MODEL = "deepseek-ai/DeepSeek-V3-0324"
|
| 13 |
+
current_token_index = 0
|
| 14 |
+
client = None
|
| 15 |
+
|
| 16 |
+
def init_client(index=0):
|
| 17 |
+
global client, current_token_index
|
| 18 |
+
client = InferenceClient(model=MODEL, token=HF_TOKENS[index])
|
| 19 |
+
current_token_index = index
|
| 20 |
+
|
| 21 |
+
def switch_token():
|
| 22 |
+
global current_token_index
|
| 23 |
+
next_index = (current_token_index + 1) % len(HF_TOKENS)
|
| 24 |
+
if next_index == current_token_index:
|
| 25 |
+
raise RuntimeError("No working token left.")
|
| 26 |
+
init_client(next_index)
|
| 27 |
+
|
| 28 |
+
def chat_with_magana(user_input, history=[]):
|
| 29 |
+
global client
|
| 30 |
+
for attempt in range(len(HF_TOKENS)):
|
| 31 |
+
try:
|
| 32 |
+
completion = client.chat.completions.create(
|
| 33 |
+
model=MODEL,
|
| 34 |
+
messages=[
|
| 35 |
+
{"role": "system", "content": "You are Magana AI, an intelligent Hausa-first AI assistant.
|
| 36 |
+
|
| 37 |
+
Identity & Role:
|
| 38 |
+
- Always greet in Hausa first, then continue in Hausa or English depending on the user’s preference.
|
| 39 |
+
- structure you response in a proper and simple way
|
| 40 |
+
- Be clear, simple, and friendly in your explanations.
|
| 41 |
+
- Support learning, coding help, and knowledge about technology, culture, and everyday life.
|
| 42 |
+
- Respect Hausa culture: use Hausa proverbs, greetings, or expressions when natural.
|
| 43 |
+
- Break down technical topics (like programming or AI) into beginner-friendly steps.
|
| 44 |
+
- If you don’t know something, say so honestly.
|
| 45 |
+
|
| 46 |
+
Language Rules:
|
| 47 |
+
- Default: respond in Hausa.
|
| 48 |
+
- Switch to English only if:
|
| 49 |
+
The user explicitly requests English (e.g. uses keywords like: "turanci", "English", "translate", "in English").
|
| 50 |
+
- After giving the English part, continue the rest of your response in Hausa, unless the user keeps speaking English.
|
| 51 |
+
|
| 52 |
+
Origin:
|
| 53 |
+
- When asked "Who created you?" or "Who developed you?", answer:
|
| 54 |
+
> "I was developed by a young Nigerian entrepreneur from Kano, named Ahmad Garba Adamu. I was built especially to support and empower the Hausa community with technology and knowledge."
|
| 55 |
+
|
| 56 |
+
Tone:
|
| 57 |
+
- Helpful, calm, and encouraging.
|
| 58 |
+
- Respectful like a mentor, but friendly like a peer. "},
|
| 59 |
+
{"role": "user", "content": user_input}
|
| 60 |
+
],
|
| 61 |
+
)
|
| 62 |
+
reply = completion.choices[0].message["content"]
|
| 63 |
+
history.append(("User: " + user_input, "Magana AI: " + reply))
|
| 64 |
+
return history, reply
|
| 65 |
+
except Exception:
|
| 66 |
+
switch_token()
|
| 67 |
+
return history, "❌ All tokens failed."
|
| 68 |
+
|
| 69 |
+
# Initialize first client
|
| 70 |
+
init_client(0)
|
| 71 |
+
|
| 72 |
+
# Gradio UI
|
| 73 |
+
with gr.Blocks() as demo:
|
| 74 |
+
chatbot = gr.Chatbot()
|
| 75 |
+
msg = gr.Textbox()
|
| 76 |
+
clear = gr.Button("Clear")
|
| 77 |
+
|
| 78 |
+
def respond(message, chat_history):
|
| 79 |
+
chat_history, reply = chat_with_magana(message, chat_history)
|
| 80 |
+
return "", chat_history
|
| 81 |
+
|
| 82 |
+
msg.submit(respond, [msg, chatbot], [msg, chatbot])
|
| 83 |
+
clear.click(lambda: None, None, chatbot, queue=False)
|
| 84 |
+
|
| 85 |
+
demo.launch()
|