suprimedev commited on
Commit
064de95
·
verified ·
1 Parent(s): 464752f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -62
app.py CHANGED
@@ -1,80 +1,39 @@
1
  import gradio as gr
2
- from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig
3
  import torch
4
 
5
- # تنظیمات بهینه‌سازی برای اجرا روی رم 16GB
6
- quantization_config = BitsAndBytesConfig(
7
- load_in_4bit=True,
8
- bnb_4bit_compute_dtype=torch.float16,
9
- bnb_4bit_quant_type="nf4",
10
- bnb_4bit_use_double_quant=True,
11
- )
12
 
13
- # بارگذاری مدل با تنظیمات بهینه‌سازی
14
- model_name = "meta-llama/Llama-3.1-8B-Instruct"
15
  tokenizer = AutoTokenizer.from_pretrained(model_name)
16
  model = AutoModelForCausalLM.from_pretrained(
17
  model_name,
18
- quantization_config=quantization_config,
19
- device_map="auto"
20
  )
21
 
22
  def generate_response(prompt, max_new_tokens=512, temperature=0.7):
23
- """
24
- تولید پاسخ بر اساس ورودی کاربر
25
- """
26
- # کدگذاری ورودی کاربر
27
  inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
28
-
29
- # تولید پاسخ
30
  outputs = model.generate(
31
  **inputs,
32
  max_new_tokens=max_new_tokens,
33
  temperature=temperature,
34
- do_sample=True,
35
- pad_token_id=tokenizer.eos_token_id
36
  )
37
-
38
- # تبدیل خروجی به متن
39
- response = tokenizer.decode(outputs[0], skip_special_tokens=True)
40
-
41
- # حذف prompt از پاسخ
42
- response = response[len(prompt):].strip()
43
-
44
- return response
45
 
46
- # رابط Gradio
47
- with gr.Blocks(title="چت بات Llama-3.1-8B-Instruct") as demo:
48
- gr.Markdown("""
49
- # چت بات با مدل Llama-3.1-8B-Instruct
50
- این مدل با بهینه‌سازی برای اجرا روی سیستم‌های با رم محدود (16GB) پیاده‌سازی شده است.
51
- """)
52
-
53
- with gr.Row():
54
- with gr.Column():
55
- user_input = gr.Textbox(label="پیام شما", lines=5, placeholder="پیام خود را اینجا بنویسید...")
56
- with gr.Accordion("تنظیمات پیشرفته", open=False):
57
- max_tokens = gr.Slider(64, 1024, value=512, step=32, label="حداکثر طول پاسخ")
58
- temperature = gr.Slider(0.1, 1.5, value=0.7, step=0.1, label="دمای پاسخ (خلاقیت)")
59
- submit_btn = gr.Button("ارسال")
60
-
61
- with gr.Column():
62
- output_text = gr.Textbox(label="پاسخ مدل", lines=10, interactive=False)
63
-
64
- # رویدادها
65
- submit_btn.click(
66
- fn=generate_response,
67
- inputs=[user_input, max_tokens, temperature],
68
- outputs=output_text
69
- )
70
-
71
- # امکان ارسال با Enter
72
- user_input.submit(
73
- fn=generate_response,
74
- inputs=[user_input, max_tokens, temperature],
75
- outputs=output_text
76
- )
77
 
78
- # اجرای برنامه
79
- if __name__ == "__main__":
80
- demo.launch(share=False)
 
1
  import gradio as gr
2
+ from transformers import AutoTokenizer, AutoModelForCausalLM
3
  import torch
4
 
5
+ # استفاده از مدل باز جایگزین
6
+ model_name = "mistralai/Mistral-7B-Instruct-v0.2" # یا "google/gemma-7b"
 
 
 
 
 
7
 
 
 
8
  tokenizer = AutoTokenizer.from_pretrained(model_name)
9
  model = AutoModelForCausalLM.from_pretrained(
10
  model_name,
11
+ device_map="auto",
12
+ torch_dtype=torch.float16
13
  )
14
 
15
  def generate_response(prompt, max_new_tokens=512, temperature=0.7):
 
 
 
 
16
  inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
 
 
17
  outputs = model.generate(
18
  **inputs,
19
  max_new_tokens=max_new_tokens,
20
  temperature=temperature,
21
+ do_sample=True
 
22
  )
23
+ return tokenizer.decode(outputs[0], skip_special_tokens=True)
 
 
 
 
 
 
 
24
 
25
+ with gr.Blocks() as demo:
26
+ gr.Markdown("# چت بات هوشمند")
27
+ chatbot = gr.Chatbot()
28
+ msg = gr.Textbox()
29
+ clear = gr.Button("پاک کردن")
30
+
31
+ def respond(message, chat_history):
32
+ response = generate_response(message)
33
+ chat_history.append((message, response))
34
+ return "", chat_history
35
+
36
+ msg.submit(respond, [msg, chatbot], [msg, chatbot])
37
+ clear.click(lambda: None, None, chatbot, queue=False)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38
 
39
+ demo.launch()