| import gradio as gr |
| import torch |
| from transformers import pipeline, AutoTokenizer |
|
|
| |
| |
| |
| |
| MODEL_ID = "FaizTech/my-tinyllama-test" |
| |
|
|
|
|
| |
|
|
| |
| print(f"بدء تحميل النموذج: {MODEL_ID}") |
|
|
| |
| try: |
| tokenizer = AutoTokenizer.from_pretrained(MODEL_ID) |
| except Exception as e: |
| print(f"خطأ في تحميل الـ Tokenizer: {e}") |
| raise |
|
|
| |
| try: |
| pipe = pipeline( |
| "text-generation", |
| model=MODEL_ID, |
| tokenizer=tokenizer, |
| torch_dtype=torch.bfloat16, |
| device_map="auto" |
| ) |
| print("تم تحميل النموذج بنجاح!") |
| except Exception as e: |
| print(f"خطأ في تحميل النموذج: {e}") |
| raise |
|
|
|
|
| |
| def get_response(prompt_text): |
| """ |
| هذه الدالة تأخذ نصًا وتولد ردًا من النموذج. |
| """ |
| |
| messages = [{"role": "user", "content": prompt_text}] |
| prompt = pipe.tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True) |
|
|
| |
| outputs = pipe( |
| prompt, |
| max_new_tokens=256, |
| do_sample=True, |
| temperature=0.7, |
| top_p=0.95 |
| ) |
| |
| |
| response = outputs[0]["generated_text"] |
| |
| |
| |
| cleaned_response = response.replace(prompt, "").strip() |
| |
| return cleaned_response |
|
|
| |
| demo = gr.Interface( |
| fn=get_response, |
| inputs=gr.Textbox(label="أدخل سؤالك أو أمرك هنا", lines=4), |
| outputs=gr.Textbox(label="إجابة النموذج", lines=4), |
| title=f"واجهة لنموذج {MODEL_ID.split('/')[-1]}", |
| description="هذا تطبيق ويب بسيط لواجهة API لنموذجي الخاص." |
| ) |
|
|
| |
| demo.queue().launch() |