| | import gradio as gr |
| | from transformers import AutoTokenizer, AutoModelForCausalLM |
| | import torch |
| |
|
| | model_name_or_path = "bigscience/bloom" |
| | device = "cuda" if torch.cuda.is_available() else "cpu" |
| |
|
| | tokenizer = AutoTokenizer.from_pretrained(model_name_or_path) |
| | model = AutoModelForCausalLM.from_pretrained(model_name_or_path).to(device).eval() |
| |
|
| | def generate_text(prompt): |
| | inputs = tokenizer(prompt, return_tensors="pt").to(device) |
| | with torch.no_grad(): |
| | outputs = model.generate( |
| | **inputs, |
| | max_new_tokens=200, |
| | do_sample=True, |
| | top_p=0.9, |
| | temperature=0.8 |
| | ) |
| | return tokenizer.decode(outputs[0], skip_special_tokens=True) |
| |
|
| | iface = gr.Interface( |
| | fn=generate_text, |
| | inputs=gr.Textbox(lines=4, placeholder="اكتب سؤالك هنا..."), |
| | outputs="text", |
| | title="BLOOM Text Generator", |
| | description="قم بإدخال نص وسيقوم النموذج بتوليد نص مكمل أو إجابة." |
| | ) |
| |
|
| | iface.launch() |