Spaces:
Running
on
Zero
Running
on
Zero
File size: 1,674 Bytes
6009f96 b765d86 6009f96 f7906ff 6009f96 3ead256 6009f96 b765d86 6009f96 b765d86 6009f96 b765d86 6009f96 b765d86 6009f96 b765d86 6009f96 b765d86 0ff1aee b765d86 037d3a3 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
from transformers import AutoTokenizer, AutoModelForCausalLM
import torch
import gradio as gr
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
# تحميل النموذج والمحول
model_id = "wasmdashai/Seed-Coder-8B-Instruct-V1"
tokenizer = AutoTokenizer.from_pretrained(model_id, trust_remote_code=True)
model = AutoModelForCausalLM.from_pretrained(
model_id,
torch_dtype=torch.bfloat16,
device_map="auto",
trust_remote_code=True
)
# دالة الرد
def respond(message, chat_history):
# تجهيز الرسائل لتكون في تنسيق chat
messages = [{"role": "user", "content": m[0]} for m in chat_history]
messages.append({"role": "user", "content": message})
# تحويل الرسائل إلى input_ids
input_ids = tokenizer.apply_chat_template(
messages,
tokenize=True,
return_tensors="pt",
add_generation_prompt=True,
).to(model.device)
# توليد الاستجابة
outputs = model.generate(input_ids, max_new_tokens=512)
response = tokenizer.decode(outputs[0][input_ids.shape[-1]:], skip_special_tokens=True)
# إرجاع الرد و تحديث المحادثة
return response
# واجهة Gradio للدردشة
chat = gr.ChatInterface(
fn=respond,
title="Seed-Coder Chat",
description="شات مباشر مع نموذج Seed-Coder-8B-Instruct",
chatbot=gr.Chatbot(height=450),
textbox=gr.Textbox(placeholder="اكتب سؤالك هنا...", container=False, scale=7),
retry_btn="🔁 إعادة المحاولة",
clear_btn="🗑️ مسح",
)
# تشغيل التطبيق
chat.launch(share=True)
|