Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import AutoModel, AutoTokenizer
|
| 3 |
+
import torch
|
| 4 |
+
from PIL import Image
|
| 5 |
+
|
| 6 |
+
# اختر GPU إذا كان متاح
|
| 7 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 8 |
+
|
| 9 |
+
model_name = 'deepseek-ai/DeepSeek-OCR-2'
|
| 10 |
+
|
| 11 |
+
# تحميل النموذج
|
| 12 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
|
| 13 |
+
model = AutoModel.from_pretrained(
|
| 14 |
+
model_name,
|
| 15 |
+
_attn_implementation='flash_attention_2',
|
| 16 |
+
trust_remote_code=True,
|
| 17 |
+
use_safetensors=True
|
| 18 |
+
)
|
| 19 |
+
model = model.eval().to(device).to(torch.bfloat16)
|
| 20 |
+
|
| 21 |
+
def image_to_html(image):
|
| 22 |
+
# تحويل الصورة إلى Tensor
|
| 23 |
+
prompt = "<image>\n<|grounding|>Convert the document to full stack HTML code. "
|
| 24 |
+
|
| 25 |
+
# قراءة الصورة
|
| 26 |
+
image.save("temp.jpg")
|
| 27 |
+
|
| 28 |
+
# بالنسبة للنموذج DeepSeek-OCR-2 عادة يتوقع إدخال بصيغة PIL
|
| 29 |
+
inputs = tokenizer(prompt, return_tensors="pt").to(device)
|
| 30 |
+
|
| 31 |
+
# تمرير النموذج
|
| 32 |
+
with torch.no_grad():
|
| 33 |
+
generated_ids = model.generate(**inputs, max_new_tokens=1024)
|
| 34 |
+
|
| 35 |
+
# فك الترميز
|
| 36 |
+
result = tokenizer.decode(generated_ids[0], skip_special_tokens=True)
|
| 37 |
+
return result
|
| 38 |
+
|
| 39 |
+
# واجهة Gradio
|
| 40 |
+
iface = gr.Interface(
|
| 41 |
+
fn=image_to_html,
|
| 42 |
+
inputs=gr.Image(type="pil"),
|
| 43 |
+
outputs=gr.Textbox(lines=20),
|
| 44 |
+
title="AI Full Stack HTML Generator",
|
| 45 |
+
description="ارفع صورة، وسيقوم النموذج بتحويلها إلى كود HTML/JS/CSS كامل."
|
| 46 |
+
)
|
| 47 |
+
|
| 48 |
+
iface.launch()
|