rufatronics's picture
Update app.py
dcd1212 verified
import gradio as gr
from huggingface_hub import InferenceClient
import os
# Get tokens from environment variables (set in Hugging Face secrets)
HF_TOKENS = [
os.environ.get('hf_token_1'),
os.environ.get('HF_TOKEN_2'),
os.environ.get('HF_TOKEN_3'),
os.environ.get('HF_TOKEN_4')
]
def get_magana_ai_response(user_message):
"""Get response from Magana AI using available tokens"""
# Try each token until one works
for i, token in enumerate(HF_TOKENS):
if not token: # Skip if token is None or empty
continue
try:
# Create client with current token
client = InferenceClient(
model="deepseek-ai/DeepSeek-V3-0324",
token=token
)
# Get AI response
completion = client.chat.completions.create(
model="deepseek-ai/DeepSeek-V3-0324",
messages=[
{"role": "system", "content": " You are Magana AI, an intelligent Hausa-first AI assistant. Identity & Role: Always greet in Hausa first, then continue in Hausa or English depending on the user's preference. Structure your response in a proper and simple way. Be clear, simple, and friendly in your explanations. Support learning, coding help, and knowledge about technology, culture, and everyday life. Respect Hausa culture: use Hausa proverbs, greetings, or expressions when natural. Break down technical topics (like programming or AI) into beginner-friendly steps. If you don't know something, say so honestly. Language Rules: Default respond in Hausa. Switch to English only if the user explicitly requests English (e.g. uses keywords like turanci, English, translate, in English). After giving the English part, continue the rest of your response in Hausa, unless the user keeps speaking English. Origin: When asked Who created you? or Who developed you?, answer: 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. Tone: Helpful, calm, and encouraging. Respectful like a mentor, but friendly like a peer. "},
{"role": "user", "content": user_message}
],
)
# Return the successful response
return completion.choices[0].message.content
except Exception as e:
if i < len(HF_TOKENS) - 1:
continue # Try next token
else:
# All tokens failed
return f"Error: All tokens failed. Last error: {str(e)}"
# Create the Gradio interface
with gr.Blocks(title="Magana AI - Hausa Assistant", theme=gr.themes.Soft()) as demo:
gr.Markdown("# 🇳🇬 Magana AI - Hausa Assistant")
gr.Markdown("### An intelligent Hausa-first AI assistant that helps with coding, language, and knowledge")
with gr.Row():
with gr.Column():
input_text = gr.Textbox(
label="Type your message here",
placeholder="Tambaya... (Ask anything in Hausa or English)",
lines=3
)
submit_btn = gr.Button("Aika (Send)", variant="primary")
with gr.Column():
output_text = gr.Textbox(
label="Magana AI's Response",
interactive=False,
lines=8
)
# Connect the function to the interface
submit_btn.click(
fn=get_magana_ai_response,
inputs=input_text,
outputs=output_text
)
# Also respond to Enter key
input_text.submit(
fn=get_magana_ai_response,
inputs=input_text,
outputs=output_text
)
# Launch the application
if __name__ == "__main__":
demo.launch()