File size: 2,004 Bytes
64e7afa
d508438
64e7afa
d508438
 
 
 
64e7afa
d508438
64e7afa
d508438
64e7afa
d508438
 
 
 
64e7afa
d508438
 
 
 
 
 
 
 
 
 
 
64e7afa
 
d508438
 
 
 
64e7afa
d508438
64e7afa
 
d508438
64e7afa
d508438
64e7afa
d508438
 
 
 
64e7afa
d508438
 
 
 
 
 
 
64e7afa
 
d508438
 
64e7afa
 
 
 
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
52
53
54
55
56
57
58
59
60
61
62
63
import gradio as gr
from transformers import AutoModelForCausalLM, AutoTokenizer

# بارگذاری مدل و توکنایزر از Hugging Face
MODEL_NAME = "meta-llama/Llama-2-7b-chat-hf"  # مدل Llama 2 (نسخه Chat)
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
model = AutoModelForCausalLM.from_pretrained(MODEL_NAME, device_map="auto")

# تعریف تابع پاسخ‌دهی
def respond(
    message: str,
    history: list[tuple[str, str]],
    system_message: str,
    max_tokens: int,
    temperature: float,
    top_p: float,
):
    # ساختن prompt از پیام‌های قبلی
    context = f"{system_message}\n"
    for user_message, bot_response in history:
        context += f"User: {user_message}\nBot: {bot_response}\n"
    context += f"User: {message}\nBot:"

    # تولید پاسخ
    inputs = tokenizer(context, return_tensors="pt", padding=True, truncation=True)
    outputs = model.generate(
        inputs["input_ids"],
        max_new_tokens=max_tokens,
        temperature=temperature,
        top_p=top_p,
        pad_token_id=tokenizer.eos_token_id,
    )
    response = tokenizer.decode(outputs[0], skip_special_tokens=True)
    response = response.split("Bot:")[-1].strip()  # استخراج پاسخ

    yield response


# رابط کاربری Gradio
demo = gr.ChatInterface(
    fn=respond,
    additional_inputs=[
        gr.Textbox(
            value="You are an advanced and friendly assistant.",
            label="System message",
        ),
        gr.Slider(
            minimum=10, maximum=1024, value=256, step=1, label="Max new tokens"
        ),
        gr.Slider(
            minimum=0.1, maximum=2.0, value=0.7, step=0.1, label="Temperature"
        ),
        gr.Slider(
            minimum=0.1, maximum=1.0, value=0.9, step=0.05, label="Top-p"
        ),
    ],
    title="Advanced Chatbot with Llama 2",
    description="A conversational AI based on Llama 2 fine-tuned for chat.",
)

if __name__ == "__main__":
    demo.launch()