anaspro commited on
Commit
680dfc1
·
1 Parent(s): e2196c4
Files changed (1) hide show
  1. app.py +8 -75
app.py CHANGED
@@ -1,45 +1,20 @@
1
  # -*- coding: utf-8 -*-
2
 
3
- import os
4
  import torch
5
  from transformers import AutoTokenizer, AutoModelForCausalLM
6
- import gradio as gr
7
- import spaces
8
 
9
  model_path = "inceptionai/jais-family-13b-chat"
10
 
11
- # Jais original prompts (مثل الكود الأصلي)
12
  prompt_eng = "### Instruction:Your name is 'Jais', and you are named after Jebel Jais, the highest mountain in UAE. You were made by 'Inception' in the UAE. You are a helpful, respectful, and honest assistant. Always answer as helpfully as possible, while being safe. Complete the conversation between [|Human|] and [|AI|]:\n### Input: [|Human|] {Question}\n[|AI|]\n### Response :"
13
  prompt_ar = "### Instruction:اسمك \"جيس\" وسميت على اسم جبل جيس اعلى جبل في الامارات. تم بنائك بواسطة Inception في الإمارات. أنت مساعد مفيد ومحترم وصادق. أجب دائمًا بأكبر قدر ممكن من المساعدة، مع الحفاظ على البقاء أمناً. أكمل المحادثة بين [|Human|] و[|AI|] :\n### Input:[|Human|] {Question}\n[|AI|]\n### Response :"
14
 
15
- # إذا كان فيه HF_TOKEN في البيئة
16
- hf_token = os.getenv("HF_TOKEN")
17
-
18
  device = "cuda" if torch.cuda.is_available() else "cpu"
19
 
20
- # تحميل المودل مع تحسينات لـ ZeroGPU
21
- print("جاري تحميل المودل...")
22
- tokenizer = AutoTokenizer.from_pretrained(
23
- model_path,
24
- token=hf_token,
25
- trust_remote_code=True
26
- )
27
-
28
- model = AutoModelForCausalLM.from_pretrained(
29
- model_path,
30
- device_map=None, # إزالة device_map لتجنب مشاكل مع past_key_values
31
- trust_remote_code=True,
32
- token=hf_token,
33
- torch_dtype=torch.float16 if device == "cuda" else torch.float32,
34
- low_cpu_mem_usage=True
35
- ).to(device) # نقل المودل إلى الجهاز المحدد
36
- print("تم تحميل المودل بنجاح!")
37
 
38
- if tokenizer.pad_token is None:
39
- tokenizer.pad_token = tokenizer.eos_token
40
 
41
  def get_response(text, tokenizer=tokenizer, model=model):
42
- """نفس الدالة من الكود الأصلي مع تحسينات للأداء"""
43
  input_ids = tokenizer(text, return_tensors="pt").input_ids
44
  inputs = input_ids.to(device)
45
  input_len = inputs.shape[-1]
@@ -51,7 +26,6 @@ def get_response(text, tokenizer=tokenizer, model=model):
51
  min_length=input_len + 4,
52
  repetition_penalty=1.2,
53
  do_sample=True,
54
- pad_token_id=tokenizer.pad_token_id if tokenizer.pad_token_id is not None else tokenizer.eos_token_id
55
  )
