Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,33 +1,48 @@
|
|
| 1 |
-
from transformers import
|
| 2 |
-
import gradio as gr
|
| 3 |
import torch
|
|
|
|
|
|
|
| 4 |
|
| 5 |
-
|
|
|
|
| 6 |
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
|
|
|
| 13 |
|
| 14 |
-
|
| 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 |
-
|
| 20 |
-
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
-
# واجهة
|
| 24 |
chat = gr.ChatInterface(
|
| 25 |
fn=respond,
|
| 26 |
-
title="Seed-Coder
|
| 27 |
-
description="
|
| 28 |
-
chatbot=gr.Chatbot(height=
|
| 29 |
-
textbox=gr.Textbox(placeholder="اكتب
|
| 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 |
|