Spaces:
Sleeping
Sleeping
Download app.py from kawthr/arabic-model-api: direct link, hf CLI and curl.
- Browser
- Download file 1.23 kB
-
https://huggingface.co/spaces/kawthr/arabic-model-api/resolve/main/app.py
- Command line
-
hf download hf://spaces/kawthr/arabic-model-api/app.py
-
curl -L -o app.py https://huggingface.co/spaces/kawthr/arabic-model-api/resolve/main/app.py
1.23 kB
| import gradio as gr | |
| from transformers import AutoModelForCausalLM, AutoTokenizer | |
| import torch | |
| # تحميل النموذج | |
| model = AutoModelForCausalLM.from_pretrained("./my_lora_model") | |
| tokenizer = AutoTokenizer.from_pretrained("./my_lora_model") | |
| def generate_text(instruction, input_text): | |
| prompt = f"Instruction: {instruction}\nInput: {input_text}\nOutput:" | |
| inputs = tokenizer(prompt, return_tensors="pt") | |
| outputs = model.generate( | |
| **inputs, | |
| max_length=inputs['input_ids'].shape[1] + 150, | |
| do_sample=True, | |
| temperature=0.7, | |
| top_p=0.9, | |
| pad_token_id=tokenizer.eos_token_id | |
| ) | |
| generated_text = tokenizer.decode(outputs[0], skip_special_tokens=True) | |
| response = generated_text[len(prompt):].strip() | |
| return response | |
| # واجهة Gradio | |
| with gr.Blocks() as demo: | |
| gr.Markdown("# مساعد الذكاء الاصطناعي العربي") | |
| instruction = gr.Textbox(label="التعليمات", lines=2) | |
| input_text = gr.Textbox(label="النص", lines=5) | |
| btn = gr.Button("توليد النص") | |
| output = gr.Textbox(label="النتيجة", lines=10) | |
| btn.click(generate_text, [instruction, input_text], output) | |
| demo.launch() |