wasmdashai commited on
Commit
6009f96
·
verified ·
1 Parent(s): 956012f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -19
app.py CHANGED
@@ -1,33 +1,48 @@
1
- from transformers import pipeline
2
- import gradio as gr
3
  import torch
 
 
4
 
5
- import spaces
 
6
 
7
- # تحميل النموذج باستخدام GPU إذا متوفر
8
- pipe = pipeline(
9
- "text-generation",
10
- model="wasmdashai/Seed-Coder-8B-Instruct-V1"
11
-
12
- ).cuda()
 
13
 
14
- @spaces.GPU
15
  def respond(message, chat_history):
 
16
  messages = [{"role": "user", "content": m[0]} for m in chat_history]
17
  messages.append({"role": "user", "content": message})
18
 
19
- output = pipe(messages, max_new_tokens=200, do_sample=True)
20
- reply = output[0]['generated_text']
21
- return reply
 
 
 
 
 
 
 
 
 
 
 
22
 
23
- # واجهة دردشة Gradio
24
  chat = gr.ChatInterface(
25
  fn=respond,
26
- title="Seed-Coder Chatbot",
27
- description="دردشة مع نموذج Seed-Coder-8B-Instruct باستخدام GPU",
28
- chatbot=gr.Chatbot(height=400),
29
- textbox=gr.Textbox(placeholder="اكتب رسالتك هنا...", container=False, scale=7),
30
- retry_btn="🔁 إعادة",
31
  clear_btn="🗑️ مسح",
32
  )
33
 
 
1
+ from transformers import AutoTokenizer, AutoModelForCausalLM
 
2
  import torch
3
+ import gradio as gr
4
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
5
 
6
+ # تحميل النموذج والمحول
7
+ model_id = "wasmdashai/Seed-Coder-8B-Instruct-V1"
8
 
9
+ tokenizer = AutoTokenizer.from_pretrained(model_id, trust_remote_code=True)
10
+ model = AutoModelForCausalLM.from_pretrained(
11
+ model_id,
12
+ torch_dtype=torch.bfloat16,
13
+ device_map="auto",
14
+ trust_remote_code=True
15
+ )
16
 
17
+ # دالة الرد
18
  def respond(message, chat_history):
19
+ # تجهيز الرسائل لتكون في تنسيق chat
20
  messages = [{"role": "user", "content": m[0]} for m in chat_history]
21
  messages.append({"role": "user", "content": message})
22
 
23
+ # تحويل الرسائل إلى input_ids
24
+ input_ids = tokenizer.apply_chat_template(
25
+ messages,
26
+ tokenize=True,
27
+ return_tensors="pt",
28
+ add_generation_prompt=True,
29
+ ).to(model.device)
30
+
31
+ # توليد الاستجابة
32
+ outputs = model.generate(input_ids, max_new_tokens=512)
33
+ response = tokenizer.decode(outputs[0][input_ids.shape[-1]:], skip_special_tokens=True)
34
+
35
+ # إرجاع الرد و تحديث المحادثة
36
+ return response
37
 
38
+ # واجهة Gradio للدردشة
39
  chat = gr.ChatInterface(
40
  fn=respond,
41
+ title="Seed-Coder Chat",
42
+ description="شات مباشر مع نموذج Seed-Coder-8B-Instruct",
43
+ chatbot=gr.Chatbot(height=450),
44
+ textbox=gr.Textbox(placeholder="اكتب سؤالك هنا...", container=False, scale=7),
45
+ retry_btn="🔁 إعادة المحاولة",
46
  clear_btn="🗑️ مسح",
47
  )
48