File size: 2,499 Bytes
4f4e064
f536a8a
 
4f4e064
f536a8a
 
4f4e064
f536a8a
 
 
 
 
 
 
 
 
 
 
 
 
4f4e064
f536a8a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4f4e064
f536a8a
4f4e064
f536a8a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4f4e064
f536a8a
 
 
 
 
 
 
 
4f4e064
 
f536a8a
4f4e064
f536a8a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import gradio as gr
import torch
from transformers import pipeline

# Global variable to store the model
pipe = None

def load_model():
    """Load the Atlas-Chat model"""
    global pipe
    if pipe is None:
        print("🏔️ Loading Atlas-Chat-2B model...")
        pipe = pipeline(
            "text-generation",
            model="MBZUAI-Paris/Atlas-Chat-2B",
            model_kwargs={"torch_dtype": torch.bfloat16},
            device="cuda" if torch.cuda.is_available() else "cpu"
        )
        print("✅ Model loaded successfully!")
    return pipe

def chat_with_atlas(message, history):
    """Generate response from Atlas-Chat model"""
    if not message.strip():
        return "مرحبا! أهلا وسهلا. Please enter a message!"
    
    try:
        # Load model if not already loaded
        model = load_model()
        
        # Prepare the message
        messages = [{"role": "user", "content": message}]
        
        # Generate response
        outputs = model(
            messages, 
            max_new_tokens=256, 
            temperature=0.1,
            do_sample=True,
            pad_token_id=model.tokenizer.eos_token_id
        )
        
        # Extract the response
        response = outputs[0]["generated_text"][-1]["content"].strip()
        return response
        
    except Exception as e:
        return f"عذراً، واجهت خطأ: {str(e)}. جرب مرة أخرى!"

# Create the Gradio interface
demo = gr.ChatInterface(
    fn=chat_with_atlas,
    title="🏔️ Atlas-Chat: Moroccan Arabic AI Assistant",
    description="""
    **مرحبا بك في أطلس شات!** Welcome to Atlas-Chat! 🇲🇦
    
    I'm an AI assistant specialized in **Moroccan Arabic (Darija)** and English.
    Ask me questions about Morocco, culture, or just have a chat!
    
    **جرب هذه الأسئلة / Try these questions:**
    """,
    examples=[
        "شكون لي صنعك؟",
        "اشنو هو الطاجين؟", 
        "شنو كيتسمى المنتخب المغربي؟",
        "What is Morocco famous for?",
        "Tell me about Casablanca",
        "كيفاش نقدر نتعلم الدارجة؟"
    ],
    cache_examples=False,
    retry_btn="🔄 جرب مرة أخرى",
    undo_btn="↶ تراجع", 
    clear_btn="🗑️ امسح الكل",
    theme=gr.themes.Soft(
        primary_hue="blue",
        secondary_hue="green"
    )
)

# Launch the app
if __name__ == "__main__":
    demo.launch()