File size: 1,532 Bytes
7847a77
afe516c
 
7847a77
afe516c
5a235a9
 
 
 
 
 
 
 
 
 
 
0463dc3
5a235a9
0463dc3
5a235a9
7847a77
afe516c
 
 
 
 
 
 
 
 
 
 
 
 
 
7847a77
afe516c
 
7847a77
afe516c
7847a77
afe516c
 
 
b97eb4d
afe516c
7847a77
3c3d57d
afe516c
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
import gradio as gr
from transformers import AutoModelForCausalLM, AutoTokenizer
import torch

model_id = "Yui-Father/yui-lora-model"
dtype = torch.float16

try:
    tokenizer = AutoTokenizer.from_pretrained(model_id)
    model = AutoModelForCausalLM.from_pretrained(
        model_id,
        torch_dtype=dtype,
        load_in_8bit=True,
        device_map="auto",
        trust_remote_code=True,
    )
    print("Model loaded successfully!")
except Exception as e:
    print(f"Error loading model: {e}")
    raise e

def generate_response(message, history):
    formatted_prompt = f"أنت: {message}\nيوي:"
    inputs = tokenizer(formatted_prompt, return_tensors="pt").to(model.device)
    outputs = model.generate(
        inputs.input_ids,
        max_new_tokens=200,
        do_sample=True,
        top_p=0.7,
        temperature=0.7,
        num_beams=1,
        pad_token_id=tokenizer.eos_token_id,
        eos_token_id=tokenizer.eos_token_id,
    )
    response = tokenizer.decode(outputs[0][inputs.input_ids.shape[1]:], skip_special_tokens=True)

    if "أنت:" in response:
        response = response.split("أنت:")[0].strip()

    return response

iface = gr.ChatInterface(
    generate_response,
    title="تحدث مع يوي (نموذجك الخاص)",
    description="مرحباً بك! أنا يوي، نموذجك الذكي الخاص، جاهزة للمحادثة.",
    examples=[["كيف حالك؟"], ["ما هو طعامك المفضل؟"], ["احكي لي قصة قصيرة."]],
)

iface.launch()