AlphaWice commited on
Commit
f536a8a
·
verified ·
1 Parent(s): 33c1bd9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +72 -54
app.py CHANGED
@@ -1,64 +1,82 @@
1
  import gradio as gr
2
- from huggingface_hub import InferenceClient
 
3
 
4
- """
5
- For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
6
- """
7
- client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
 
10
- def respond(
11
- message,
12
- history: list[tuple[str, str]],
13
- system_message,
14
- max_tokens,
15
- temperature,
16
- top_p,
17
- ):
18
- messages = [{"role": "system", "content": system_message}]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
 
20
- for val in history:
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
- respond,
48
- additional_inputs=[
49
- gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
50
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
51
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
52
- gr.Slider(
53
- minimum=0.1,
54
- maximum=1.0,
55
- value=0.95,
56
- step=0.05,
57
- label="Top-p (nucleus sampling)",
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()