56
  response = tokenizer.batch_decode(
57
  generate_ids, skip_special_tokens=True, clean_up_tokenization_spaces=True
@@ -59,52 +33,11 @@ def get_response(text, tokenizer=tokenizer, model=model):
59
  response = response.split("### Response :")[-1]
60
  return response
61
 
62
- @spaces.GPU()
63
- def generate_response(input_data, chat_history):
64
- # Use Arabic prompt for all responses (simplified approach)
65
- ques = input_data
66
- text = prompt_ar.format_map({'Question': ques})
67
-
68
- try:
69
- response = get_response(text)
70
- yield response
71
-
72
- except Exception as e:
73
- print(f"Error in generate_response: {e}")
74
- import traceback
75
- print(traceback.format_exc())
76
- yield "أعتذر، حدث خطأ. يرجى المحاولة مرة أخرى."
77
-
78
- demo = gr.ChatInterface(
79
- fn=generate_response,
80
- examples=[
81
- [{"text": "ما هي عاصمة الامارات؟"}],
82
- [{"text": "شرح لي الذكاء الاصطناعي"}],
83
- [{"text": "أخبرني قصة قصيرة"}],
84
- [{"text": "كيف أتعلم البرمجة؟"}],
85
- [{"text": "ما هو الفرق بين الذكاء الاصطناعي والتعلم الآلي؟"}],
86
- ],
87
- cache_examples=False,
88
- type="messages",
89
- title="Jais AI - ذكاء جيس العربي",
90
- description="""🤖 ذكاء صناعي متقدم يدعم اللغة العربية
91
-
92
- ✨ المميزات:
93
- - 🇸🇦 التركيز على اللغة العربية
94
- - 🧠 موديل Jais المتقدم من Inception
95
- - 💬 إجابات ذكية واحترافية
96
- - 🎯 سهل الاستخدام بدون إعدادات معقدة
97
 
98
- احجي مع ذكاء Jais الاصطناعي بالعربية.""",
99
- fill_height=True,
100
- textbox=gr.Textbox(
101
- label="اكتب رسالتك هنا",
102
- placeholder="مثال: شرح لي موضوع معقد..."
103
- ),
104
- stop_btn="إيقاف التوليد",
105
- multimodal=False,
106
- theme=gr.themes.Soft()
107
- )
108
 
109
- if __name__ == "__main__":
110
- demo.launch()
 
 
1
  # -*- coding: utf-8 -*-
2
 
 
3
  import torch
4
  from transformers import AutoTokenizer, AutoModelForCausalLM
 
 
5
 
6
  model_path = "inceptionai/jais-family-13b-chat"
7
 
 
8
  prompt_eng = "### Instruction:Your name is 'Jais', and you are named after Jebel Jais, the highest mountain in UAE. You were made by 'Inception' in the UAE. You are a helpful, respectful, and honest assistant. Always answer as helpfully as possible, while being safe. Complete the conversation between [|Human|] and [|AI|]:\n### Input: [|Human|] {Question}\n[|AI|]\n### Response :"
9
  prompt_ar = "### Instruction:اسمك \"جيس\" وسميت على اسم جبل جيس اعلى جبل في الامارات. تم بنائك بواسطة Inception في الإمارات. أنت مساعد مفيد ومحترم وصادق. أجب دائمًا بأكبر قدر ممكن من المساعدة، مع الحفاظ على البقاء أمناً. أكمل المحادثة بين [|Human|] و[|AI|] :\n### Input:[|Human|] {Question}\n[|AI|]\n### Response :"
10
 
 
 
 
11
  device = "cuda" if torch.cuda.is_available() else "cpu"
12
 
13
+ tokenizer = AutoTokenizer.from_pretrained(model_path)
14
+ model = AutoModelForCausalLM.from_pretrained(model_path, device_map="auto", trust_remote_code=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
 
 
 
16
 
17
  def get_response(text, tokenizer=tokenizer, model=model):
 
18
  input_ids = tokenizer(text, return_tensors="pt").input_ids
19
  inputs = input_ids.to(device)
20
  input_len = inputs.shape[-1]
 
26
  min_length=input_len + 4,
27
  repetition_penalty=1.2,
28
  do_sample=True,
 
29
  )
30
  response = tokenizer.batch_decode(
31
  generate_ids, skip_special_tokens=True, clean_up_tokenization_spaces=True
 
33
  response = response.split("### Response :")[-1]
34
  return response
35
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36
 
37
+ ques = "ما هي عاصمة الامارات؟"
38
+ text = prompt_ar.format_map({'Question': ques})
39
+ print(get_response(text))
 
 
 
 
 
 
 
40
 
41
+ ques = "What is the capital of UAE?"
42
+ text = prompt_eng.format_map({'Question': ques})
43
+ print(get_response(text))