Update app.py
Browse files
app.py
CHANGED
|
@@ -1,64 +1,82 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
|
|
|
|
| 3 |
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
"""
|
| 7 |
-
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
| 8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
def
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
)
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
-
|
| 21 |
-
if val[0]:
|
| 22 |
-
messages.append({"role": "user", "content": val[0]})
|
| 23 |
-
if val[1]:
|
| 24 |
-
messages.append({"role": "assistant", "content": val[1]})
|
| 25 |
-
|
| 26 |
-
messages.append({"role": "user", "content": message})
|
| 27 |
-
|
| 28 |
-
response = ""
|
| 29 |
-
|
| 30 |
-
for message in client.chat_completion(
|
| 31 |
-
messages,
|
| 32 |
-
max_tokens=max_tokens,
|
| 33 |
-
stream=True,
|
| 34 |
-
temperature=temperature,
|
| 35 |
-
top_p=top_p,
|
| 36 |
-
):
|
| 37 |
-
token = message.choices[0].delta.content
|
| 38 |
-
|
| 39 |
-
response += token
|
| 40 |
-
yield response
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
"""
|
| 44 |
-
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
| 45 |
-
"""
|
| 46 |
demo = gr.ChatInterface(
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
],
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
)
|
| 61 |
|
| 62 |
-
|
| 63 |
if __name__ == "__main__":
|
| 64 |
-
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
from transformers import pipeline
|
| 4 |
|
| 5 |
+
# Global variable to store the model
|
| 6 |
+
pipe = None
|
|
|
|
|
|
|
| 7 |
|
| 8 |
+
def load_model():
|
| 9 |
+
"""Load the Atlas-Chat model"""
|
| 10 |
+
global pipe
|
| 11 |
+
if pipe is None:
|
| 12 |
+
print("🏔️ Loading Atlas-Chat-2B model...")
|
| 13 |
+
pipe = pipeline(
|
| 14 |
+
"text-generation",
|
| 15 |
+
model="MBZUAI-Paris/Atlas-Chat-2B",
|
| 16 |
+
model_kwargs={"torch_dtype": torch.bfloat16},
|
| 17 |
+
device="cuda" if torch.cuda.is_available() else "cpu"
|
| 18 |
+
)
|
| 19 |
+
print("✅ Model loaded successfully!")
|
| 20 |
+
return pipe
|
| 21 |
|
| 22 |
+
def chat_with_atlas(message, history):
|
| 23 |
+
"""Generate response from Atlas-Chat model"""
|
| 24 |
+
if not message.strip():
|
| 25 |
+
return "مرحبا! أهلا وسهلا. Please enter a message!"
|
| 26 |
+
|
| 27 |
+
try:
|
| 28 |
+
# Load model if not already loaded
|
| 29 |
+
model = load_model()
|
| 30 |
+
|
| 31 |
+
# Prepare the message
|
| 32 |
+
messages = [{"role": "user", "content": message}]
|
| 33 |
+
|
| 34 |
+
# Generate response
|
| 35 |
+
outputs = model(
|
| 36 |
+
messages,
|
| 37 |
+
max_new_tokens=256,
|
| 38 |
+
temperature=0.1,
|
| 39 |
+
do_sample=True,
|
| 40 |
+
pad_token_id=model.tokenizer.eos_token_id
|
| 41 |
+
)
|
| 42 |
+
|
| 43 |
+
# Extract the response
|
| 44 |
+
response = outputs[0]["generated_text"][-1]["content"].strip()
|
| 45 |
+
return response
|
| 46 |
+
|
| 47 |
+
except Exception as e:
|
| 48 |
+
return f"عذراً، واجهت خطأ: {str(e)}. جرب مرة أخرى!"
|
| 49 |
|
| 50 |
+
# Create the Gradio interface
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
demo = gr.ChatInterface(
|
| 52 |
+
fn=chat_with_atlas,
|
| 53 |
+
title="🏔️ Atlas-Chat: Moroccan Arabic AI Assistant",
|
| 54 |
+
description="""
|
| 55 |
+
**مرحبا بك في أطلس شات!** Welcome to Atlas-Chat! 🇲🇦
|
| 56 |
+
|
| 57 |
+
I'm an AI assistant specialized in **Moroccan Arabic (Darija)** and English.
|
| 58 |
+
Ask me questions about Morocco, culture, or just have a chat!
|
| 59 |
+
|
| 60 |
+
**جرب هذه الأسئلة / Try these questions:**
|
| 61 |
+
""",
|
| 62 |
+
examples=[
|
| 63 |
+
"شكون لي صنعك؟",
|
| 64 |
+
"اشنو هو الطاجين؟",
|
| 65 |
+
"شنو كيتسمى المنتخب المغربي؟",
|
| 66 |
+
"What is Morocco famous for?",
|
| 67 |
+
"Tell me about Casablanca",
|
| 68 |
+
"كيفاش نقدر نتعلم الدارجة؟"
|
| 69 |
],
|
| 70 |
+
cache_examples=False,
|
| 71 |
+
retry_btn="🔄 جرب مرة أخرى",
|
| 72 |
+
undo_btn="↶ تراجع",
|
| 73 |
+
clear_btn="🗑️ امسح الكل",
|
| 74 |
+
theme=gr.themes.Soft(
|
| 75 |
+
primary_hue="blue",
|
| 76 |
+
secondary_hue="green"
|
| 77 |
+
)
|
| 78 |
)
|
| 79 |
|
| 80 |
+
# Launch the app
|
| 81 |
if __name__ == "__main__":
|
| 82 |
+
demo.launch()
|