LAHJA-AI / app.py
wasmdashai's picture
Update app.py
6009f96 verified
raw
history blame
1.66 kB
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()