Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,80 +1,39 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 3 |
import torch
|
| 4 |
|
| 5 |
-
#
|
| 6 |
-
|
| 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 |
-
|
| 19 |
-
|
| 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 |
-
|
| 47 |
-
|
| 48 |
-
gr.
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 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()
|
|
|
|
|
|